So I'm trying to create a series of radio buttons and check boxes that are displayed as follows:
Radio Button
Check Box
Radio Button
Check Box
Radio Button
However, I'm still in the learning process for java and I was wondering if anyone could solve this problem. At the moment the buttons and boxes are being displayed in the correct location, however the first radio button ("Times") is not being displayed for some reason. If you could perhaps describe the reason and a possible solution that'd be great.
Thanks
Updated Code:
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JTextField;
public class Question2 {
public static void main(String[] args) {
MyFrame f = new MyFrame("Font Chooser");
f.init();
}
}
class MyFrame extends JFrame {
MyFrame(String title) {
super(title);
}
private JPanel mainPanel;
private GridBagConstraints gbc = new GridBagConstraints();
private GridBagLayout gbLayout = new GridBagLayout();
void init() {
mainPanel = new JPanel();
mainPanel.setLayout(gbLayout);
mainPanel.setBorder(BorderFactory.createEmptyBorder(10, 20, 10, 20));
this.setContentPane(mainPanel);
gbc.gridx = 0;
gbc.gridy = 1;
JCheckBox cb = new JCheckBox("Bold");
gbLayout.setConstraints(cb, gbc);
mainPanel.add(cb);
gbc.gridy = 3;
gbLayout.setConstraints(cb, gbc);
cb = new JCheckBox("Italic");
mainPanel.add(cb);
gbc.gridx = 1;
gbc.gridy = 0;
JRadioButton rb = new JRadioButton("Times");
gbLayout.setConstraints(rb, gbc);
mainPanel.add(rb, gbc);
gbc.gridy = 2;
gbLayout.setConstraints(rb, gbc);
rb = new JRadioButton("Helvatica");
mainPanel.add(rb, gbc);
gbc.gridy = 4;
gbLayout.setConstraints(rb, gbc);
rb = new JRadioButton("Courier");
mainPanel.add(rb, gbc);
this.pack();
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
}
}
Here's the problem, you are saying each height is 3 high, but really each cell is 1.
cRadioButton.gridheight = 3; // change this to 1
Here's the full source, and I did make some of the suggested changes from the other answer because at some point you will want to do something different (different action listener implementation for each type of button).
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JTextField;
public class MyFrame1 extends JFrame {
MyFrame1(String title) {
super(title);
}
private JPanel mainPanel;
private GridBagConstraints gbc = new GridBagConstraints();
private GridBagLayout gbLayout = new GridBagLayout();
void init() {
mainPanel = new JPanel();
mainPanel.setLayout(gbLayout);
mainPanel.setBorder(BorderFactory.createEmptyBorder(10, 20, 10, 20));
this.setContentPane(mainPanel);
gbc.gridx = 0;
gbc.gridy = 1;
JCheckBox italic = new JCheckBox("Italic");
gbLayout.setConstraints(italic, gbc);
mainPanel.add(italic);
JCheckBox bold = new JCheckBox("Bold");
gbc.gridy = 3;
gbLayout.setConstraints(bold, gbc);
mainPanel.add(bold);
gbc.gridx = 1;
gbc.gridy = 0;
JRadioButton times = new JRadioButton("Times");
gbLayout.setConstraints(times, gbc);
mainPanel.add(times, gbc);
gbc.gridy = 2;
JRadioButton helv = new JRadioButton("Helvatica");
gbLayout.setConstraints(helv, gbc);
mainPanel.add(helv, gbc);
gbc.gridy = 4;
JRadioButton courier = new JRadioButton("Courier");
gbLayout.setConstraints(courier, gbc);
mainPanel.add(courier, gbc);
this.pack();
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
}
public static void main(String[] args) {
MyFrame1 f = new MyFrame1("Font Chooser");
f.init();
}
}
It seems like you keep reassigning the same object, which may be leading to your overlapping. Instead of
JRadioButton rb = new JRadioButton("Times");
//...
newPanel.add(rb);
rb = new JRadioButton("Helvatica");
//...
newPanel.add(rb);
//and so on
try something like
JRadioButton times = new JRadioButton("Times");
JRadioButton helva = new JRadioButton("Helvatica");
//...
newPanel.add(times);
newPanel.add(helva);
Related
I'm having some troubles working with GridBagConstraints, specifically with the insets property.
I have two components, and I want some custom margin between theme. Now, what I did was using constraints.insets in this way:
polinomioConstraints.insets = new Insets(5, 0, 15 ,0);
checkboxesPanel.add(infoAggiunte, polinomioConstraints);
checkBoxesPanel.add(anotherString, constraintWithoutInsets);
The problem is that for some reason a white block appears in the insets area, so between the two components, and I found no way to change that white color to the actual panel's background
Here's a GUI I just did for someone else. I added the yellow background.
I don't see white in between the Swing components.
Here's the complete runnable code.
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
public class MainRegistrationPage {
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
new MainRegistrationPage();
}
});
}
private JTextField firstname;
private JTextField lastname;
private JTextField email;
private JPasswordField password;
private JFrame frame;
public MainRegistrationPage() {
// create user interface
frame = new JFrame("Registration Page");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(createRegistrationPanel(), BorderLayout.CENTER);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
private JPanel createRegistrationPanel() {
JPanel mainPanel = new JPanel(new GridBagLayout());
mainPanel.setBorder(BorderFactory.createEmptyBorder(0, 5, 5, 5));
mainPanel.setBackground(Color.YELLOW);
Font font = new Font("Georgia", Font.PLAIN, 14);
GridBagConstraints gbc = new GridBagConstraints();
gbc.anchor = GridBagConstraints.LINE_START;
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.gridwidth = 1;
gbc.insets = new Insets(5, 5, 5, 5);
gbc.gridx = 0;
gbc.gridy = 0;
JLabel label = new JLabel("First Name:");
label.setFont(font);
mainPanel.add(label, gbc);
gbc.gridx++;
firstname = new JTextField(30);
firstname.setFont(font);
mainPanel.add(firstname, gbc);
gbc.gridx = 0;
gbc.gridy++;
label = new JLabel("Last Name:");
label.setFont(font);
mainPanel.add(label, gbc);
gbc.gridx++;
lastname = new JTextField(30);
lastname.setFont(font);
mainPanel.add(lastname, gbc);
gbc.gridx = 0;
gbc.gridy++;
label = new JLabel("Email:");
label.setFont(font);
mainPanel.add(label, gbc);
gbc.gridx++;
email = new JTextField(30);
email.setFont(font);
mainPanel.add(email, gbc);
gbc.gridx = 0;
gbc.gridy++;
label = new JLabel("Password:");
label.setFont(font);
mainPanel.add(label, gbc);
gbc.gridx++;
password = new JPasswordField(30);
password.setFont(font);
mainPanel.add(password, gbc);
gbc.gridwidth = 2;
gbc.gridx = 0;
gbc.gridy++;
JButton submit = new JButton("Submit");
submit.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
}
});
mainPanel.add(submit, gbc);
return mainPanel;
}
}
I wrote this simple class to test GridBagLayout.
My scope was to put some buttons in diagonal into panel, using grid bag layout and setting gridx and gridy, but I have a strange behaviour.
If I put gridwidth = 2 on button "2", button "3" will be drawn under button labeled "2".
My class is a simple demo, but I cant figure out what is wrong with it
Guess Why?
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class Test extends JFrame {
private JPanel panel;
private JButton b1;
private JButton b2;
private JButton b3;
private JButton b4;
private JButton b5;
private JLabel label1;
public Test() {
panel = new JPanel();
panel.setLayout(new GridBagLayout());
panel.setBackground(Color.DARK_GRAY);
GridBagConstraints gbc = new GridBagConstraints();
b1 = new JButton("1");
gbc.gridx = 0;
gbc.gridy = 0;
panel.add(b1, gbc);
b2 = new JButton("2");
gbc = new GridBagConstraints();
gbc.gridx = 1;
gbc.gridy = 1;
gbc.gridwidth = 2;
panel.add(b2, gbc);
gbc = new GridBagConstraints();
b3 = new JButton("3");
gbc.gridx = 2;
gbc.gridy = 2;
panel.add(b3, gbc);
gbc = new GridBagConstraints();
b4 = new JButton("4");
gbc.gridx = 3;
gbc.gridy = 3;
panel.add(b4, gbc);
gbc = new GridBagConstraints();
b5 = new JButton("5");
gbc.gridx = 1;
gbc.gridy = 4;
gbc.gridwidth = 3;
gbc.fill = GridBagConstraints.HORIZONTAL;
panel.add(b5, gbc);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setLocation(500, 400);
this.setSize(800, 300);
this.setTitle("Frame principale dell'applicazione");
this.setResizable(true);
getContentPane().add(panel, BorderLayout.CENTER);
this.setVisible(true);
}
public static void main(String[] args) {
Test mioFrame = new Test();
}
}
I created the following unusual GUI
Here's what I did.
You only need one instance of GridBagConstraints.
I made all of the gridwidth values 1.
I set gbc.fill to GridBagConstraints.NONE.
Here's the code I ran.
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
public class GridBagLayoutTestGUI extends JFrame {
private static final long serialVersionUID = 1L;
private JPanel panel;
private JButton b1;
private JButton b2;
private JButton b3;
private JButton b4;
private JButton b5;
private JLabel label1;
public GridBagLayoutTestGUI() {
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setTitle("Frame principale dell'applicazione");
this.panel = createMainPanel();
getContentPane().add(panel, BorderLayout.CENTER);
this.pack();
this.setLocationByPlatform(true);
this.setVisible(true);
}
private JPanel createMainPanel() {
JPanel panel = new JPanel();
panel.setLayout(new GridBagLayout());
panel.setBackground(Color.DARK_GRAY);
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridx = 0;
gbc.gridy = 0;
gbc.fill = GridBagConstraints.NONE;
b1 = new JButton("1");
panel.add(b1, gbc);
gbc.gridx = 1;
gbc.gridy = 1;
b2 = new JButton("2");
panel.add(b2, gbc);
gbc.gridx = 2;
gbc.gridy = 2;
b3 = new JButton("3");
panel.add(b3, gbc);
gbc.gridx = 3;
gbc.gridy = 3;
b4 = new JButton("4");
panel.add(b4, gbc);
gbc.gridx = 4;
gbc.gridy = 4;
b5 = new JButton("5");
panel.add(b5, gbc);
return panel;
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
new GridBagLayoutTestGUI();
}
});
}
}
My scope was to put some buttons in diagonal into panel, using gridbaglayout and setting gridx and gridy, but I have a strange behaviour. if I put gridwidth = 2 on button "2", button "3" will be drawn under button labeled "2" .
Button 1 is added to (0, 0) // ok
Button 3 is added to (2, 2) // doesn't work
Button 3 is not added to column 2 because column 1 doesn't have a width.
Yes you tried to add:
Button 2 to (1, 1)
but the problem is when you specify a gridwidth of 2, the GridBagLayout doesn't know what the width of column 1 should be so it uses 0.
Effectively column 2 becomes column 1 and Button 3 is painted in column 1.
In general, a column doesn't have a width unless you add a component to that column with a gridwidth = 1.
If you want to randomly add components to a grid then you need to configure a minium width for each column in the GridBagLayout. See: Creating a board game layout using JLayeredPane for an example of this approach.
I am having issues with GridBagLayout & GridBagConstraints in a GUI I am beginning to build. I have to pictures, one of the current state of the GUI, & one of the desired state of the GUI. I have been trying to reach the desired state but have been unable to :(. Here is the code, & I will also attach the 2 pictures I mentioned above. Moreover, there is an issue with the way that I am formatting the first or second checkbox, but I have been unable to figure out what the issue is.
Driver Class:
import javax.swing.SwingUtilities;
public class Driver {
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
new TheFrame();
}
});
}
}
JFrame Class:
import javax.swing.JFrame;
import javax.swing.JCheckBox;
import javax.swing.JLabel;
import java.awt.GridBagLayout;
import java.awt.GridBagConstraints;
import java.awt.Insets;
public class TheFrame extends JFrame {
//Declarations
private GridBagConstraints gbc;
private String myString;
private JLabel selectionLab;
private JCheckBox defconLevel1;
private JCheckBox defconLevel2;
private JCheckBox defconLevel3;
private JCheckBox defconLevel4;
private JCheckBox defconLevel5;
public TheFrame() {
super("DEFCON DEACTIVATOR");
this.setSize(500,500);
this.setVisible(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.getContentPane().setLayout(new GridBagLayout());
//Initialization
gbc = new GridBagConstraints();
selectionLab = new JLabel("Please Select DECON Level");
defconLevel1 = new JCheckBox("DEFCON 1");
defconLevel2 = new JCheckBox("DEFCON 2");
defconLevel3 = new JCheckBox("DEFCON 3");
defconLevel4 = new JCheckBox("DEFCON 4");
defconLevel5 = new JCheckBox("DEFCON 5");
//Configuration
//Add to contentPane
//ROW 1
gbc.gridx = 0;
gbc.gridy = 0;
gbc.anchor = gbc.NORTH;
gbc.weighty = 1;
gbc.insets = new Insets(0,0,0,0);
this.getContentPane().add(selectionLab);
//ROW 2
gbc.gridx = 0;
gbc.gridy = 1;
gbc.anchor = gbc.NORTH;
gbc.weighty= 1;
gbc.insets = new Insets(0,0,0,0);
this.getContentPane().add(defconLevel1,gbc);
gbc.gridx = 1;
gbc.gridy = 1;
gbc.anchor = gbc.NORTH;
gbc.weighty= 1;
gbc.insets = new Insets(0,0,0,0);
this.getContentPane().add(defconLevel2,gbc);
gbc.gridx = 2;
gbc.gridy = 1;
gbc.anchor = gbc.NORTH;
gbc.weighty= 1;
gbc.insets = new Insets(0,0,0,0);
this.getContentPane().add(defconLevel3,gbc);
gbc.gridx = 3;
gbc.gridy = 1;
gbc.anchor = gbc.NORTH;
gbc.weighty= 1;
gbc.insets = new Insets(0,0,0,0);
this.getContentPane().add(defconLevel4,gbc);
gbc.gridx = 4;
gbc.gridy = 1;
gbc.anchor = gbc.NORTH;
gbc.weighty= 1;
gbc.insets = new Insets(0,0,0,0);
this.getContentPane().add(defconLevel5,gbc);
}
}
Updated Code:
Driver Class
import javax.swing.SwingUtilities;
public class Driver {
//Declarations
private static SelectionPanel selectionPanel;
private static HeaderPanel headerPanel;
private static TheFrame frame = new TheFrame(selectionPanel,headerPanel);
// public Driver() {
//
// }
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
new Driver();
}
});
}
}
TheFrame Class
import javax.swing.JFrame;
import java.awt.GridBagLayout;
import java.awt.GridBagConstraints;
import java.awt.Insets;
public class TheFrame extends JFrame {
//Declarations
private GridBagConstraints gbc;
private SelectionPanel selectionPanel;
private HeaderPanel headerPanel;
public TheFrame(SelectionPanel selectionPanel, HeaderPanel headerPanel) {
super("DEFCON DEACTIVATOR");
this.selectionPanel = selectionPanel;
this.headerPanel = headerPanel;
//Initialization
gbc = new GridBagConstraints();
selectionPanel = new SelectionPanel();
headerPanel = new HeaderPanel();
this.getContentPane().setLayout(new GridBagLayout());
//Configuration
//Add to contentPane
gbc.anchor = gbc.NORTH; //Content-Pane GLOBAL
gbc.insets = new Insets(0,0,0,0); //Content-Pane GLOBAL
gbc.weightx = 0; //Content-Pane GLOBAL
gbc.weighty = 0.05;
gbc.gridx = 0;
gbc.gridy = 0;
this.getContentPane().add(headerPanel,gbc);
gbc.weighty = 1;
gbc.gridx = 0;
gbc.gridy = 1;
this.getContentPane().add(selectionPanel,gbc);
//Finalize JFrame Last Steps Configurations
this.setSize(500,500);
this.setVisible(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
SelectionPanel Class
import java.awt.Insets;
import javax.swing.JCheckBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
public class SelectionPanel extends JPanel {
//Declarations
private JCheckBox defconLevel1;
private JCheckBox defconLevel2;
private JCheckBox defconLevel3;
private JCheckBox defconLevel4;
private JCheckBox defconLevel5;
private GridBagConstraints gbc;
public SelectionPanel() {
//Initializations
defconLevel1 = new JCheckBox("DEFCON 1");
defconLevel2 = new JCheckBox("DEFCON 2");
defconLevel3 = new JCheckBox("DEFCON 3");
defconLevel4 = new JCheckBox("DEFCON 4");
defconLevel5 = new JCheckBox("DEFCON 5");
gbc = new GridBagConstraints();
//Configuration
this.setLayout(new GridBagLayout());
//Add
//ROW 1
gbc.insets = new Insets(0,0,0,0); //Content-Pane Global
//gbc.anchor = gbc.EAST; //Makes Elements chain-follow each other
gbc.gridy = 0;
gbc.gridx = 0;
this.add(defconLevel1,gbc);
gbc.gridx = 1;
this.add(defconLevel2,gbc);
gbc.gridx = 2;
this.add(defconLevel3,gbc);
gbc.gridx = 3;
this.add(defconLevel4,gbc);
gbc.gridx = 4;
this.add(defconLevel5,gbc);
}
}
HeaderPanel Class
import javax.swing.JPanel;
import javax.swing.JLabel;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
public class HeaderPanel extends JPanel {
private JLabel headerLab;
private GridBagConstraints gbc;
public HeaderPanel() {
//Initialize
headerLab = new JLabel("PLEASE SELECT DEFCON LEVEL");
gbc = new GridBagConstraints();
//Configure
this.setLayout(new GridBagLayout());
//Add
gbc.gridx = 0;
gbc.gridy = 0;
this.add(headerLab,gbc);
}
}
Present Picture:
Wished Design:
Updated Image:
The constraint for the label also needs:
gbc.gridwitdh = 5;
This will allow the label to take up the same horizontal space as the 5 checkboxes, allowing each check box to be displayed in its own column.
You will then need to reset the gridwidth to 1 before adding the other components.
Another option might be to use a Titled Border on your panel. Then you can just use a FlowLayout for adding all the check boxes. This is an easier solution since you don't need to worry about all the GridBagConstraints.
Edit:
Read the section from the Swing tutorial on How to Use GridBagLayout for information about all the constraints.
The first thing you need to do is actually use the constraints for the label otherwise setting the gridwidth won't do anything as the default constrainsts will be used:
//this.getContentPane().add(selectionLab);
add(selectionLab, gbc);
It still won't look correct because you will then need to understand the proper values to be used with the following constraints:
weighty
anchor
weightx
I'll let you play with the constraints one at a time to see what happens as you change the constraint. The tutorial link will help with the different values.
I have a problem with GridbagLayout; I've 5 buttons and I want to have them in this way:
I've already tried different approaches but no one works in the correct way.
For example:
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import javax.swing.Box;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
public class TestGridBagLayout {
protected void initUI() {
JFrame frame = new JFrame("Test");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel southPanel = new JPanel(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridwidth = 2;
gbc.gridy = 0;
JButton enterRoom = new JButton("Enter room");
JButton exitRoom = new JButton("Exit room");
JButton login = new JButton("Login");
JButton logout = new JButton("Logout");
JButton whoIsIn = new JButton("Who is in");
gbc.gridx = 1;
southPanel.add(enterRoom, gbc);
gbc.gridx = 5;
southPanel.add(exitRoom, gbc);
gbc.gridy = 1;
gbc.gridx = 0;
southPanel.add(login, gbc);
gbc.gridx = 3;
southPanel.add(logout, gbc);
gbc.gridx = 6;
southPanel.add(whoIsIn, gbc);
frame.add(southPanel);
frame.pack();
frame.setVisible(true);
}
public static void main(String[] args) throws ClassNotFoundException, InstantiationException, IllegalAccessException,
UnsupportedLookAndFeelException {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
new TestGridBagLayout().initUI();
}
});
}
}
Appears:
I'm not interested in other approaches (such as GridLayout), I'd like to know what I'm missing.
GridBagLayout can be a strange animal in some cases. But anyway, gridwidth is something that works, only if there is an actual component that requires some width within the "spanned" column (for example, if you say gridx=0 and gridwidth=2, column 0 has a component and the "spanned" column is column 1).
In your case, column 2, 4 & 7 have no components, so their width is set to 0. Additionnaly, column 5 also gets a width of 0, because column 6 provides enough witdth to the exit room button, so in the end you get the result you see.
Now, not sure of the kind of layout you are trying to achieve (I saw your screenshot, but how should it behave when the panel collapses/expands in width?). So find below, an example that comes a bit closer to what you describe (although I don't find it very nice)
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
public class TestGridBagLayout2 {
protected void initUI() {
JFrame frame = new JFrame("Test");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel southPanel = new JPanel(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridy = 0;
JButton enterRoom = new JButton("Enter room");
JButton exitRoom = new JButton("Exit room");
JButton login = new JButton("Login");
JButton logout = new JButton("Logout");
JButton whoIsIn = new JButton("Who is in");
gbc.gridx = 0;
gbc.weightx = 1.0;
gbc.anchor = GridBagConstraints.EAST;
southPanel.add(enterRoom, gbc);
gbc.anchor = GridBagConstraints.WEST;
gbc.gridx = 2;
southPanel.add(exitRoom, gbc);
gbc.gridy = 1;
gbc.gridx = 0;
southPanel.add(login, gbc);
gbc.weightx = 0;
gbc.gridx = 1;
southPanel.add(logout, gbc);
gbc.weightx = 1.0;
gbc.anchor = GridBagConstraints.EAST;
gbc.gridx = 2;
southPanel.add(whoIsIn, gbc);
frame.add(southPanel);
frame.pack();
frame.setSize(frame.getWidth() * 4 / 3, frame.getHeight());
frame.setMinimumSize(frame.getSize());
frame.setVisible(true);
}
public static void main(String[] args) throws ClassNotFoundException, InstantiationException, IllegalAccessException,
UnsupportedLookAndFeelException {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
new TestGridBagLayout().initUI();
}
});
}
}
GridbagLayout seems to require a row where a component occupies all of the columns in the row. See: Why does this GridBagLayout not appear as planned? for the basis of this solution.
Note the that horizontal strut size was choosen to be half the size of the "Logout" button so that two cells with span the width of the logout button to give the centering of components that you desire.
import java.awt.*;
import javax.swing.*;
public class SSCCE extends JPanel
{
public SSCCE()
{
JButton enterRoom = new JButton("Enter room");
JButton exitRoom = new JButton("Exit room");
JButton login = new JButton("Login");
JButton logout = new JButton("Logout");
JButton whoIsIn = new JButton("Who is in");
setLayout( new GridBagLayout() );
GridBagConstraints gbc = new GridBagConstraints();
gbc.insets = new Insets(5, 0,5, 0);
gbc.gridwidth = 2;
gbc.gridx = 1;
gbc.gridy = 0;
add(enterRoom, gbc);
gbc.gridx = 5;
gbc.gridy = 0;
add(exitRoom, gbc);
gbc.gridx = 0;
gbc.gridy = 1;
add(login, gbc);
gbc.gridx = 3;
gbc.gridy = 1;
add(logout, gbc);
gbc.gridx = 6;
gbc.gridy = 1;
add(whoIsIn, gbc);
// Add dummy components so every cell has a component.
gbc.insets = new Insets(0, 0, 0, 0);
gbc.gridwidth = 1;
gbc.gridy = 2;
int strutWidth = logout.getPreferredSize().width / 2;
for (int i = 0; i < 8; i++)
{
gbc.gridx = i;
add(Box.createHorizontalStrut(strutWidth), gbc);
}
}
private static void createAndShowGUI()
{
JFrame frame = new JFrame("SSCCE");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new SSCCE(), BorderLayout.NORTH);
frame.setLocationByPlatform( true );
frame.pack();
frame.setVisible( true );
}
public static void main(String[] args)
{
EventQueue.invokeLater(new Runnable()
{
public void run()
{
createAndShowGUI();
}
});
}
}
Edit: Each time I try adding the gui tag, it switches to user-interface. Someone mind explaining/fixing that?
I want the client to be resizable. I want the JSeparator to fill the frame's width when resizing, but I want the JLabels to stay next to the fields.
It starts out like this, which the JLabels are too far apart from the fields as it is:
When I resize it horizontally, this is the result:
Which is obviously way too far apart. The code that I use to set up these components are:
public class LoginPanel extends JPanel {
private JTextField userfield = new JTextField(10);
private JPasswordField passfield = new JPasswordField(10);
private JButton login = new JButton("Login");
private JButton create = new JButton("Create Account");
public LoginPanel() {
setLayout(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.anchor = GridBagConstraints.CENTER;
gbc.weightx = 1;
gbc.gridx = 2;
JLabel label = new JLabel("Username: ");
add(label, gbc);
gbc.gridx = 3;
gbc.gridwidth = 2;
add(userfield, gbc);
gbc.gridy = 1;
add(passfield, gbc);
gbc.gridx = 2;
label = new JLabel("Password: ");
add(label, gbc);
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.gridy = 2;
gbc.gridx = 1;
gbc.gridwidth = 5;
add(new JSeparator(JSeparator.HORIZONTAL), gbc);
}
}
(Had to cut out a few things, please tell me if I'm missing anything)
I've tried anchoring, but I'm still not 100% familiar with GridBagLayout (and the constraints) yet, so I'm not sure if I'm my attempts are in the right direction.
How would I prevent the Username: and Password: labels from moving away from my fields, with still being able to resize?
Also, I want to use GridBagLayout. There is still a lot of things I need to add, and I do not want to use a simple layout due to the fact that I'm going to need flexibility.
Make use of GridBagConstraints#anchor
import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JSeparator;
import javax.swing.JTextField;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
public class LogInTest {
public static void main(String[] args) {
new LogInTest();
}
public LogInTest() {
EventQueue.invokeLater(new Runnable() {
#Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
}
JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new BorderLayout());
frame.add(new LoginPanel());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public class LoginPanel extends JPanel {
private JTextField userfield = new JTextField(10);
private JPasswordField passfield = new JPasswordField(10);
private JButton login = new JButton("Login");
private JButton create = new JButton("Create Account");
public LoginPanel() {
setLayout(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.anchor = GridBagConstraints.CENTER;
gbc.weightx = 1;
gbc.gridx = 2;
gbc.anchor = GridBagConstraints.EAST;
JLabel label = new JLabel("Username: ");
add(label, gbc);
gbc.anchor = GridBagConstraints.WEST;
gbc.gridx = 3;
gbc.gridwidth = 2;
add(userfield, gbc);
gbc.gridy = 1;
add(passfield, gbc);
gbc.anchor = GridBagConstraints.EAST;
gbc.gridx = 2;
label = new JLabel("Password: ");
add(label, gbc);
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.gridy = 2;
gbc.gridx = 1;
gbc.gridwidth = 5;
add(new JSeparator(JSeparator.HORIZONTAL), gbc);
}
}
}
You may also want to consider making use of compound layouts, that is, separate each area into it's own container and focus on the individual layout needs for each section and then build them all up into a single layout