I am trying to add a scrollbar to my JTextarea but the scrollbar does not show up neither my Jtoolbar
can anyone please tell me what is wrong with this code. so that i can fix it. I have been looking eeverywhere but The scrollpane stil does not show up
public PlayerGui() {
// create main windows
super("Liste");
JTextArea editors = new JTextArea();
editors.setLineWrap(true);
editors.setWrapStyleWord(true);
// scroll bar
JScrollPane scroll = new JScrollPane(editors);
setEditor(editors);
// create center panel
JPanel cent = new JPanel();
//create Panel for the to
JPanel north = new JPanel();
setNorthpanel(north);
// create tool bar
JToolBar toolbar = new JToolBar();
toolbar.add(scroll);
// set center panel and add preferred layout and backgrounds and size
setCenter(cent);
getCenter().setLayout(new BorderLayout());
// add scroll bar and toolbar
add(scroll, BorderLayout.EAST);
add(toolbar, BorderLayout.SOUTH);
//getCenter().setBackground(Color.black);
Dimension size = new Dimension(getCenter().getPreferredSize());
getEditor().setPreferredSize(size);
getCenter().getPreferredSize();
getCenter().setBorder(new CompoundBorder(new EmptyBorder(10,10 ,10,10),new EtchedBorder(Color.BLACK, Color.black)));
//add text editor to the center panel
getCenter().add(getEditor(),BorderLayout.CENTER);
//set layout of the frame
setLayout(new BorderLayout());
menubar1 = new JMenuBar();
//create menu list from a string arrays
for(int i=0; i<list.length; i++){
JMenu menus = new JMenu(list[i]);
menubar1.add(menus);
}
You are using the layout managers incorrectly. Please find your modified code which is working.
public class TestFrame extends JFrame {
private static final long serialVersionUID = 1L;
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
TestFrame frame = new TestFrame();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
public TestFrame() {
// create main windows
super("Liste");
// scroll bar
JScrollPane scroll = new JScrollPane();
//setEditor(editors);
// create center panel
JPanel cent = new JPanel();
//create Panel for the to
JPanel north = new JPanel();
getContentPane().setLayout(new BorderLayout(0, 0));
//toolbar.add(scroll);
// set center panel and add preferred layout and backgrounds and size
//setCenter(cent);
//getCenter().setLayout(new BorderLayout());
// add scroll bar and toolbar
getContentPane().add(scroll);
JTextArea textArea = new JTextArea();
scroll.setViewportView(textArea);
JToolBar toolBar = new JToolBar();
getContentPane().add(toolBar, BorderLayout.NORTH);
JMenuBar menubar1 = new JMenuBar();
//create menu list from a string arrays
// for(int i=0; i<list.length; i++){
// JMenu menus = new JMenu(list[i]);
// menubar1.add(menus);
// }
}
}
Hope this will be helpful. :-)
Related
I am trying to make a dynamic tabs with the option to add new ones in the application and some buttons next to it... To do so I have a main class:
private static void initAndDisplayUI() {
frame = new JFrame(...)
tabbedPane = new TabbedPane();
insertTab(tabbedPane, TabFactory.createTab(), true);
insertNewTabButton(tabbedPane);
...
}
}
Container class:
public class TabbedPane extends JPanel {public TabbedPane() {
this.captions = new TabCaptions();
this.tabs = new ArrayList<Tab>();
this.contentContainer = new JPanel(new BorderLayout());
setLayout(new BorderLayout());
add(captions, BorderLayout.NORTH);
add(contentContainer, BorderLayout.CENTER);
}
...
}
And a TabCaptions:
public class TabCaptions extends JPanel {
private TabCaption selectedTab;
private JComponent tabsPane;
private JScrollPane scrollPane;
private JPanel buttonsPane;
public TabCaptions() {
createUI();
}
private void createUI() {
setLayout(new BoxLayout(this, BoxLayout.X_AXIS));
setBackground(Color.DARK_GRAY);
add(createTabsPane());
add(createButtonsPane());
add(Box.createHorizontalGlue());
}
private JComponent createTabsPane() {
tabsPane = new JPanel();
tabsPane.setOpaque(false);
tabsPane.setLayout(new BoxLayout(tabsPane, BoxLayout.X_AXIS));
scrollPane = new JScrollPane(tabsPane, JScrollPane.VERTICAL_SCROLLBAR_NEVER, JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
return tabsPane;
}
...
}
As a result I have a region with the tabs and some button next to it. However the app window draws a scrollPane with some weird size... I would like to display this "add new tab" button right next to the tabs that are created, resize it when the new tab is being added BUT with the functionality to display a scrollbar once it hits the maximum window width. I already have the scrolling but how can I make the behaviour of this dynamic position of new page?
I already have the scrolling
Not based on the code you have provided:
scrollPane = new JScrollPane(tabsPane, JScrollPane.VERTICAL_SCROLLBAR_NEVER, JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
return tabsPane;
You need to add the JScrollPane to the frame, so you need to return the scroll pane from the method:
scrollPane = new JScrollPane(tabsPane, JScrollPane.VERTICAL_SCROLLBAR_NEVER, JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
//return tabsPane;
return scrollPane;
I got a project where I always want to center the focused JPanel. So I thought I can just change the Viewport position. But I can't use the viewport. I created an example project to show how I use the viewport. I just want that the user only see one of the orange boxes. But it should be also possible to view all boxes at once. So the view has to zoom in or something like this. How can I fix this problem?
My example:
import javax.swing.*;
import java.awt.*;
public class main {
public static void main(String [] args){
//create JFrame
JFrame _frame = new JFrame();
//create Viewport
JViewport _view = new JViewport();
//create Mainpanel
JPanel _mainPanel = new JPanel();
//tell the view to handle mainpanel
_view.setView(_mainPanel);
//create Layout
GridLayout _layout = new GridLayout(3,3,3,3);
//set gridlayout to mainpanel
_mainPanel.setLayout(_layout);
for(int i = 0;i<12;i++){
JPanel _tempPanel = new JPanel();
_tempPanel.setBackground(Color.ORANGE);
_tempPanel.setBorder(BorderFactory.createLineBorder(Color.black));
_mainPanel.add(_tempPanel);
}
_view.setExtentSize(new Dimension(300,300));
//add mainpanel to frame
_frame.add(_mainPanel);
_frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
_frame.pack();
//set size of Jframe
_frame.setSize(1000,1000);
_frame.setVisible(true);
}
}
JViewPort can not help you with your requirement.
Here is an ugly but running code. You can improve it yourself.
public static void main(String[] args) {
// create JFrame
JFrame _frame = new JFrame();
JPanel conPanel = new JPanel(new BorderLayout());
// create Mainpanel
JPanel _mainPanel = new JPanel() {
#Override
public String toString() {
return "All";
}
};
// create Layout
GridLayout _layout = new GridLayout(3, 3, 3, 3);
// set gridlayout to mainpanel
_mainPanel.setLayout(_layout);
JComboBox<JPanel> combo = new JComboBox<>();
combo.addItem(_mainPanel);
for (int i = 0; i < 12; i++) {
final int fi = i;
JPanel _tempPanel = new JPanel() {
#Override
public String toString() {
return "Panel" + fi;
}
#Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
g.drawString(toString(), 5, 15);
}
};
_tempPanel.setBackground(Color.ORANGE);
_tempPanel.setBorder(BorderFactory.createLineBorder(Color.black));
_mainPanel.add(_tempPanel);
combo.addItem(_tempPanel);
}
combo.addActionListener( e -> {
JPanel panel = (JPanel)combo.getSelectedItem();
conPanel.remove(_mainPanel);
_mainPanel.removeAll();
for(int i = 1; i < combo.getItemCount(); i++)
_mainPanel.add(combo.getItemAt(i));
conPanel.add(panel, BorderLayout.CENTER);
conPanel.revalidate();
conPanel.repaint();
} );
conPanel.add(_mainPanel, BorderLayout.CENTER);
JPanel buttonsPanel = new JPanel(new FlowLayout(FlowLayout.CENTER));
buttonsPanel.add(combo);
conPanel.add(buttonsPanel, BorderLayout.SOUTH);
// add mainpanel to frame
_frame.setContentPane(conPanel);
_frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// set size of Jframe
_frame.setSize(1000, 1000);
_frame.setVisible(true);
}
Essentially, I am trying to add a home screen with 4 buttons, 3 difficulty buttons and a play button. I add the buttons to a JPanel and add the JPanel with a BoxLayout of Center. Why does the buttons still go all the way off to the right? Setting the icon for a JLabel on and adding it to the home screen JPanel is a possible mess up the flow of components? I want the difficulty buttons to be on top of the of the gif with the Play button at the bottom. Thanks for your help.
//container
snake = new JFrame();
snake.setLayout(new BorderLayout());
//home screen panel
homeScreen = new JPanel();
homeScreen.setLayout(new BoxLayout(homeScreen, BoxLayout.X_AXIS));
homeScreen.setPreferredSize(new Dimension(320, 320));
JLabel bg = new JLabel();
ImageIcon icon = new ImageIcon("HomeBG.gif");
icon.getImage().flush();
bg.setIcon(icon);
homeScreen.add(bg);
easy = new JButton("Easy");
medium = new JButton("Medium");
hard = new JButton("Hard");
play = new JButton("Play");
//button listeners code here
homeScreen.add(easy);
homeScreen.add(medium);
homeScreen.add(hard);
homeScreen.add(play);
snake.add(homeScreen, BorderLayout.CENTER);
snake.setTitle("Snake Game");
snake.pack();
snake.setVisible(true);
snake.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
You need to change your code as shown below.
snake = new JFrame();
snake.setLayout(new BorderLayout());
//home screen panel
homeScreen = new JPanel(new BorderLayout());
//homeScreen.setLayout(new BoxLayout(homeScreen, BoxLayout.X_AXIS));
homeScreen.setPreferredSize(new Dimension(320, 320)); // probably you need to remove this line!
JLabel bg = new JLabel();
ImageIcon icon = new ImageIcon("HomeBG.gif");
icon.getImage().flush();
bg.setIcon(icon);
homeScreen.add(bg);
easy = new JButton("Easy");
medium = new JButton("Medium");
hard = new JButton("Hard");
play = new JButton("Play");
//button listeners code here
JPanel buttonsPanel = new JPanel(new FlowLayout(FlowLayout.CENTER));
buttonsPanel.add(easy);
buttonsPanel.add(medium);
buttonsPanel.add(hard);
buttonsPanel.add(play);
homeScreen.add(buttonsPanel, BorderLayout.NORTH);
snake.add(homeScreen, BorderLayout.CENTER);
snake.setTitle("Snake Game");
snake.pack();
snake.setVisible(true);
snake.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
I would use a compound layout for this. Put the level buttons in a (panel in a) FlowLayout. Put the play button in a 2nd FlowLayout. Add those panels to the PAGE_START and PAGE_END of a BorderLayout. Add a label containing the GIF to the CENTER of the same border layout.
BTW - the level buttons should be radio buttons (in a button group - BNI).
import java.awt.*;
import java.awt.image.BufferedImage;
import javax.swing.*;
import javax.swing.border.EmptyBorder;
public class LayoutManagersWithIcon {
private JComponent ui = null;
LayoutManagersWithIcon() {
initUI();
}
public void initUI() {
if (ui!=null) return;
ui = new JPanel(new BorderLayout(4,4));
ui.setBorder(new EmptyBorder(4,4,4,4));
JPanel levelPanel = new JPanel(new FlowLayout(FlowLayout.CENTER, 5, 5));
ui.add(levelPanel, BorderLayout.PAGE_START);
levelPanel.add(new JRadioButton("Easy"));
levelPanel.add(new JRadioButton("Medium"));
levelPanel.add(new JRadioButton("Hard"));
JPanel startPanel = new JPanel(new FlowLayout(FlowLayout.CENTER, 5, 5));
ui.add(startPanel, BorderLayout.PAGE_END);
startPanel.add(new JButton("Play"));
JLabel label = new JLabel(new ImageIcon(
new BufferedImage(400, 100, BufferedImage.TYPE_INT_RGB)));
ui.add(label);
}
public JComponent getUI() {
return ui;
}
public static void main(String[] args) {
Runnable r = new Runnable() {
#Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (Exception useDefault) {
}
LayoutManagersWithIcon o = new LayoutManagersWithIcon();
JFrame f = new JFrame(o.getClass().getSimpleName());
f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
f.setLocationByPlatform(true);
f.setContentPane(o.getUI());
f.pack();
f.setMinimumSize(f.getSize());
f.setVisible(true);
}
};
SwingUtilities.invokeLater(r);
}
}
I have a JPanel which is in a box layout but I am unsure how to align the JPanel to center of the window (and stay centered even if window is resized) I've tried looking for a solution but all questions seem over complicated compared to what it is that I'm looking for.
import java.awt.*;
import javax.swing.*;
public class Stacker extends JFrame {
public Stacker() {
super("Stacker");
setSize(430, 200);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// create top panel
JPanel commandPane = new JPanel();
BoxLayout vertical = new BoxLayout(commandPane,
BoxLayout.Y_AXIS);
commandPane.setLayout(vertical);
JButton subscribe = new JButton("Subscribe");
JButton unsubscribe = new JButton("Unsubscribe");
JButton refresh = new JButton("Refresh");
JButton save = new JButton("Save");
commandPane.add(subscribe);
commandPane.add(unsubscribe);
commandPane.add(refresh);
commandPane.add(save);
JMenuItem j1 = new JMenuItem("File");
JMenuItem j2 = new JMenuItem("Open");
JMenuItem j3 = new JMenuItem("Close");
JMenuBar menubar = new JMenuBar();
JMenu menu = new JMenu("Feeds");
menu.add(j1);
menu.add(j2);
menu.add(j3);
menubar.add(menu);
setJMenuBar(menubar);
// create bottom panel
/*JPanel textPane = new JPanel();
JTextArea text = new JTextArea(4, 70);
JScrollPane scrollPane = new JScrollPane(text);
// put them together
FlowLayout flow = new FlowLayout();
setLayout(flow);
add(commandPane);
add(scrollPane); */
setJMenuBar(menubar);
add(commandPane);
setVisible(true);
}
public static void main(String[] arguments) {
Stacker st = new Stacker();
}
}
You say you're using a BoxLayout, but is the JPanel with the BoxLayout the JPanel you want to center, or does it contain the JPanel you want to center?
If it contains the JPanel you want to center, then you can add a glue on either side of the JPanel to be centered. If it is the JPanel you want to center, then you can use GridBagLayout or BoxLayout to achieve the effect you're talking about.
Googling something like "Java center component" will give you a ton of results.
for this idea (still not clear from your description) use GridBagLayout without set for GridBagConstraints
.
.
.
import java.awt.*;
import javax.swing.*;
public class CenteredJPanel {
private JFrame frame = new JFrame("Test");
private JPanel panel = new JPanel();
private JButton subscribe = new JButton("Subscribe");
private JButton unsubscribe = new JButton("Unsubscribe");
private JButton refresh = new JButton("Refresh");
private JButton save = new JButton("Save");
public CenteredJPanel() {
panel.setLayout(new GridBagLayout());
panel.add(subscribe);
panel.add(unsubscribe);
panel.add(refresh);
panel.add(save);
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
frame.add(panel);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
#Override
public void run() {
CenteredJPanel centeredJLabel = new CenteredJPanel();
}
});
}
}
Problem:
My program layout is fine, as below before I add JToolbar to BorderLayout.PAGE_START
Here's a screenshot before JToolbar is added:
Here's how it looked like after adding JToolbar:
May I know what did I do wrong?
Here's the code I used:
//Create the text pane and configure it.
textPane = new JTextPane();
-snipped code-
JScrollPane scrollPane = new JScrollPane(textPane);
scrollPane.setPreferredSize(new Dimension(300, 300));
//Create the text area for the status log and configure it.
changeLog = new JTextArea(5, 30);
changeLog.setEditable(false);
JScrollPane scrollPaneForLog = new JScrollPane(changeLog);
//Create a split pane for the change log and the text area.
JSplitPane splitPane = new JSplitPane(
JSplitPane.VERTICAL_SPLIT,
scrollPane, scrollPaneForLog);
splitPane.setOneTouchExpandable(true);
//Create the status area.
JPanel statusPane = new JPanel(new GridLayout(1, 1));
CaretListenerLabel caretListenerLabel =
new CaretListenerLabel("Caret Status");
statusPane.add(caretListenerLabel);
//Create the toolbar
JToolBar toolBar = new JToolBar();
-snipped code-
//Add the components.
getContentPane().add(toolBar, BorderLayout.PAGE_START);
getContentPane().add(splitPane, BorderLayout.CENTER);
getContentPane().add(statusPane, BorderLayout.PAGE_END);
//Set up the menu bar.
actions = createActionTable(textPane);
JMenu editMenu = createEditMenu();
JMenu styleMenu = createStyleMenu();
JMenuBar mb = new JMenuBar();
mb.add(editMenu);
mb.add(styleMenu);
setJMenuBar(mb);
Please help, I'm new to GUI Building, and I don't feel like using Netbeans to drag and drop the UI for me... Thank you in advance.
Instead of using setSize() on the JFrame, set the preferred size of your center component as you do now and invoke pack(), which "Causes this Window to be sized to fit the preferred size and layouts of its subcomponents." Expanding on #Bragaadeesh's example,
public static void main(String[] args) {
TestFrame frame = new TestFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.build();
frame.pack();
frame.setVisible(true);
}
Then, change to scrollPane.setPreferredSize(new Dimension(500, 300)) or JTextArea changeLog = new JTextArea(10, 30) to see the difference.
I don't know what is the issue. I tried to run it on my system by fixing the compilation issues. Here is the code and screenshot.
import java.awt.*;
import javax.swing.*;
public class TestFrame extends JFrame{
public static void main(String[] args) {
TestFrame frame = new TestFrame();
frame.build();
frame.setVisible(true);
}
public void build(){
setSize(600,600);
//Create the text pane and configure it.
JTextPane textPane = new JTextPane();
JScrollPane scrollPane = new JScrollPane(textPane);
scrollPane.setPreferredSize(new Dimension(300, 300));
//Create the text area for the status log and configure it.
JTextArea changeLog = new JTextArea(5, 30);
changeLog.setEditable(false);
JScrollPane scrollPaneForLog = new JScrollPane(changeLog);
//Create a split pane for the change log and the text area.
JSplitPane splitPane = new JSplitPane(
JSplitPane.VERTICAL_SPLIT,
scrollPane, scrollPaneForLog);
splitPane.setOneTouchExpandable(true);
//Create the status area.
JPanel statusPane = new JPanel(new GridLayout(1, 1));
JLabel caretListenerLabel =
new JLabel("Caret Status");
statusPane.add(caretListenerLabel);
//Create the toolbar
JToolBar toolBar = new JToolBar();
toolBar.add(new JButton("Btn1"));
toolBar.add(new JButton("Btn2"));
toolBar.add(new JButton("Btn3"));
toolBar.add(new JButton("Btn4"));
//Add the components.
getContentPane().add(toolBar, BorderLayout.PAGE_START);
getContentPane().add(splitPane, BorderLayout.CENTER);
getContentPane().add(statusPane, BorderLayout.PAGE_END);
//Set up the menu bar.
JMenu editMenu = new JMenu("test");
JMenu styleMenu = new JMenu("test");
JMenuBar mb = new JMenuBar();
mb.add(editMenu);
mb.add(styleMenu);
setJMenuBar(mb);
}
}
EDIT: I understand why now.
I used Paint to give me a rough estimation of the pixels, and previously I did not know that the height from the start of the top frame title bar is counted! So that adds up to ~= 504. I get it now.
So next time when I have to set the height roughly, I think I'll use Paint.
Hmmm weird. I have to change from:
//Display the window.
frame.setSize(640, 480);
to
//Display the window.
frame.setSize(640, 504);
Then only it works.
Can somebody teach me how to estimate or set the width/height for the components? Because initially I wanted it to be 640,480 but apparently now it needs 640,504.