I am working on a project where I have a requirement that when a button is clicked, an object that is in a GridBagLayer moves to a different spot. Here is my code so far:
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
public class main extends JFrame{
private JButton button,littlebutton;
private JLabel holdred1,holdred2;
private ImageIcon red1,red2;
public main(){
//sets the layout
setLayout(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
//sets the button
button = new JButton("Button");
c.gridx = 0;
c.gridy = 0;
add(button,c);
//sets the image
red1 = new ImageIcon(getClass().getResource("/images/redtile.png"));
red2 = new ImageIcon(getClass().getResource("/images/redtile.png"));
//puts it in the JLabel
holdred1 = new JLabel(red1);
c.gridx = 0;
c.gridy = 0;
add(holdred1, c);
holdred2 = new JLabel(red2);
c.gridx = 1;
c.gridy = 0;
add(holdred2, c);
littlebutton = new JButton("Click Me");
c.gridx = 0;
c.gridy = 0;
add(littlebutton,c);
event e = new event();
littlebutton.addActionListener(e);
}
public class event implements ActionListener {
public void actionPerformed(ActionEvent e){
//code goes here
}
}
public static void main(String args[]){
//displaying the window
main gui = new main();
gui.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
gui.setSize(575,575);
gui.setVisible(true);
gui.setTitle("Change Spot");
}
}
I don't have much experience with Grid Bag Layouts so I have no clue how to do this, Thanks in advance!
The question is how can I make the button change it's gridx to 1
In GridBagConstraints you specify what position you want the element to take in the grid. You already have done that, in this portion of code
c.gridx = 0;
c.gridy = 0;
add(holdred1, c);
holdred2 = new JLabel(red2);
c.gridx = 1;
c.gridy = 0;
add(holdred2, c);
littlebutton = new JButton("Click Me");
c.gridx = 0;
c.gridy = 0;
But you're telling the layout to place multiple components in the same grid using c.gridx = 0, c.gridy = 0. Every component should have its own cell space so they don't overlap.
I edited your code:
setLayout(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
//sets the button
button = new JButton("Button");
c.gridx = 2;
c.gridy = 2;
add(button, c);
//sets the image
// red1 = new ImageIcon(getClass().getResource("/images/redtile.png"));
// red2 = new ImageIcon(getClass().getResource("/images/redtile.png"));
//puts it in the JLabel
holdred1 = new JLabel("Red1");
c.gridx = 1;
c.gridy = 1;
add(holdred1, c);
holdred2 = new JLabel("Red2");
c.gridx = 1;
c.gridy = 2;
add(holdred2, c);
littlebutton = new JButton("Click Me");
c.gridx = 2;
c.gridy = 1;
add(littlebutton, c);
event e = new event();
littlebutton.addActionListener(e);
I assume that you wanted the labels to be on top of the buttons, so I place they on a side so you could practice it.
Another easy tips, if you want a component to take more than a cell in the table you have to use c.weightx = someValue, c.weighty = someValue.
Take a look: How to use GridBagLayout.
Related
I'm trying to make a mine sweeper game with Swing library (just for training).
I spotted a problem when I tried to change size of a button with setSize() method (part of Container class). One of my previous program was able to handle that (with ActionListener)
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class ButtonChanger extends JFrame {
JButton big, small, dis;
JLabel message, poof;
public ButtonChanger(){
setLayout(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
message = new JLabel("Click to make it");
c.fill = GridBagConstraints.HORIZONTAL;
c.gridx = 0;
c.gridy = 0;
add(message, c);
big = new JButton("BIG");
big.setSize(30, 30); // no reaction for this call
c.fill = GridBagConstraints.HORIZONTAL;
c.gridx = 0;
c.gridy = 1;
c.gridwidth = 1;
add(big, c);
small = new JButton("small");
c.fill = GridBagConstraints.HORIZONTAL;
c.gridx = 1;
c.gridy = 1;
add(small, c);
dis = new JButton("disapear");
c.fill = GridBagConstraints.HORIZONTAL;
c.gridx = 3;
c.gridy = 1;
add(dis, c);
poof = new JLabel(" poof!");
c.fill = GridBagConstraints.HORIZONTAL;
c.gridx = 3;
c.gridy = 1;
poof.setVisible(false);
add(poof, c);
big.setSize(300,300); // same story
event a = new event();
big.addActionListener(a);
small.addActionListener(a);
dis.addActionListener(a);
}
public class event implements ActionListener{
public void actionPerformed(ActionEvent a){
String op = a.getActionCommand();
if(op.equals("BIG")){
big.setSize(50,50); // finaly!
} else if(op.equals("small")){
small.setSize(10,10);
} else if(op.equals("disapear")){
dis.setVisible(false);
poof.setVisible(true);
}
}
}
public static void main(String args[]){
ButtonChanger gui = new ButtonChanger();
gui.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
gui.setVisible(true);
gui.setSize(250,250);
gui.setTitle("");
}
}
But when I tried to use same solution here - nothing happened.
import java.util.Random;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Board extends JPanel
implements ActionListener{
private Integer pixelWidth;
private Integer pixelHeight;
public Field[][] board;
private Random generator = new Random();
public Board(int width,int height){ // width - x axis scaled with "button" unit
// height - y axis scaled with "button" unit
setLayout(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
board = new Field[width][height];
pixelWidth = width * Field.fieldW;
pixelHeight = height * Field.fieldH;
for(int i = 0;i < height;i++)
for(int j = 0;j < width;j++){
board[i][j] = new Field(generator.nextBoolean());
}
for(int i = 0;i < height;i++){
c.fill = GridBagConstraints.HORIZONTAL;
c.gridy = i;
for(int j = 0;j < width;j++){
c.gridx = j;
add(board[i][j].getFieldButton(),c);
add(board[i][j].getFieldLabel(),c);
}
}
/* for(int i = 0;i < height;i++)
for(int j = 0;j < width;j++){
board[i][j].setField();
}*/
}
public void actionPerformed(ActionEvent e){
String co = e.getActionCommand();
if(co.equals(" ")){ // button is single space sign
for(int i = 0;i < 10;i++)
for(int j = 0;j < 10;j++){
board[i][j].setField(); //inside this method is call for setSize() for fieldButton
}
}
}
}
Previously I changed Board class to make it inhetitate after JFrame (set up as main GUI frame in whole code) but that gave me same output as original.
Did I mess up something?
big = new JButton("BIG");
big.setSize(30, 30); // no reaction for this call
c.fill = GridBagConstraints.HORIZONTAL;
There is no reaction for big.setSize... because then in GridBagConstraints you set fill to horizontal, so button will be "stretched" to fill horizontally.
Instead use c.fill = GridBagConstraints.NONE
fill
Used when the component's display area is larger than the component's requested size to determine whether and how to resize the component. Valid values (defined as GridBagConstraints constants) include NONE (the default), HORIZONTAL (make the component wide enough to fill its display area horizontally, but do not change its height), VERTICAL (make the component tall enough to fill its display area vertically, but do not change its width), and BOTH (make the component fill its display area entirely).
You can read more here: https://docs.oracle.com/javase/tutorial/uiswing/layout/gridbag.html
I am trying to get a JScrollPane to appear on my JTable. I passed the table to the scrollpane when i created an instance of the component. But to no avail it has yet to show on my table.
table = new JTable();
scrollPane = new JScrollPane(table);
table.setPreferredSize(new Dimension(200,100));
I dont know how i can fix this issue, i cant seem to find an issue that would cause it to fail. Here is the rest of the GUI code. It is very long. Adding the jtable to a jpanel starts at line 152.
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package javasql;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.GraphicsConfiguration;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.Insets;
import java.awt.event.ActionListener;
import javax.swing.*;
import java.awt.event.ActionEvent;
import javax.swing.table.DefaultTableModel;
/**
*
* #author KJ4CC
*/
public class UserInterface implements ActionListener {
DefaultTableModel dtm = new DefaultTableModel(0, 0);
public UserInterface() {
startGui();
}
JFrame frame = new JFrame();
Javasql sql = new Javasql();
JPanel buttom = new JPanel(new GridBagLayout());
JPanel commandPane = new JPanel(new GridBagLayout());
JPanel top = new JPanel(new GridBagLayout());
JPanel buttons = new JPanel(new GridBagLayout());
JPanel label = new JPanel(new GridBagLayout());
JButton connect = new JButton("Connect To Database");
JButton clr = new JButton("Clear Command");
JButton exeSql = new JButton("Execute SQL Command");
JButton clrRes = new JButton("Clear Result Window");
JLabel infoLabel = new JLabel("Enter Database Information ");
JLabel driverLabel = new JLabel("JDBC Driver: ");
JLabel dbLabel = new JLabel("Database URL: ");
JLabel userLabel = new JLabel("Username: ");
JLabel passLabel = new JLabel("Password: ");
JLabel sqlLabel = new JLabel("Enter SQL Command: ");
JLabel connectionLabel = new JLabel("No Connection Now ");
JLabel exeLabel = new JLabel("SQL Execution Result: ");
//creating an instance of the new table
public JTable table;
public JScrollPane scrollPane;
JComboBox driverSelect = new JComboBox();
JComboBox url = new JComboBox();
JTextField username = new JTextField();
JTextField pass = new JTextField();
JTextArea command = new JTextArea(1, 1);
public void startGui() {
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
GridBagConstraints c = new GridBagConstraints();
System.out.println("sdf");
c.insets = new Insets(0, 0, 0, 10);
c.fill = 0;
c.weightx = 1;
//adding all of the compoenets to their panel and then to the frame.
c.gridx = 0;
c.gridy = 0;
c.fill = GridBagConstraints.HORIZONTAL;
top.add(infoLabel, c);
c.gridx = 0;
c.gridy = 1;
top.add(driverLabel, c);
c.gridx = 1;
c.gridy = 1;
c.ipadx = 150;
c.fill = GridBagConstraints.HORIZONTAL;
top.add(driverSelect, c);
c.gridx = 0;
c.gridy = 2;
c.fill = 0;
top.add(dbLabel, c);
c.gridx = 1;
c.gridy = 2;
c.fill = GridBagConstraints.HORIZONTAL;
top.add(url, c);
c.gridx = 0;
c.gridy = 3;
c.fill = 0;
c.fill = 0;
top.add(userLabel, c);
c.gridx = 1;
c.gridy = 3;
c.fill = GridBagConstraints.HORIZONTAL;
top.add(username, c);
c.gridx = 0;
c.gridy = 4;
c.fill = 0;
c.fill = 0;
top.add(passLabel, c);
c.gridx = 1;
c.gridy = 4;
c.fill = GridBagConstraints.HORIZONTAL;
top.add(pass, c);
//add the driver and url to the comboboxes
c.gridx = 2;
c.gridy = 0;
commandPane.add(sqlLabel, c);
c.gridx = 2;
c.gridy = 1;
c.ipadx = 150;
c.ipady = 75; //sql text area for command
c.fill = GridBagConstraints.FIRST_LINE_END;
c.fill = GridBagConstraints.HORIZONTAL;
commandPane.add(command, c);
c.insets = new Insets(0, 0, 0, 20);
c.ipadx = 9;
c.ipady = 1;
c.gridx = 0;
c.gridy = 0;
//buttons
label.add(connectionLabel, c);
c.gridx = 1;
c.gridy = 0;
//c.insets = new Insets(0, 0, 0, 50);
buttons.add(connect, c);
connect.addActionListener(this);
c.gridx = 2;
buttons.add(clr, c);
clr.addActionListener(this);
c.gridx = 3;
buttons.add(exeSql, c);
exeSql.addActionListener(this);
//adding the label and buttons above and below the tabel.
c.gridx = 0;
c.gridy = 1;
buttom.add(exeLabel, c);
c.gridx = 0;
c.gridy = 2;
//-----------------------------------------------------------------Table here
table = new JTable();
scrollPane = new JScrollPane(table);
table.setPreferredSize(new Dimension(200, 100));
c.fill = GridBagConstraints.HORIZONTAL;
buttom.add(table, c);
buttom.add(scrollPane);
c.gridx = 0;
c.gridy = 3;
buttom.add(clrRes, c);
c.weightx = 2;
c.weighty = 2;
c.gridx = 0;
c.gridy = 0;
frame.setLayout(new GridLayout(3, 2));
frame.add(top);
c.gridx = 2;
c.gridy = 1;
frame.add(commandPane);
frame.add(label);
frame.add(buttons);
frame.add(buttom, BorderLayout.SOUTH);
//adding the content panel to the jframe.
frame.pack();
frame.setSize(1000, 550);
frame.setVisible(true);
//adding items to both of the combo boxes.
driverSelect.addItem("com.mysql.jdbc.Driver");
url.addItem("jdbc:mysql://localhost:3306/project3");
}
public void actionPerformed(ActionEvent e) {
if (e.getSource() == connect) {
sql.connect(this);
} else if (e.getSource() == clr) {
command.setText("");
} else if (e.getSource() == exeSql) {
sql.exeCommand(this);
}
}
}
You can not add the table twice :
table = new JTable();
scrollPane = new JScrollPane(table); //here
table.setPreferredSize(new Dimension(200, 100));
c.fill = GridBagConstraints.HORIZONTAL;
buttom.add(table, c); //here
buttom.add(scrollPane);
If you add it to the scrollPane, just add the scrollpane. A Component can't have two parents.
I did not check your complete code but try
buttom.add(scrollPane,c);
instead of this
buttom.add(table, c); //here
buttom.add(scrollPane);
scrollPane = new JScrollPane(table);
table.setPreferredSize(new Dimension(200, 100));
c.fill = GridBagConstraints.HORIZONTAL;
buttom.add(table, c);
buttom.add(scrollPane);
here you add the table twice, directly in the first line and (implicitly) along with the ScrollPane at the last line.
In Swing this is not possible. Therefore the JTable is removed from the Scrollpane at the first line, when you add it directly to the bottom panel, and in turn at the last line an empty scrollpane is added, removing the JTable added earlier.
just remove the first line.
I'm trying to make 4 columns within a panel.
JLabel(Title)
JLabel JLabel
JLabel JTextArea JLabel JTextArea
...
...
...
JButton
Its pretty much a panel where you enter data. The labels will be something like "speed" then you type in a number in the text area next to it. I'm having a problem with the gridbaglayout though. The title is pretty big so it looks something like this. Using GridBagLayout , The title is at (1,0) but seeing as its so big, when I put the JLabel1 (0,1) and JLabel2 (2,0), they are way too spaced out since the title seems to have taken a pretty big chunk.
JLabel(Title..............)
JLabel1 JLabel2
...
...
...
JButton
I want it to be more like
JLabel(Title..........................................)
JLabel JLabel
JLabel JTextArea JLabel JTextArea
...
...
...
JButton
The code if you'd like to run it:
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
public class Example {
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable(){
#Override
public void run(){
new Example();
}
});
}
JFrame go = new JFrame("Example");
JPanel panel = new JPanel();
JButton Button = new JButton("Button");
GridBagLayout Grid = new GridBagLayout();
JLabel Title = new JLabel("LARGEE TITLEE");
JLabel Label1 = new JLabel("Label 1");
JLabel Label2 = new JLabel("Label 2");
public Example() {
panel.setLayout(Grid);
GridBagConstraints c = new GridBagConstraints();
Title.setFont(new Font("Serif", Font.BOLD, 60));
c.insets = new Insets(10,10,10,10);
c.gridy = 0; c.gridx = 1;
panel.add(Title, c);
c.gridy = 1; c.gridx = 0;
panel.add(Label1 , c);
c.gridx = 2;
panel.add(Label2, c);
c.gridy = 2; c.gridx = 1;
panel.add(Button, c);
go.add(panel);
go.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
go.setSize(750, 750);
go.setVisible(true);
}
}
Divide the screen into parts and specify the width of every component (gridwidth), as well as the gridx and gridy, so that they will be placed accordingly.
The output of the sample I wrote looks like this:
Code:
public class Example extends JPanel {
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
new Example();
}
});
}
JFrame go = new JFrame("Example");
JPanel panel = new JPanel();
GridBagLayout Grid = new GridBagLayout();
JLabel Title = new JLabel("LARGE TITLE", SwingConstants.CENTER);
JLabel Label1 = new JLabel("Label 1", SwingConstants.CENTER);
JLabel Label2 = new JLabel("Label 2", SwingConstants.CENTER);
JLabel anotherLabel1 = new JLabel("Another Label 1", SwingConstants.CENTER);
JLabel anotherLabel2 = new JLabel("Another Label 2", SwingConstants.CENTER);
JTextArea textArea1 = new JTextArea("TextArea 1");
JTextArea textArea2 = new JTextArea("TextArea 2");
public Example() {
panel.setLayout(Grid);
panel.setComponentOrientation(ComponentOrientation.LEFT_TO_RIGHT);
Title.setFont(new Font("Serif", Font.BOLD, 60));
JButton button;
panel.setLayout(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
c.fill = GridBagConstraints.HORIZONTAL;
c.ipady = 40; //increase height of the title
c.weightx = 0.5;
c.gridwidth = 4;
c.gridx = 0;
c.gridy = 0;
panel.add(Title, c);
c.ipady = 0;
c.gridwidth = 2;
c.weightx = 0.5;
c.gridx = 0;
c.gridy = 1;
panel.add(Label1, c);
c.weightx = 0.5;
c.gridwidth = 2;
c.gridx = 2;
c.gridy = 1;
panel.add(Label2, c);
c.ipady = 0;
c.gridwidth = 1;
c.weightx = 0.25;
c.gridx = 0;
c.gridy = 2;
panel.add(anotherLabel1, c);
c.weightx = 0.25;
c.gridx = 1;
c.gridy = 2;
panel.add(textArea1, c);
c.weightx = 0.25;
c.gridx = 2;
c.gridy = 2;
panel.add(anotherLabel2, c);
c.weightx = 0.25;
c.gridx = 3;
c.gridy = 2;
panel.add(textArea2, c);
button = new JButton("JButton");
c.ipady = 0;
c.weighty = 1.0;
c.insets = new Insets(10, 0, 0, 0);
c.gridx = 1;
c.gridwidth = 2;
c.gridy = 3;
panel.add(button, c);
go.add(panel);
go.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
go.pack();
go.setSize(750, 300);
go.setVisible(true);
}
}
Related Documentation:
How to Use GridBagLayout
If I get it right, what you need is to set the title label full width, try this:
c.fill = GridBagConstraints.HORIZONTAL;
c.gridwidth = 4; //where 4 is the numbers of columns
c.gridx=0; // set position at (0,0) because now is full width
panel.add(title, c);
I am making a simple calculator GUI in Java to start to learn how to use Java. The following code is not working. Only the first column is appearing. Where is the second and third column going?
package Start;
import java.awt.*;
import javax.swing.*;
public class CalculatorGUI {
public static void addComponentsToPane(Container pane) {
pane.setLayout(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
c.fill = GridBagConstraints.HORIZONTAL;
JLabel label;
JButton button;
label = new JLabel("I'm a calculator");
c.gridwidth = GridBagConstraints.REMAINDER;
c.weightx = 0.0;
c.gridx = 0;
c.gridy = 0;
pane.add(label, c);
button = new JButton("1");
c.gridx = 0;
c.gridy = 1;
pane.add(button, c);
button = new JButton("2");
c.gridx = 1;
c.gridy = 1;
pane.add(button, c);
button = new JButton("3");
c.gridx = 2;
c.gridy = 1;
pane.add(button, c);
button = new JButton("4");
c.gridx = 0;
c.gridy = 2;
pane.add(button, c);
button = new JButton("5");
c.gridx = 1;
c.gridy = 2;
pane.add(button, c);
button = new JButton("6");
c.gridx = 2;
c.gridy = 2;
pane.add(button, c);
button = new JButton("7");
c.gridx = 0;
c.gridy = 3;
pane.add(button, c);
button = new JButton("8");
c.gridx = 1;
c.gridy = 3;
pane.add(button, c);
button = new JButton("9");
c.gridx = 2;
c.gridy = 3;
pane.add(button, c);
}
public static void createAndShowGUI() {
JFrame frame = new JFrame("CalculatorGUI");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
addComponentsToPane(frame.getContentPane());
frame.pack();
frame.setVisible(true);
}
public static void main(String[] args) {
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}
}
i tried making the label just say c.gridwidth = 3; but that does the same thing. When i make the width just equal 1 all the buttons appear, but the label is in only in one cell. How do i make the label span 3 columns? Without making the other buttons disappear.
How do i make the label span 3 columns without gridwidth?
c.gridwidth = 3;
You will then need to reset it to 1 before adding other components.
so that it is centered over the '2' button
You will also need to set the text alignment of the label:
label.setHorizontalAlignment(JLabel.CENTER);
You're using
c.gridwidth = GridBagConstraints.REMAINDER;
Which Specifies that the current component is the last component in its column or row.
So comment this line out, and it should work just fine.
Reaf more About GridBagLayout.
The code below was copied from Oracle tutorials and modified to place components OTHER than buttons--a label and two text fields and an additional button. It produces the form that it should (shown below), so you shouldn't have to scrutinize the first 30 or so lines of code:
If I UNcomment out the two lines that refer to "box" and comment out the adjoining lines that refer to button, look at where the checkbox is placed.
I can't figure out how what's going on. I want the checkbox on the last line, as the code SEEMS to say it MUST be positioned, yet it is NOT.
All I am doing is changing TWO LINES that refer to button to refer instead to checkbox. Are the rules for GridBagLayout component-dependent?? What should I do (and why isn't the code working as is) to get checkbox on bottom line?
public class Testy {
public static void addComponentsToPane(Container pane) {
pane.setLayout(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
c.fill = GridBagConstraints.HORIZONTAL;
c.weightx = 0.5;
c.gridx = 0; c.gridy = 0;
JLabel label = new JLabel("Button 1 ");
pane.add(label, c);
JTextField text = new JTextField("Button 2 ");
c.gridx = 1; c.gridy = 0;
pane.add(text, c);
JButton button = new JButton("Button 3 ");
c.gridx = 2; c.gridy = 0;
pane.add(button, c);
text = new JTextField("LONG Text 4 ");
c.ipady = 40;
c.weightx = 0.0;
c.gridwidth = 3;
c.gridx = 0; c.gridy = 1;
pane.add(text, c);
button = new JButton("5 ");
c.ipady = 0;
c.weighty = 1.0;
c.gridx = 1; c.gridy = 2;
c.gridwidth = 2;
pane.add(button, c);
// Here's the part of the code in question
button = new JButton("666"); // This placement is FINE, at bottom of form.
// JCheckBox box = new JCheckBox("Me?"); // "box" does NOT get placed on last line.
c.weighty = 0;
c.gridx = 0; c.gridy = 3;
c.gridwidth = 1;
// pane.add(box);
pane.add(button, c);
}
Here are the required imports and two methods that I removed so the problem code area above could be seen without scrolling:
import java.awt.*;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
private static void createAndShowGUI() /* calls addComponentsToPane() */{
JFrame frame = new JFrame("GridBagLayoutDemo");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
addComponentsToPane(frame.getContentPane());
frame.pack();
frame.setVisible(true);
}
public static void main(String[] args) /* calls createAndShowGUI() */ {
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
} }); }
}
// pane.add(box);
pane.add(button, c);
You just have to use the right method:
pane.add(box, c);
See Container#add(Component comp) and Container#add(Component comp, Object constraints).