Reputation: 301
I'm new and learning JFrame currently and I'm running into a problem with the alignment of my ComboBox, checkboxes and buttons. The listeners are not hooked up yet and this is just a test to see if it's aligned properly. My question is why doesn't the alignment function seem to be working? I want the combo box to be at the top the checkboxes in the middle and the buttons on the bottom.
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.event.ItemListener;
import java.awt.event.ItemEvent;
import javax.swing.JFrame;
import javax.swing.JTextField;
import javax.swing.JCheckBox;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.SwingConstants;
public class CheckBoxFrame extends JFrame
{
private final JComboBox<String> colorComboBox;
private final JCheckBox backgroundColorCheckBox;
private final JCheckBox foregroundColorCheckBox;
private final JButton okButton;
private final JButton cancelButton;
private static final String[] color =
{"Red", "Blue", "Green", "Purple"};
public CheckBoxFrame()
{
super("ColorSelect");
setLayout(new FlowLayout());
colorComboBox = new JComboBox<String>(color);
colorComboBox.setAlignmentX(SwingConstants.TOP);
add(colorComboBox);
backgroundColorCheckBox = new JCheckBox("Background");
foregroundColorCheckBox = new JCheckBox("Foreground");
foregroundColorCheckBox.setVerticalAlignment(SwingConstants.CENTER);
backgroundColorCheckBox.setVerticalAlignment(SwingConstants.CENTER);
add(backgroundColorCheckBox); // add bold checkbox to JFrame
add(foregroundColorCheckBox); // add italic checkbox to JFrame
okButton = new JButton("Ok");
cancelButton = new JButton("Cancel");
okButton.setVerticalAlignment(SwingConstants.BOTTOM);
cancelButton.setVerticalAlignment(SwingConstants.BOTTOM);
add(okButton);
add(cancelButton);
// register listeners for JCheckBoxes
CheckBoxHandler handler = new CheckBoxHandler();
backgroundColorCheckBox.addItemListener(handler);
foregroundColorCheckBox.addItemListener(handler);
}
private class CheckBoxHandler implements ItemListener
{
// respond to checkbox events
@Override
public void itemStateChanged(ItemEvent event)
{
}
}
} // end class CheckBoxFrame
Upvotes: 0
Views: 689
Reputation: 63
Refer to the official docs on Layout Managers for Java Swing: https://docs.oracle.com/javase/tutorial/uiswing/layout/visual.html.
FlowLayout
is one of the more basic Layout Managers so I would suggest looking into the GridBagLayout
.
Try this:
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.event.ItemListener;
import java.awt.event.ItemEvent;
import javax.swing.JFrame;
import javax.swing.JTextField;
import javax.swing.JCheckBox;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.SwingConstants;
public class CheckBoxFrame extends JFrame
{
private final JComboBox<String> colorComboBox;
private final JCheckBox backgroundColorCheckBox;
private final JCheckBox foregroundColorCheckBox;
private final JButton okButton;
private final JButton cancelButton;
private static final String[] color = {"Red", "Blue", "Green", "Purple"};
public CheckBoxFrame()
{
super("ColorSelect");
setLayout(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
colorComboBox = new JComboBox<String>(color);
c.fill = GridBagConstraints.HORIZONTAL;
c.gridx = 0;
c.gridy = 0;
c.gridwidth = 2;
add(colorComboBox, c);
backgroundColorCheckBox = new JCheckBox("Background");
c.fill = GridBagConstraints.HORIZONTAL;
c.gridx = 0;
c.gridy = 1;
c.gridwidth = 1;
add(backgroundColorCheckBox, c); // add bold checkbox to JFrame
foregroundColorCheckBox = new JCheckBox("Foreground");
c.fill = GridBagConstraints.HORIZONTAL;
c.gridx = 1;
c.gridy = 1;
c.gridwidth = 1;
add(foregroundColorCheckBox, c); // add italic checkbox to JFrame
okButton = new JButton("Ok");
c.fill = GridBagConstraints.HORIZONTAL;
c.gridx = 0;
c.gridy = 2;
c.gridwidth = 1;
add(okButton, c);
cancelButton = new JButton("Cancel");
c.fill = GridBagConstraints.HORIZONTAL;
c.gridx = 1;
c.gridy = 2;
c.gridwidth = 1;
add(cancelButton, c);
// register listeners for JCheckBoxes
CheckBoxHandler handler = new CheckBoxHandler();
backgroundColorCheckBox.addItemListener(handler);
foregroundColorCheckBox.addItemListener(handler);
}
private class CheckBoxHandler implements ItemListener
{
// respond to checkbox events
@Override
public void itemStateChanged(ItemEvent event)
{
}
}
} // end class CheckBoxFrame
Upvotes: 1
Reputation: 347204
From the JavaDocs for setVerticalAlignment
Sets the vertical alignment of the icon and text.
This changes the way in which the icon
and text
properties are laid out WITHIN the context of the component.
setVerticalAlignment
won't affect how the component is laid out in the container.
To change the way components are laid out in the container, you will need to change the layout manager, see Lesson: Laying Out Components Within a Container
Based on your requirements, there are number of ways you might achieve it.
You could use a GridLayout
, which all the components for an individual row wrapped in their own JPanel
and this panel added to the primary container.
You could also use a GridBagLayout
, which, while more complex, is also more flexible
Upvotes: 1