I've recently been working with the layout manager GridBagLayout and have been running into trouble when it comes to the grid system it uses.
What I want to do is have the JButton I create here:
JButton myButton = new JButton("Some text");
gc.gridx = 2;
gc.gridy = 4;
gc.weightx = 0.5;
gc.weighty = 0.5;
jp.add(myButton, gc);
To be 2 columns over, and 4 rows down given the code:
gc.gridx = 2
gc.gridy = 4
I define the number of columns and rows in the component (JPanel) here:
GridBagConstraints gc = new GridBagConstraints();
gc.gridwidth = 2;
gc.gridheight = 10;
However, what I am getting is this:
The components are overlapping despite being in clearly different sections. Any help is much appreciated. Here is the full code:
package com.UI;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
public class UserInterface{
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
JFrame f = createFrame();
JPanel jp = createPanel();
addLabelAndText(jp);
f.add(jp, BorderLayout.WEST);
}
});
}
static JFrame createFrame() {
JFrame f = new JFrame();
f.setLayout(new BorderLayout());
f.setSize(new Dimension(600, 600));
f.setVisible(true);
f.setLocationRelativeTo(null);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
return f;
}
static JPanel createPanel() {
JPanel jp = new JPanel();
jp.setBackground(Color.gray);
jp.setLayout(new GridBagLayout());
Dimension size = jp.getPreferredSize();
size.width = 200;
jp.setPreferredSize(size);
jp.setBorder(BorderFactory.createEtchedBorder());
return jp;
}
static void addLabelAndText(JPanel jp) {//How to call a type represented by the passed String? JLabel, for example?
GridBagConstraints gc = new GridBagConstraints();
gc.gridwidth = 2;
gc.gridheight = 10;
JTextField jtb = new JTextField(5);
JLabel jl = new JLabel("Enter input here: ");
gc.gridx = 2;
gc.gridy = 0;
gc.weightx = 0.5;
gc.weighty = 0.5;
gc.anchor = GridBagConstraints.NORTHEAST;
jp.add(jtb, gc);
gc.gridx = 1;
gc.gridy = 2;
gc.weightx = 0.5;
gc.weighty = 0.5;
gc.anchor = GridBagConstraints.NORTHWEST;
gc.anchor = GridBagConstraints.FIRST_LINE_END;
jp.add(jl, gc);
JButton myButton = new JButton("Some text");
gc.gridx = 2;
gc.gridy = 4;
gc.weightx = 0.5;
gc.weighty = 0.5;
jp.add(myButton, gc);
}
}
Thanks for any help
Related
I want to build this same frame in the image with Layout Manager
What I already did:
Used BorderLayout to add all three JLabel to BorderLayout.WEST(Used GridLayout(3,1).
Used BorderLayout to add all three JTextField to BorderLayout.CENTER(used GridLayout(3,1).
I build this frame by manually adding components using the setBounds method.
Here's my implementation of your screen capture.
import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.WindowConstants;
public class SmplForm implements Runnable {
#Override // java.lang.Runnable
public void run() {
createAndDisplayGui();
}
private void createAndDisplayGui() {
JFrame frame = new JFrame("Nonce creator");
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
frame.add(createForm(), BorderLayout.CENTER);
frame.add(createButtonsPanel(), BorderLayout.PAGE_END);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
private JPanel createButtonsPanel() {
JPanel buttonsPanel = new JPanel();
JButton proceedButton = new JButton("Proceed");
buttonsPanel.add(proceedButton);
return buttonsPanel;
}
private JPanel createForm() {
JPanel form = new JPanel(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.anchor = GridBagConstraints.LINE_START;
gbc.gridx = 0;
gbc.gridy = 0;
gbc.insets.bottom = 5;
gbc.insets.left = 5;
gbc.insets.right = 5;
gbc.insets.top = 5;
JLabel transactionLabel = new JLabel("Transaction");
form.add(transactionLabel, gbc);
gbc.gridx = 1;
JTextField transactionTextField = new JTextField(20);
form.add(transactionTextField, gbc);
gbc.gridx = 0;
gbc.gridy = 1;
JLabel nonceLabel = new JLabel("Nonce");
form.add(nonceLabel, gbc);
gbc.gridx = 1;
JTextField nonceTextField = new JTextField(20);
form.add(nonceTextField, gbc);
gbc.gridx = 2;
JCheckBox autoCheckBox = new JCheckBox("Auto");
form.add(autoCheckBox, gbc);
gbc.gridx = 0;
gbc.gridy = 2;
JLabel hashLabel = new JLabel("Hash");
form.add(hashLabel, gbc);
gbc.gridx = 1;
JTextField hashTextField = new JTextField(20);
hashTextField.setText("8350e5a3e24c153df2275c9f80692773");
hashTextField.setEnabled(false);
form.add(hashTextField, gbc);
return form;
}
public static void main(String[] args) {
EventQueue.invokeLater(new SmplForm());
}
}
This GUI has three slight tweaks, the first two as seen in the example by Abra:
The button is center aligned.
The label for the check box is to the right of the check.
The labels are right aligned.
import java.awt.*;
import javax.swing.*;
import javax.swing.border.EmptyBorder;
public class NonceCreator {
private JComponent ui = null;
NonceCreator() {
initUI();
}
public void initUI() {
if (ui!=null) return;
ui = new JPanel(new GridBagLayout());
ui.setBorder(new EmptyBorder(20,30,20,30));
GridBagConstraints gbc = new GridBagConstraints();
gbc.insets = new Insets(5,5,5,5);
// add the labels
gbc.anchor = GridBagConstraints.EAST;
ui.add(new JLabel("Transaction"), gbc);
gbc.gridy = 1;
ui.add(new JLabel("Nonce"), gbc);
gbc.gridy = 2;
ui.add(new JLabel("Hash"), gbc);
gbc.anchor = GridBagConstraints.WEST;
// add the text fields
gbc.gridx = 1;
gbc.gridy = 0;
gbc.gridwidth = 2;
ui.add(new JTextField(30), gbc);
gbc.gridy = 2;
JTextField hashField = new JTextField(
"8350e5a3e24c153df2275c9f80692773", 30);
hashField.setEditable(false);
ui.add(hashField, gbc);
gbc.gridy = 1;
gbc.gridwidth = 1;
ui.add(new JTextField(20), gbc);
// add the check box
gbc.gridx = 2;
ui.add(new JCheckBox("Auto"), gbc);
// add the button
gbc.gridx = 0;
gbc.gridy = 3;
gbc.gridwidth = 3;
gbc.anchor = GridBagConstraints.CENTER;
ui.add(new JButton("Proceed"), gbc);
}
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) {
}
NonceCreator o = new NonceCreator();
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 don't use a visual GUI Builder. I think you can use GroupLayout or GridBagLayout for a flexible design. Also you can use a design tool like Adobe XD, and convert this to Java. That will be easier.
Look at;
https://docs.oracle.com/javase/tutorial/uiswing/layout/group.html
https://docs.oracle.com/javase/tutorial/uiswing/layout/gridbag.html
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 am trying to build a GUI for a little Java game using a GridBagLayout. Finally it should look like this:
This is my code:
private GridBagConstraints c;
...
c.gridx = 1;
c.gridy = 0;
c.weightx = 0;
add(playButton, c);
c.gridx = 1;
c.gridy = 1;
c.weightx = 0;
add(optionsButton, c);
c.gridx = 1;
c.gridy = 2;
c.weightx = 0;
add(manualButton, c);
c.gridx = 1;
c.gridy = 3;
c.weightx = 0;
add(exitButton, c);
c.gridx = 0;
c.gridy = 4;
c.weightx = 1;
c.anchor = GridBagConstraints.SOUTHWEST;
add(creditsButton, c);
c.gridx = 2;
c.gridy = 4;
c.weightx = 1;
c.anchor = GridBagConstraints.SOUTHEAST;
add(legalNoticeButton, c);
At the moment it looks like this:
My question is who I can set the two buttons to the bottom without setting the four other bottoms to the top?
Something like this is more or less #Gilbert Le Blanc explained (with minor differences):
Basically use another panel to move your two buttons to the south and spread them to WEST and EAST:
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import javax.swing.JButton;
import javax.swing.JComponent;
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");
final JPanel centerPanel = new JPanel();
centerPanel.setLayout(new GridBagLayout());
JButton playButton = new JButton("Play");
JButton optionsButton = new JButton("Options");
JButton manualButton = new JButton("Manual");
JButton exitButton = new JButton("Exit");
JButton creditsButton = new JButton("Credits");
JButton legalNoticeButton = new JButton("Legal");
GridBagConstraints c = new GridBagConstraints();
c.gridwidth = GridBagConstraints.REMAINDER;
centerPanel.add(playButton, c);
centerPanel.add(optionsButton, c);
centerPanel.add(manualButton, c);
centerPanel.add(exitButton, c);
JPanel bottomPanel = new JPanel(new BorderLayout());
bottomPanel.add(creditsButton, BorderLayout.WEST);
// Filler component that avoids having the bottom panel too small
bottomPanel.add(new JComponent() {
#Override
public Dimension getPreferredSize() {
return new Dimension(centerPanel.getPreferredSize().width, 0);
}
});
bottomPanel.add(legalNoticeButton, BorderLayout.EAST);
frame.add(centerPanel);
frame.add(bottomPanel, BorderLayout.SOUTH);
frame.pack();
frame.setMinimumSize(frame.getPreferredSize());
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();
}
});
}
}
Here is my code. When I resize the program the other layouts, it works fine except for the CheckPanel (with GridBagLayout); somehow it became bigger. How can I handle this?
Advance Thanks.
JPanel CheckPanel = new JPanel();
CheckPanel.setBackground(Color.WHITE);
GridBagConstraints gbc_CheckPanel = new GridBagConstraints();
gbc_CheckPanel.gridwidth = 2;
gbc_CheckPanel.fill = GridBagConstraints.BOTH;
gbc_CheckPanel.gridx = 0;
gbc_CheckPanel.gridy = 1;
CenterPanel.add(CheckPanel, gbc_CheckPanel);
CheckPanel.setLayout(new GridLayout(0, 6, 0, 0));
The constraint
gbc_CheckPanel.fill = GridBagConstraints.BOTH;
tells the GridBagLayout to let this component use any available extra space (also depending on its preferred- and maximum size). Using GridBagConstraints.NONE or GridBagConstraints.VERTICAL could solve this, but you still have to decide which component will receive the extra space.
EDIT: Based on the comments, it is still not clear how the other components are inserted, and how the distribution of extra space is specified. In this example, the weighty fields are used to say that the table should receive all extra space. Maybe it helps to identify differences to the original program:
import java.awt.Color;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
public class CheckboxConstraints
{
public static void main(String[] args)
{
SwingUtilities.invokeLater(new Runnable()
{
#Override
public void run()
{
createAndShowGUI();
}
});
}
private static void createAndShowGUI()
{
JFrame f = new JFrame();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel centerPanel = new JPanel(new GridBagLayout());
centerPanel.setBackground(Color.RED);
fill(centerPanel);
f.getContentPane().setLayout(new GridLayout(1,1));
f.getContentPane().add(centerPanel);
f.setSize(500, 300);
f.setLocationRelativeTo(null);
f.setVisible(true);
}
private static void fill(JPanel centerPanel)
{
addSearchBar(centerPanel);
addCheckBoxPanel(centerPanel);
addTable(centerPanel);
}
private static void addSearchBar(JPanel centerPanel)
{
GridBagConstraints gbc = null;
gbc = new GridBagConstraints();
gbc.gridwidth = 1;
gbc.fill = GridBagConstraints.BOTH;
gbc.gridx = 0;
gbc.gridy = 0;
gbc.weightx = 0;
gbc.weighty = 0;
centerPanel.add(new JButton("Search"), gbc);
gbc = new GridBagConstraints();
gbc.gridwidth = 1;
gbc.fill = GridBagConstraints.BOTH;
gbc.gridx = 1;
gbc.gridy = 0;
gbc.weightx = 1;
gbc.weighty = 0;
centerPanel.add(new JTextField(), gbc);
}
private static void addCheckBoxPanel(JPanel centerPanel)
{
JPanel checkPanel = new JPanel();
checkPanel.setBackground(Color.WHITE);
GridBagConstraints gbc_CheckPanel = new GridBagConstraints();
gbc_CheckPanel.gridwidth = 2;
gbc_CheckPanel.fill = GridBagConstraints.BOTH;
gbc_CheckPanel.gridx = 0;
gbc_CheckPanel.gridy = 1;
gbc_CheckPanel.weighty = 0;
centerPanel.add(checkPanel, gbc_CheckPanel);
checkPanel.setLayout(new GridLayout(0, 6, 0, 0));
checkPanel.add(new JCheckBox("C0"));
checkPanel.add(new JCheckBox("C1"));
checkPanel.add(new JCheckBox("C2"));
checkPanel.add(new JCheckBox("C3"));
checkPanel.add(new JCheckBox("C4"));
checkPanel.add(new JCheckBox("C5"));
}
private static void addTable(JPanel centerPanel)
{
JTable table = new JTable(new Object[][] {
{"C00", "C01" },
{"C00", "C01" },
}, new Object[]{ "H0", "H1" });
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridwidth = 2;
gbc.fill = GridBagConstraints.BOTH;
gbc.gridx = 0;
gbc.gridy = 2;
gbc.weighty = 1.0;
JScrollPane scrollPane = new JScrollPane(table);
centerPanel.add(scrollPane, gbc);
}
}
Please try:
gbc_CheckPanel.fill =GridBagConstraints.HORIZONTAL
and to adjust the space vertically
gbc_CheckPanel.ipady = 20; //Not sure this works in your case but this should fix the resizing issue
Another solution is to add a new JPanel with BorderLayout and insert the panel with JCheckBoxes using the constraint BorderLayout.NORTH and the table with constraint BorderLayout.CENTRE
Learing GridBagLayout, The issue here is, the name label and combox don't show up on the top of the panel, but I have set its anchor to NORTH. Why ?
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.WindowConstants;
public class Test2 {
public Test2() {
JFrame frame = new JFrame();
frame.setTitle("test");
frame.getContentPane().setLayout(new GridLayout(1,2));
frame.setSize(800, 600);
JPanel panel1 = new JPanel();
panel1.setLayout(new GridBagLayout());
JLabel label = new JLabel("name");
GridBagConstraints gridBagConstraints = new GridBagConstraints();
gridBagConstraints.anchor = GridBagConstraints.NORTH;
gridBagConstraints.weightx = 0.0;
gridBagConstraints.weighty = 0.0;
gridBagConstraints.gridx = 0;
gridBagConstraints.gridy = 0;
panel1.add(label, gridBagConstraints);
String[] petStrings = { "Bird", "Cat", "Dog", "Rabbit", "Pig" };
JComboBox petList = new JComboBox(petStrings);
gridBagConstraints = new GridBagConstraints();
gridBagConstraints.anchor = GridBagConstraints.NORTH;
gridBagConstraints.fill = GridBagConstraints.HORIZONTAL;
gridBagConstraints.weightx = 1.0;
gridBagConstraints.weighty = 0.0;
gridBagConstraints.gridx = 1;
gridBagConstraints.gridy = 0;
panel1.add(petList, gridBagConstraints);
frame.getContentPane().add(panel1);
frame.getContentPane().add(new JPanel());
frame.setVisible(true);
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new Test2();
}
}
You have to change
gridBagConstraints.weighty = 0.0;
to
gridBagConstraints.weighty = 1.0;
otherwise the area reserved for the component is slimmed to the size of the component, and it doesn't matter in which direction you "anchor" the component.
The result after changing the weighty is the following: