I have got a window that should display the following:
JLablel "Have you used GUI before?" on the top, centered
two radioButtons "Yes" and "No" below it, somewhat in the center, a little bit towards the left
a JButton "NEXT" in the bottom-right corner
All three elements should have green font and darkGrey background.
The problem is that the window which is showing up, does not look like I would like it to.
And this is my code:
yesButton = new JRadioButton(yes);
//yesButton.setMnemonic(KeyEvent.VK_B); // doesn't work?
yesButton.setActionCommand(yes);
noButton = new JRadioButton(no);
// noButton.setMnemonic(KeyEvent.VK_C); // doesn't work?
noButton.setActionCommand(no);
ButtonGroup group = new ButtonGroup();
group.add(yesButton);
group.add(noButton);
nextButton = new JButton("NEXT");
nextButton.setActionCommand(next);
yesButton.addActionListener(this);
noButton.addActionListener(this);
nextButton.addActionListener(this);
JPanel radioPanel = new JPanel(new GridLayout(0, 1));
radioPanel.add(yesButton);
radioPanel.add(noButton);
add(radioPanel, BorderLayout.WEST);
// setBorder(BorderFactory.createEmptyBorder(20,20,20,20));
// radioPanel.setBorder(new EmptyBorder(250, 250, 20, 20));
// there is no difference between the above two, right?
String q = "Have you used GUI before?";
JPanel area = new JPanel(new BorderLayout());
area.setBackground(Color.darkGray);
JLabel textLabel2 = new JLabel("<html><div style=\"text-align: center;\">"
+ q + "</html>", SwingConstants.CENTER);
textLabel2.setForeground(Color.green);
Font font2 = new Font("SansSerif", Font.PLAIN, 30);
textLabel2.setFont(font2);
//textLabel2.setBorder(new EmptyBorder(0, 0, 250, 0)); //top, left, bottom, right
area.add(textLabel2, BorderLayout.NORTH);
area.add(nextButton, BorderLayout.EAST);
add(area, BorderLayout.CENTER);
I feel I'm nearly there, thanks for any help!
--EDIT--
A screenshot:
You need to use nested panels.
for the BorderLayout.NORTH you can add the JLabel directly. You will need to set the horizontal text alignment to center.
for the radio buttons you can create a JPanel with a FlowLayout and then add the buttons to the panel and add the panel to the CENTER.
for the button you add the button to a panel using a FlowLayout that is right aligned, then add the panel to the SOUTH.
There are other choices. You could also use a Vertical BoxLayout as the layout of the main panel and then add child panels to it.
You won't be able to get much control with just a BorderLayout. Try something else like MigLayout or one of the other many many layout managers Java has (GridBag, Box, etc).
In MigLayout it would look something like:
area.setLayout(new MigLayout("fill"));
area.add(textLabel2, "wrap");
area.add(radioPanel, "wrap");
area.add(nextButton, "tag right");
Related
This seems like a simple thing to do, but I can't get it to work.
I have a BorderLayout. I want to use the top part for a title bar. I want to add a JPanel with labels, buttons and other components. However, the PAGE_START part of the border layout won't left align the panel. Here's the situation, with comments in where I've tried to set the alignment.
I noticed that when I don't add a panel to the border layout, and just write the JLabel straight in, it has left alignment by default.
This is not what I want, though, because I am planning on putting a BoxLayout.X_AXIS horizontally through the BorderLayout.PAGE_START title area. Seems to be a reasonable thing to do?
The Container pane argument to the static method is just the single panel on the main JFrame.
public static void addComponentsToPane(Container pane)
{
JLabel jlabel = new JLabel("I want to left align this inside a JPanel");
// Doesn't work: jlabel.setAlignmentX(Component.LEFT_ALIGNMENT);
JPanel jpanel = new JPanel();
//Doesn't work: jlabel.setAlignmentX(Component.LEFT_ALIGNMENT);
jpanel.add(jlabel);
pane.add(jpanel, BorderLayout.PAGE_START);
// Other parts of the BoxLayout (works fine)
JButton button = new JButton("Button 2 (CENTER)");
button.setPreferredSize(new Dimension(200, 100));
pane.add(button, BorderLayout.CENTER);
button = new JButton("Button 3 (LINE_START)");
pane.add(button, BorderLayout.LINE_START);
button = new JButton("Long-Named Button 4 (PAGE_END)");
pane.add(button, BorderLayout.PAGE_END);
button = new JButton("5 (LINE_END)");
pane.add(button, BorderLayout.LINE_END);
}
Even when I tell the panel to left align the label, it doesn't appear left aligned.
Does anyone know what I am doing wrong?
By default a JPanel uses a FlowLayout with "center" alignment.
if you want components "left" aligned, then you need to set the layout on the panel to use a FlowLayout with "left" alignment.
Read the FlowLayout API for the proper constructor to use to set the alignment.
Or you can also read the Swing tutorial on How to Use FlowLayut which gives the constructors and valid values to specify the alignment.
In a part of my software I have a layout on bottom that holds couple of JButtons and a JLabel. I want to keep buttons one the right side of the panel, and label on the left side. I could manage to put buttons on the right, but do not know how to keep the JLabel on the left side.
Here is the code:
bottomPanel = new JPanel(new FlowLayout(FlowLayout.RIGHT));
ftpBack = new JButton("Back");
ftpNext = new JButton("Next");
label = new JLabel("Text);
bottomPanel.add(label);
bottomPanel.add(ftpBack);
bottomPanel.add(ftpNext);
mainPanel.add(bottomPanel, BorderLayout.SOUTH);
This is what I am trying to achieve:
Any idea how to make it ?
You can't do this with a FlowLayout.
You can use a horizontal BoxLayout:
Box box = Box.createHorizontalBox();
box.add(label);
box.add(Box.createHorizontalGlue());
box.add(backButton);
box.add(Box.createHorizontalStrut(5));
box.add(nextButton);
Read the section from the Swing tutorial on How to Use BoxLayout for more information and examples.
Or another approach is to nest layout managers:
JPanel main = new JPanel( new BorderLayout() );
main.add(label, BorderLayout.WEST);
JPanel buttonPanel= new JPanel();
buttonPanel.add(back);
buttonPanel.add(next);
main.add(buttonPanel, BorderLayout.EAST);
I am trying to make a panel to have 4 items on top. these are a JLabel, JTextField, JLabel and JTextField.
In the center I need a JTextArea and to the left of it a JList that is scrollable.
On the bottom I need 3 buttons.
What would be the best layout manager for this and how should I go about it?
Would having just 3 columns be a good idea?
Heres what I have so far:
JPanel panel = new JPanel();
JTextField IDLabel = new JLabel("ID: ");
IDLabel.setBounds(10, 10, 80, 25);
panel.add(IDLabel);
JTextArea IDText = new JTextField(5);
IDText.setBounds(100, 10, 160, 25);
panel.add(IDText);
JLabel TitleLabel = new JLabel("Title: ");
TitleLabel.setBounds(10, 10, 80, 25);
panel.add(TitleLabel);
JTextField TitleText = new JTextField(10);
TitleText.setBounds(100, 10, 160, 25);
panel.add(TitleText);
JList list = new JList(new String[]{"test1", "test22"});
list.setFixedCellWidth(150);
list.setFixedCellHeight(50);
list.setFont(new Font("Serif",Font.BOLD,16));
list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
panel.add(list);
JTextArea BodyArea = new JTextArea();
BodyArea.setSize(200, 200);
BodyArea.setText("Test area");
panel.add(BodyArea);
You will mostly likely need to use a combination of layouts (AKA compound layouts), for example
North Panel
Create a JPanel and assign it a FlowLayout or GridBagLayout or GridLayout depending on what you want to achieve.
Add the JLabel, JTextField, JLabel, JTextField to it.
Center Panel
Create a JPanel with BorderLayout. Add the JTextArea to the CENTER position and the JList to the WEST position
South Panel
Craete a JPanel with a FlowLayout or GridBagLayout or GridLayout depending on what you want to achieve.
Add the buttons to it.
Putting it together
Create a JPanel with a BorderLayout, add the "north" panel to the NORTH position, the "center" panel to the CENTER position and the "south" panel to the SOUTH position
You could use a single container and a GridBagLayout, but that's a lot of work.
Take a look at Laying Out Components Within a Container for more details
Maybe start with a BorderLayout for the main layout. Then you can add components to the PAGE_START (NORTH), LINE_START (WEST) and CENTER and PAGE_END (SOUTH). Check out the section from the Swing tutorial on Using Layout Manager, for more information and examples.
Of course you would also use panels when you want to display multiple components in a single area. So your buttons would first be added to panels.
Also, follow standard naming conventions. Variable names should NOT start with an upper case character( ie. BodyArea, TitleText). You got variable like (panel, list) correct so be consistent.
I have a panel which is divided by two parts with BoxLayout.X_AXIS:
public TabsPanel() {
setLayout(new BoxLayout(this, BoxLayout.X_AXIS));
add(createLeftPanel());
add(createRightPanel());
}
Each left and right panels have the following structure: an outer panel with BorderLayout, and an inner panel in BorderLayout.CENTER of the outer panel, which in its turn has BoxLayout.Y_AXIS and several components from top to bottom. The right panel has JTextArea with JScrollPane as one of its components:
protected JPanel createRightPanel() {
JPanel pane = new JPanel();
pane.setLayout(new BorderLayout());
JPanel panel = new JPanel();
panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
JTextArea label = createLabel();
JScrollPane scroll = new JScrollPane(label);
scroll.setMaximumSize(new Dimension(500, 200));
panel.add(Box.createRigidArea(new Dimension(0,106)));
panel.add(scroll);
JPanel panel_buttons = new JPanel();
panel_buttons.setLayout(new BoxLayout(panel_buttons, BoxLayout.LINE_AXIS));
panel_buttons.setAlignmentX(Component.CENTER_ALIGNMENT);
Font font_text = new Font("Georgia", Font.PLAIN, 20);
JButton[] buttons = new JButton[2];
buttons[0] = new JButton("Clear");
buttons[1] = new JButton("Exit");
for (int i = 0; i < buttons.length; i++) {
buttons[i].setMaximumSize(new Dimension(120, 40));
buttons[i].setFont(font_text);
panel_buttons.add(buttons[i]);
if (i == 0)
panel_buttons.add(Box.createRigidArea(new Dimension(40, 0)));
buttons[i].addActionListener(new TextActionListener(label));
}
panel.add(Box.createRigidArea(new Dimension(0,20)));
panel.add(panel_buttons);
pane.add(panel, BorderLayout.CENTER);
return pane;
}
When text goes beyond the borders, scroll bars appear and I can move them and read the text. Looks like everything is ok, but when I either click any place outside the scroll pane or even just move the pointer, the scroll pane moves to the left and grows down. It doesn't change its width, but it shifts to the left because the area between it and the right panel's borders increases. Accordingly, size of the left panel shrinks. When I clear the text area and again either click or move the pointer, it is back to its normal size.
What is the reason its height grows and its left and right margins increase? What am I doing wrong?
UPDATE. I've found the problem. The thing is that I didn't create JTextArea correctly. I initialized it without parameters:
JTextArea text = new JTextArea("Some initial text");
Now I have rewritten:
JTextArea text = new JTextArea(5,10);
It is now shifted to the left by about 5 mm and do not changes its height. Still not perfect, but looks like I am on the right track.
Thank you everybody for your help!
BoxLayout accepting Min, Max and PreferredSize override those methods for JPanel
use JSPlitPane, there you can to hide Divider
2 steps to correct:
Set the size of the JTextArea: JTextArea text = new JTextArea(row, col);
Still shifts to the left by the size of the vertical bar:
either add ChangeListener to adjust the size of the JScrollPane
scroll.getViewport().addChangeListener(new ChangeListener() {
#Override
public void stateChanged(ChangeEvent e) {
if (scroll.getVerticalScrollBar().isVisible())
scroll.setPreferredSize(480, 200);
}
}
});
or add scroll.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
I am making KenKen as my term project using java swing library. For alignment I have used gridbag and gridlayout, But now i want to add one more component of JPanel to the UI. These screenshots will make the problem more clear:
Now I select the grid cell to which i want to add respective candidates of in the left most panel.
It disturbs the adjacent alignments of the grid and panels.
Here are the panels with their respective layouts:
JPanel buttonPanel = new JPanel();
buttonPanel.setLayout(new GridLayout(1, 4, 5, 5));
buttonPanel.setPreferredSize(new Dimension(20,40));
buttonPanel.add(undoButton);
buttonPanel.add(redoButton);
buttonPanel.add(eraseButton);
buttonPanel.add(hintButton);
JPanel cellPanel = new JPanel();
cellPanel.setName("cellPanel");
cellPanel.setLayout(new GridLayout(pSize, pSize, 0, 0));
JPanel numPanel = new JPanel();
numPanel.setName("numPanel");
numPanel.setLayout(new GridLayout(1,1,5,5));
numPanel.setPreferredSize((new Dimension(50,60)));
JPanel candPanel = new JPanel();
candPanel.setName("candidatesPanel");
JLabel candidates = new JLabel("Candidates");
candidates.setFont(new Font("Courier New", Font.ITALIC, 14));
candidates.setForeground(Color.GRAY);
candPanel.setLayout(new GridLayout(0,1));
candPanel.add(candidates);
Then it all goes into the content panel:
content.add(buttonPanel, pos.nextCol().expandW());
content.add(candPanel, pos.nextRow());
content.add(new Gap(GAP) , pos.nextRow()); // Add a gap below
content.add(cellPanel, pos.nextCol());
content.add(numPanel,pos.nextCol().expandW());
The buttons are all generated on runtime, and they are added to the candPanel in an action listener.
You appear to be using a GridBagConstraints subclass of which I am unaware (variable pos), though I can guess its function from context.
Assuming your problem is that you want the candidates panel to be to the left of the cellPanel, and not above it, you need to swap the lines which add the candPanel and the new Gap(GAP) as follows:
content.add(buttonPanel, pos.nextCol().expandW());
content.add(new Gap(GAP), pos.nextRow()); // These two lines
content.add(candPanel, pos.nextRow()); // swapped over
content.add(cellPanel, pos.nextCol());
content.add(numPanel,pos.nextCol().expandW());