How do I put spaces between components in a GridBagLayout? - java

In a JFrame with a BorderLayout, I have a "control panel" (with buttons and stuff) on the bottom of the window. In the JPanel of this "control panel" I'd like to use a GridBagLayout.
This is the result I have right now.
I was thinking to divide the layout in a 3 rows x 8 columns table.
In this configuration, the "+" symbol should take just one square and all the buttons should fill the panel.
The code is this:
buttonsPanel.setLayout(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
c.fill = GridBagConstraints.HORIZONTAL;
removeCost = new JButton("-");
c.gridx = 5;
c.gridy = 0;
buttonsPanel.add(removeCost, c);
addCost = new JButton("+");
c.gridx = 7;
c.gridy = 0;
buttonsPanel.add(addCost, c);
text = new JLabel("Incasso");
c.gridx = 0;
c.gridy = 1;
c.gridwidth = 3;
buttonsPanel.add(text, c);
cost = new JTextArea();
cost.setBorder(BorderFactory.createLineBorder(Color.black, 1, true));
cost.setPreferredSize(new Dimension(80, 18));
c.gridx = 5;
c.gridy = 1;
c.gridwidth = 3;
buttonsPanel.add(cost, c);
cancel = new JButton("Cancella");
c.anchor = GridBagConstraints.LINE_START;
c.fill = GridBagConstraints.HORIZONTAL;
c.gridx = 0;
c.gridy = 2;
c.gridwidth = 3;
c.insets = new Insets(0, 2, 2, 30);
buttonsPanel.add(cancel, c);
enter = new JButton("Accetta");
c.anchor = GridBagConstraints.LINE_END;
c.fill = GridBagConstraints.HORIZONTAL;
c.gridx = 5;
c.gridy = 2;
c.gridwidth = 3;
c.insets = new Insets(0, 30, 2, 2);
buttonsPanel.add(enter, c);
What am I doing wrong?

You can not create a new instance of the GridBagConstrains. I did so.
I execute your code. Please see screenshot.
import javax.swing.*;
import java.awt.*;
public class app {
public static void main(String[] args) {
JFrame frame = new JFrame();
JPanel buttonsPanel = new JPanel(new GridBagLayout());
frame.add(buttonsPanel);
// =====================
GridBagConstraints c = new GridBagConstraints();
c.fill = GridBagConstraints.HORIZONTAL;
JButton removeCost = new JButton("-");
c.gridx = 5;
c.gridy = 0;
buttonsPanel.add(removeCost, c);
JButton addCost = new JButton("+");
c.gridx = 7;
c.gridy = 0;
buttonsPanel.add(addCost, c);
JLabel text = new JLabel("Incasso");
c.gridx = 0;
c.gridy = 1;
c.gridwidth = 3;
buttonsPanel.add(text, c);
JTextArea cost = new JTextArea();
cost.setBorder(BorderFactory.createLineBorder(Color.black, 1, true));
cost.setPreferredSize(new Dimension(80, 18));
c.gridx = 5;
c.gridy = 1;
c.gridwidth = 3;
buttonsPanel.add(cost, c);
JButton cancel = new JButton("Cancella");
c.anchor = GridBagConstraints.LINE_START;
c.fill = GridBagConstraints.HORIZONTAL;
c.gridx = 0;
c.gridy = 2;
c.gridwidth = 3;
c.insets = new Insets(0, 2, 2, 30);
buttonsPanel.add(cancel, c);
JButton enter = new JButton("Accetta");
c.anchor = GridBagConstraints.LINE_END;
c.fill = GridBagConstraints.HORIZONTAL;
c.gridx = 5;
c.gridy = 2;
c.gridwidth = 3;
c.insets = new Insets(0, 30, 2, 2);
buttonsPanel.add(enter, c);
frame.setVisible(true);
frame.setLocationRelativeTo(null);
frame.setSize(new Dimension(400, 400));
}
}
You need to have this behavior?

You need to create a new instance of the GridBagConstraints (c = new GridBagConstraints) every time you use it as a parameter to the add method.

Related

Using the GridBagLayout

I have never used layouts before and from what I understand GridBagLayout is probably not the easiest layout to start on, however I am trying to use it anyway. I am trying to use the GridBagLayout for my JPanel so that when I re-size it the components grow. I only need it to keep the natural height of the components but I am trying to use GridBagConstraints.HORIZONTAL to make the width of a component re-size automatically. I have the code
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.awt.font.*;
import java.io.*;
public class NumberConverterDesignTest
{
JFrame numberConversionWindow = new JFrame("Number Conversion");
JPanel numberPanel = new JPanel();
////////COMPONENTS////////
JLabel lblTitle;
JLabel lblBase2;
JLabel lblBase8;
JLabel lblBase10;
JLabel lblBase16;
JTextField txtBase2;
JTextField txtBase8;
JTextField txtBase10;
JTextField txtBase16;
///////////Font///////////
GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
String []fontFamilies = ge.getAvailableFontFamilyNames();
Font FontT5 = new Font("SansSerif", Font.BOLD, 45);
public void numberConvertGUI()
{
numberConversionWindow.setBounds(10, 10, 420, 300);
numberConversionWindow.setMinimumSize(new Dimension(420, 300));
numberConversionWindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
numberConversionWindow.setLayout(new GridLayout(1,1));
createNumberConversionPanel();
numberConversionWindow.getContentPane().add(numberPanel);
numberConversionWindow.setVisible(true);
}
public void createNumberConversionPanel()
{
numberPanel.setLayout(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
lblTitle = new JLabel();
c.weightx = 0.5;
c.fill = GridBagConstraints.HORIZONTAL;
c.gridx = 0;
c.gridy = 0;
c.gridwidth = 2;
lblTitle.setText("Number Converter");
lblTitle.setHorizontalAlignment(JLabel.CENTER);
lblTitle.setFont(FontT5);
numberPanel.add(lblTitle, c);
lblBase2 = new JLabel();
c.weightx = 0.0;
c.fill = GridBagConstraints.HORIZONTAL;
c.gridx = 0;
c.gridy = 1;
lblBase2.setText("Binary: ");
lblBase2.setHorizontalAlignment(JLabel.RIGHT);
numberPanel.add(lblBase2);
lblBase8 = new JLabel();
//c.weightx = 0.5;
c.weighty = 1.0;
c.fill = GridBagConstraints.HORIZONTAL;
c.gridx = 0;
c.gridy = 2;
lblBase8.setText("Octal: ");
lblBase8.setHorizontalAlignment(JLabel.RIGHT);
numberPanel.add(lblBase8);
lblBase10 = new JLabel();
c.weightx = 0.5;
c.fill = GridBagConstraints.HORIZONTAL;
c.gridx = 0;
c.gridy = 3;
lblBase10.setText("Decimal: ");
lblBase10.setHorizontalAlignment(JLabel.RIGHT);
numberPanel.add(lblBase10);
lblBase16 = new JLabel();
c.weightx = 0.5;
c.fill = GridBagConstraints.HORIZONTAL;
c.gridx = 0;
c.gridy = 4;
lblBase16.setText("Hexadecimal: ");
lblBase16.setHorizontalAlignment(JLabel.RIGHT);
numberPanel.add(lblBase16);
txtBase2 = new JTextField();
c.weightx = 0.0;
c.fill = GridBagConstraints.HORIZONTAL;
c.gridx = 1;
c.gridy = 1;
txtBase2.setText("");
numberPanel.add(txtBase2);
txtBase8 = new JTextField();
//c.weightx = 0.5;
c.weighty = 1.0;
c.fill = GridBagConstraints.HORIZONTAL;
c.gridx = 1;
c.gridy = 2;
txtBase8.setText("");
numberPanel.add(txtBase8);
txtBase10 = new JTextField();
c.weightx = 0.5;
c.fill = GridBagConstraints.HORIZONTAL;
c.gridx = 1;
c.gridy = 3;
txtBase10.setText("");
numberPanel.add(txtBase10);
txtBase16 = new JTextField();
c.weightx = 0.5;
c.fill = GridBagConstraints.HORIZONTAL;
c.gridx = 1;
c.gridy = 4;
txtBase16.setText("");
numberPanel.add(txtBase16);
}
public static void main(String[] args)
{
NumberConverterDesignTest nC = new NumberConverterDesignTest();
nC.numberConvertGUI();
}
}
and when this is compiled I get
My aim is to try and get
I believe the problem to be what I have set weightx to be, however I am not sure what to set it to, to get the result that I would like. Any help would be greatly appreciated
You need to pass the updated constraint each time you call the numberPanel.add method.

GridBagLayout Grid Width

When I use the code below to make a JFrame using a GridBagLayout, this is the output:
I want the first three rows' buttons to all have equal widths, but they aren't.
I also want the fourth row buttons to all have equal widths.
How can I do this?
Here is the code:
public Test() {
setTitle("Calculator");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JMenuBar menu = new JMenuBar();
setJMenuBar(menu);
setLayout(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
field = new JTextField(20);
field.setFont(new Font("Cambria Math", Font.PLAIN, 32));
field.setEditable(false);
updateDisplay();
c.fill = GridBagConstraints.HORIZONTAL;
//c.insets = new Insets(9,9,9,9);
c.gridx = 0;
c.gridy = 0;
c.gridwidth = 24;
c.gridheight = 2;
add(field, c);
// sin, cos, tan
c.weightx = 0;
c.gridwidth = 4;
c.gridheight = 1;
c.gridx = 0;
c.weightx = 0;
c.gridy = 2;
add(createButton("sin"), c);
c.gridx = 4;
add(createButton("cos"), c);
c.gridx = 8;
add(createButton("tan"), c);
// cot, sec, csc
c.gridx = 0;
c.gridy = 3;
add(createButton("cot"), c);
c.gridx = 4;
add(createButton("sec"), c);
c.gridx = 8;
add(createButton("csc"), c);
// asin, acos, atan
c.gridx = 0;
c.gridy = 4;
add(createButton("asin"), c);
c.gridx = 4;
add(createButton("acos"), c);
c.gridx = 8;
add(createButton("atan"), c);
// atan2, acot, asec, acsc
c.gridy = 5;
c.gridwidth = 3;
c.weightx = 0;
c.gridx = 0;
add(createButton("atan2"), c);
c.gridx = 3;
add(createButton("acot"), c);
c.gridx = 6;
add(createButton("asec"), c);
c.gridx = 9;
add(createButton("acsc"), c);
pack();
setVisible(true);
}
public JButton createButton(String name) {
JButton button = new JButton(name);
button.setActionCommand(name);
button.addActionListener(this);
button.setFont(new Font("Dialog", Font.PLAIN, 25));
return button;
}
I don't think you can use GridBagLayout for this.
Instead create a main panel with a GridLayout(0, 1);
Then create second panel for the first 3 buttons with a GridLayout(1, 0) and add this panel to the first panel.
Repeat for the other three rows.

wrap JScrollPane to TextArea in Java (working with GridBagConstraints)

i new to java and swing (came from c#) and try to add scrollbar to my textarea with no success.
I tried all kinds of techniques , it is very difficult to me
please take a look on the following code, I would be very happy
package firmwareUpdate;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
import javax.swing.border.EtchedBorder;
import javax.swing.border.TitledBorder;
public class firmwareUpdate {
public static void main(String[] args) {
Runnable r = new Runnable() {
#Override
public void run() {
new firmwareUpdate().createUI();
}
};
EventQueue.invokeLater(r);
}
private void createUI() {
JFrame frame = new JFrame();
frame.getContentPane().setLayout(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
//natural height, maximum width
c.fill = GridBagConstraints.HORIZONTAL;
// row #1
JButton btnUpload = new JButton("Browse hex/bin file...");
c.weightx = 0.5;
c.fill = GridBagConstraints.HORIZONTAL;
c.gridx = 0;
c.gridy = 0;
c.gridwidth = 1;
frame.getContentPane().add(btnUpload, c);
JLabel lblFileName = new JLabel("aaa");
c.fill = GridBagConstraints.HORIZONTAL;
c.weightx = 0.5;
c.gridx = 0;
c.gridy = 1;
c.gridwidth = 4;
frame.getContentPane().add(lblFileName, c);
// row #2
JButton btnConfig = new JButton("Config");
// c.fill = GridBagConstraints.HORIZONTAL;
// c.ipady = 40; //make this component tall
// c.weightx = 0.0;
c.gridx = 0;
c.gridy = 2;
c.gridwidth = 1;
frame.getContentPane().add(btnConfig, c);
JButton btnHex2Bin = new JButton("hex2bin");
// c.fill = GridBagConstraints.HORIZONTAL;
// c.ipady = 40; //make this component tall
// c.weightx = 0.0;
c.gridx = 1;
c.gridy = 2;
c.gridwidth = 1;
frame.getContentPane().add(btnHex2Bin, c);
JButton btnFirmwareUpdate = new JButton("Firmware Update");
// c.fill = GridBagConstraints.HORIZONTAL;
// c.ipady = 40; //make this component tall
// c.weightx = 0.0;
c.gridx = 2;
c.gridy = 2;
c.gridwidth = 1;
frame.getContentPane().add(btnFirmwareUpdate, c);
JButton btnFirmwareUpdateStop = new JButton("Firmware Update Stop");
//c.fill = GridBagConstraints.HORIZONTAL;
// c.ipady = 40; //make this component tall
//c.weightx = 0.0;
c.gridx = 3;
c.gridy = 2;
c.gridwidth = 1;
frame.getContentPane().add(btnFirmwareUpdateStop, c);
// row #3
JButton btnCheckSum = new JButton("Check Checksum");
c.fill = GridBagConstraints.HORIZONTAL;
// c.ipady = 40; //make this component tall
c.weightx = 0.0;
c.gridx = 0;
c.gridy = 3;
c.gridwidth = 1;
frame.getContentPane().add(btnCheckSum, c);
JButton btnStartBootloader = new JButton("Start Bootloader");
c.fill = GridBagConstraints.HORIZONTAL;
// c.ipady = 40; //make this component tall
c.weightx = 0.0;
c.gridx = 1;
c.gridy = 3;
c.gridwidth = 1;
frame.getContentPane().add(btnStartBootloader, c);
JButton btnStartApplication = new JButton("Start Application");
c.fill = GridBagConstraints.HORIZONTAL;
// c.ipady = 40; //make this component tall
c.weightx = 0.0;
c.gridx = 2;
c.gridy = 3;
c.gridwidth = 1;
frame.getContentPane().add(btnStartApplication, c);
JTextArea txtFileContent = new JTextArea(30,68);
c.fill = GridBagConstraints.HORIZONTAL;
c.ipady = 0; //reset to default
c.weighty = 1.0; //request any extra vertical space
c.anchor = GridBagConstraints.PAGE_START; //bottom of space
c.insets = new Insets(10,0,0,0); //top padding
c.gridx = 0; //aligned with button 2
c.gridy = 4; //third row
c.gridwidth = 4; //4 columns wide
frame.getContentPane().add(txtFileContent, c);
//txtFileContent.setVisible(false);
////////////// JScrollPane scrollPane = new JScrollPane(txtFileContent);
/////////////// scrollPane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
// middlePanel.add(scrollPane);
// scrollPane.setBounds(10,60,780,500);
//scrollPane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
////////////////// frame.getContentPane().add(scrollPane);
txtFileContent.setLineWrap(true);
txtFileContent.setWrapStyleWord(true);
JScrollPane scrolltxt = new JScrollPane();
scrolltxt.setViewportView(txtFileContent);
scrolltxt.setWheelScrollingEnabled(true);
frame.getContentPane().add(scrolltxt, c);
frame.setIconImage(new ImageIcon(getClass().getResource("/resources/fwup.png")).getImage());
frame.setTitle("Firmware Update");
//frame.setSize(600,800);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setVisible(true);
}
}
Try This
JTextArea txtFileContent = new JTextArea(30,68);
JScrollPane scroll = new JScrollPane(txtFileContent);
frame.add(scroll);
I'm not too sure if this will solve your problem, but have you considered that not setting any column widths or heights in your layout may be your concern (line 30 of the code)? Allow me to give you an example:
GridBagLayout layout=new GridBagLayout();
GridBagConstraints gbc=new GridBagConstraints();
layout.columnWidths=new int[] {198, 198};
layout.rowHeights=new int[] {70};
panel.setLayout(layout);
Wherein, panel is a panel that is pushed onto the JFrame.
NB: I am personally against using the built-in content pane of a JFrame, because of its tendancies to default out on the user; however, for all intents and purposes, it shouldn't be a problem if done right.
I hope this helps, and best of luck!
When I tested your code, the scroll bar for the JTextArea shows up as expected when you exceed the row count of the initial JTextArea (The default value for the scrollbars' visibility are only to be shown when needed: JScrollPane API under verticalScrollBarPolicy and horizontalScrollBarPolicy). If you are asking to have it default as visible, then use either setVerticalScrollBarPolicy(...) or setHorizontalScrollBarPolicy(...) on your JScrollPane variable. There are several options available for ScrollPaneConstants

GridBagLayout is not working as expected

GridBagLayout is not working as expected. I can not find where I'm making the mistake. I know that somewhere I'm doing some silly mistake but can't find it.
Here I have made 3 buttons, 4 text fields and 3 combo boxes, and I want to align them in proper order using GridBagLayout.
Code is as follows :
GridBagConstraints c = new GridBagConstraints();
JPanel secondPanel = new JPanel();
f=new JFrame("Server Side");
b=new JButton("3DES ALGO");
c.fill = GridBagConstraints.HORIZONTAL;
c.gridx = 1;
c.gridy = 0;
secondPanel.add(b, c);
b1=new JButton("Browse");
c.gridx = 2;
c.gridy = 0;
secondPanel.add(b1, c);
b2=new JButton("Color");
c.gridx = 0;
c.gridy = 0;
secondPanel.add(b2, c);
comboBox1 = new JComboBox(s);
c.gridx = 0;
c.gridy = 1;
secondPanel.add(b, c);
comboBox2 = new JComboBox(s);
c.fill = GridBagConstraints.HORIZONTAL;
c.gridx = 1;
c.gridy = 1;
secondPanel.add(b, c);
comboBox3 = new JComboBox(s);
c.fill = GridBagConstraints.HORIZONTAL;
c.gridx = 2;
c.gridy = 1;
secondPanel.add(b, c);
b10=new JButton("OK");
tf=new TextField();
c.fill = GridBagConstraints.HORIZONTAL;
c.gridx = 2;
c.gridy = 1;
secondPanel.add(tf, c);
tf1=new TextField();
c.fill = GridBagConstraints.HORIZONTAL;
c.gridx = 0;
c.gridy = 2;
secondPanel.add(tf1, c);
tf2=new TextField();
c.fill = GridBagConstraints.HORIZONTAL;
c.gridx = 1;
c.gridy = 2;
secondPanel.add(tf2, c);
tf3=new TextField();
c.fill = GridBagConstraints.HORIZONTAL;
c.gridx = 2;
c.gridy = 2;
secondPanel.add(tf3, c);
b.addActionListener(this);
b1.addActionListener(this);
b2.addActionListener(this);
b.setBounds(200,0,330,50);
b1.setBounds(500,100,90,30);
b2.setBounds(0,0,70,30);
Container content = f.getContentPane();
content.setBackground(Color.RED);
b.setForeground(Color.white);
b.setBackground(Color.black);
b1.setForeground(Color.white);
b1.setBackground(Color.black);
b2.setForeground(Color.white);
b2.setBackground(Color.black);
tf.setBackground(Color.CYAN);
tf1.setBackground(Color.GRAY);
tf1.setForeground(Color.white);
tf2.setBackground(Color.GRAY);
tf2.setForeground(Color.white);
tf3.setBackground(Color.GRAY);
tf3.setForeground(Color.white);
f.add(b);
f.add(b1);
f.add(b2);
secondPanel.setLayout(new GridBagLayout());
secondPanel.add(comboBox1);
secondPanel.add(comboBox2);
secondPanel.add(comboBox3);
f.add(tf);
f.add(tf1);
f.add(tf2);
f.add(tf3);
f.add(secondPanel);
f.setLayout(new GridBagLayout());
f.addWindowListener(new WindowEventListener());
f.pack();
f.setVisible(true);
The grids are 0 based, so your basic code would be:
c.gridx = 0;
c.gridy = 0;
panel.add(b1, c);
c.gridx = 1;
panel.add(b2, c);
c.gridx = 2;
panel.add(b3, c);
c.gridx = 0;
c.gridy = 1;
panel.add(combo1, c)
c.gridx = 1;
panel.add(combo2, c);
...
I'm pretty sure you need to set the layout to a GridBagLayout before adding your components to secondPanel.
I also see that you add your JComboBoxes without any constraints at all, which almost certainly is not what you want to do.
Also, I see you are calling setBounds. Don't do that. It is a LayoutManager's job to set child components' bounds, not yours.
Although the documentation isn't as clear as it could be, GridBagConstraints is a lot easier to use when you make use of GridBagConstraints.RELATIVE and GridBagConstraints.REMAINDER. gridx and gridy are set to RELATIVE by default, probably because it's usually the easiest use case.
I find my code is easiest to read if I create all of my components first, then add them to a layout.
So, if you want to lay out your buttons and fields in three rows, you could do this:
b = new JButton("3DES ALGO");
b1 = new JButton("Browse");
b2 = new JButton("Color");
b.addActionListener(this);
b1.addActionListener(this);
b2.addActionListener(this);
comboBox1 = new JComboBox(s);
comboBox2 = new JComboBox(s);
comboBox3 = new JComboBox(s);
tf = new JTextField(10);
tf1 = new JTextField(10);
tf2 = new JTextField(10);
tf3 = new JTextField(10);
GridBagConstraints c = new GridBagConstraints();
JPanel secondPanel = new JPanel(new GridBagLayout());
c.fill = GridBagConstraints.HORIZONTAL;
// Components are easier to see and use if there is some space between them.
c.insets.right = 6;
c.insets.bottom = 6;
// At this point, c.gridx and c.gridy are both GridBagConstraints.RELATIVE.
// c.gridwidth and c.gridheight are 1.
secondPanel.add(b, c);
secondPanel.add(b1, c);
// A gridwidth of REMAINDER means "This is the last component on this row."
c.gridwidth = GridBagConstraints.REMAINDER;
secondPanel.add(b2, c);
// Since the last component was added with a width of REMAINDER, the next
// component is automatically on the next row, as long as gridx and gridy
// are still set to GridBagConstraints.RELATIVE.
c.gridwidth = 1;
secondPanel.add(tf, c);
secondPanel.add(tf1, c);
secondPanel.add(tf2, c);
c.gridwidth = GridBagConstraints.REMAINDER;
secondPanel.add(tf3, c);
c.gridwidth = 1;
secondPanel.add(comboBox1, c);
secondPanel.add(comboBox2, c);
c.gridwidth = GridBagConstraints.REMAINDER;
secondPanel.add(comboBox3, c);

Issues with jlabel/button formatting

The category labels 1, 2, 3, 4 and 5 don't seem to align with row I'm assigning them to. I don't understand where the problem is; the x value is equal. If someone could lead me in the right direction, that would be really great.
Here's my code
public class GovernmentJepordyGame {
public static void main(String[] args)
{
JFrame frame = new JFrame("Jepordy");
frame.setVisible(true);
frame.setSize(1300,900);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel panel = new JPanel(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
frame.getContentPane().add(panel, BorderLayout.PAGE_START);
/*JLabel label = new JLabel("Welcome to government jepordy.");*/
JLabel catagoryOne = new JLabel("Catagory 1");
c.gridx = 0;
c.gridy = 0;
panel.add(catagoryOne, c);
JButton button500A = new JButton("500");
c.gridx = 0;
c.gridy = 1;
c.insets = new Insets(65, 65, 65, 65);
panel.add(button500A, c);
JButton button400A = new JButton("400");
c.gridx = 0;
c.gridy = 2;
panel.add(button400A, c);
JButton button300A = new JButton("300");
c.gridx = 0;
c.gridy = 3;
panel.add(button300A, c);
JButton button200A = new JButton("200");
c.gridx = 0;
c.gridy = 4;
panel.add(button200A, c);
JButton button100A = new JButton("100");
c.gridx = 0;
c.gridy = 5;
panel.add(button100A, c);
JLabel catagoryTwo = new JLabel("Catagory 2");
c.gridx = -1;
c.gridy = 0;
panel.add(catagoryTwo, c);
JButton button500B = new JButton("500");
c.gridx = -1;
c.gridy = 1;
c.insets = new Insets(65, 65, 65, 65);
panel.add(button500B, c);
JButton button400B = new JButton("400");
c.gridx = -1;
c.gridy = 2;
panel.add(button400B, c);
JButton button300B = new JButton("300");
c.gridx = -1;
c.gridy = 3;
panel.add(button300B, c);
JButton button200B = new JButton("200");
c.gridx = -1;
c.gridy = 4;
panel.add(button200B, c);
JButton button100B = new JButton("100");
c.gridx = -1;
c.gridy = 5;
panel.add(button100B, c);
JLabel catagoryThree = new JLabel("Catagory 3");
c.gridx = -2;
c.gridy = 0;
panel.add(catagoryThree);
JButton button500C = new JButton("500");
c.gridx = -2;
c.gridy = 1;
c.insets = new Insets(65, 65, 65, 65);
panel.add(button500C, c);
JButton button400C = new JButton("400");
c.gridx = -2;
c.gridy = 2;
panel.add(button400C, c);
JButton button300C = new JButton("300");
c.gridx = -2;
c.gridy = 3;
panel.add(button300C, c);
JButton button200C = new JButton("200");
c.gridx = -2;
c.gridy = 4;
panel.add(button200C, c);
JButton button100C = new JButton("100");
c.gridx = -2;
c.gridy = 5;
panel.add(button100C, c);
JLabel catagoryFour = new JLabel("Catagory 4");
c.gridx = -3;
c.gridy = 0;
panel.add(catagoryFour);
JButton button500D = new JButton("500");
c.gridx = -3;
c.gridy = 1;
c.insets = new Insets(65, 65, 65, 65);
panel.add(button500D, c);
JButton button400D = new JButton("400");
c.gridx = -3;
c.gridy = 2;
panel.add(button400D, c);
JButton button300D = new JButton("300");
c.gridx = -3;
c.gridy = 3;
panel.add(button300D, c);
JButton button200D = new JButton("200");
c.gridx = -3;
c.gridy = 4;
panel.add(button200D, c);
JButton button100D = new JButton("100");
c.gridx = -3;
c.gridy = 5;
panel.add(button100D, c);
JLabel catagoryFive = new JLabel("Catagory 5");
c.gridx = -4;
c.gridy = 0;
panel.add(catagoryFive);
JButton button500E = new JButton("500");
c.gridx = -4;
c.gridy = 1;
c.insets = new Insets(65, 65, 65, 65);
panel.add(button500E, c);
JButton button400E = new JButton("400");
c.gridx = -4;
c.gridy = 2;
panel.add(button400E, c);
JButton button300E = new JButton("300");
c.gridx = -4;
c.gridy = 3;
panel.add(button300E, c);
JButton button200E = new JButton("200");
c.gridx = -4;
c.gridy = 4;
panel.add(button200E, c);
JButton button100E = new JButton("100");
c.gridx = -4;
c.gridy = 5;
panel.add(button100E, c);
}
}
The gridx should be a positive value, something like...
JFrame frame = new JFrame("Jepordy");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel panel = new JPanel(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
frame.getContentPane().add(panel, BorderLayout.PAGE_START);
c.gridx = 0;
for (int index = 0; index < 5; index++) {
c.gridy = 0;
JLabel label = new JLabel("Catagory " + index);
c.insets = new Insets(0, 0, 0, 0);
panel.add(label, c);
c.insets = new Insets(65, 65, 65, 65);
for (int cat = 1; cat < 6; cat++) {
c.gridy++;
panel.add(new JButton(Integer.toString(cat * 100)), c);
}
c.gridx++;
}
frame.setVisible(true);
frame.setSize(1300, 900);
for instance...
You are getting x and y mixed up. The top left corner of your screen is 0,0. As you move to the right, x increases, as you move down, y increases. So assigning a (-) value to your x coordinate system is not necessary, it will move likely start moving your buttons off screen to the left. Every button, try increasing the x grid value, instead of the y, and you should get your desired results.
gridx = 10....gridx = 20.. and so on.
Even better, implement some sort of loop to iterate through each button and increase the x values upon each iterate.
Or, look into flowlayout managers, they handle this automatically.

Categories