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);
Related
I have JFrame form which must contain TextField somewhere around center of the form, I am using GridBagLayout to set TextField position on the frame.
The code like this below:
public static JFrame enterFrameDraw(JFrame frame){
JButton btnEnter = new JButton("Sign in!");
JLabel loginLabel = new JLabel("Login!");
GridBagLayout gbl = new GridBagLayout();
JTextField textFieldLogin = new JTextField();
textFieldLogin.setBackground(Color.GRAY);
btnEnter.addActionListener(e -> {
try{
Chat.btnEnterHandler(frame);
} catch (Exception e2){
e2.printStackTrace();
}
});
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
frame.setTitle(FRM_TITLE);
frame.setLocation(FRM_LOC_X, FRM_LOC_Y);
frame.setSize(FRM_WIDTH, FRM_HEIGHT);
frame.setLayout(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
c.anchor = GridBagConstraints.NORTH;
c.fill = GridBagConstraints.NONE;
c.gridheight = 1;
c.gridwidth = GridBagConstraints.REMAINDER;
c.gridx = GridBagConstraints.RELATIVE;
c.gridy = GridBagConstraints.RELATIVE;
c.insets = new Insets(40, 0, 0, 0);
c.ipadx = 0;
c.ipady = 0;
c.weightx = 0.0;
c.weighty = 0.0;
gbl.setConstraints(textFieldLogin, c);
frame.add(textFieldLogin, c);
frame.setResizable(false);
frame.setVisible(true);
return frame;
}
And result seems like:
Image
Could the problem lies in the "frame.add()" instead of simple "add()"?
UPDATE:
Method enterFrameDraw() after modification. Still doesn't work.
public static JFrame enterFrameDraw(JFrame frame){
JButton btnEnter = new JButton("Sign in!");
JLabel loginLabel = new JLabel("Login!");
GridBagLayout gbl = new GridBagLayout();
JTextField textFieldLogin = new JTextField();
textFieldLogin.setBackground(Color.GRAY);
btnEnter.addActionListener(e -> {
try{
Chat.btnEnterHandler(frame);
} catch (Exception e2){
e2.printStackTrace();
}
});
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
frame.setTitle(FRM_TITLE);
frame.setLocation(FRM_LOC_X, FRM_LOC_Y);
frame.setSize(FRM_WIDTH, FRM_HEIGHT);
frame.setLayout(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
c.anchor = GridBagConstraints.NORTH;
c.fill = GridBagConstraints.HORIZONTAL;
c.gridheight = 1;
c.gridwidth = GridBagConstraints.REMAINDER;
c.gridx = GridBagConstraints.RELATIVE;
c.gridy = GridBagConstraints.RELATIVE;
c.insets = new Insets(0, 40, 0, 40);
c.ipadx = 0;
c.ipady = 0;
c.weightx = 0.5;
c.weighty = 0.0;
gbl.setConstraints(textFieldLogin, c);
frame.add(textFieldLogin);
frame.setResizable(false);
frame.setVisible(true);
return frame;
}
UPDATE 2:
What i want to have on the form.
Image: This one
As far as I can tell there are two problems.
You need to set your constraints' fill to horizontal:
c.fill = GridBagConstraints.HORIZONTAL;
You need to set the constraints' weightx to a non-zero value:
c.weightx = 0.5;
Edit 1:
There's 2 ways you can centralize the content:
Define the constraints' insets to push it in from the left and right, like so:
c.insets = new Insets(0, 40, 0, 40);
The value 40 here is just an example. You could change the parameters to whatever suits.
You can use javax.swing.Box to create empty grid cells. To keep your content in the center, you can create an empty 'strut' on either side of the middle components:
c.gridx = 0;
c.gridy = 0;
c.weightx = 0.5;
frame.add(Box.createHorizontalStrut(100), c);
c.gridx = 1;
frame.add(textFieldLogin, c);
c.gridx = 2;
frame.add(Box.createHorizontalStrut(100), c);
Again, the value of 100 here is just an example, you should tweak the parameters to your needs.
It would be worthwhile reading up on things like Struts and Glues in the context of gridbaglayouts, they're really useful in organizing your content the way you want it.
Edit 2:
To add more components below the text field that's in the center, you should use the second approach of adding horizontal struts to the cells in the first row.
The idea is that putting a strut in a cell fills up space that would be used by a component, if there was a component in that cell. So by specifying the width of a horizontal strut in the first row, any following components with the same x value as the strut will share the same width as the strut.
So in this case, your method might look something like this:
public static JFrame enterFrameDraw(JFrame frame){
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
frame.setResizable(false);
frame.setSize(500, 500);
frame.setLayout(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
c.fill = GridBagConstraints.HORIZONTAL;
c.gridx = 0;
c.gridy = 0;
c.weightx = 1;
frame.add(Box.createHorizontalStrut(80), c);
JTextField textFieldLogin = new JTextField();
textFieldLogin.setBackground(Color.GRAY);
c.gridx = 1;
c.gridy = 0;
frame.add(textFieldLogin, c);
c.gridx = 2;
c.gridy = 0;
frame.add(Box.createHorizontalStrut(80), c);
JLabel label = new JLabel("Label");
c.gridx = 0;
c.gridy = 1;
frame.add(label, c);
frame.setVisible(true);
return frame;
}
Here a label is added to the second row, and it does not appear in the center.
what I want to achieve is something like this:
This is my code:
JDialog messageDialog = new JDialog();
messageDialog.setLayout(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
messageDialog.setBounds(0, 0, 350, 250);
messageDialog.setLocationRelativeTo(null);
messageDialog.setVisible(true);
JPanel btnPanel = new JPanel();
JPanel clearPanel = new JPanel();
JPanel labelsPanel = new JPanel();
JPanel txtPanel = new JPanel();
JButton newMessage = new JButton("New");
JButton recievedMessages = new JButton("Recieved");
JButton sendMessages = new JButton("Sent");
JButton refreshMessages = new JButton("Refresh");
JLabel recievedMessLab = new JLabel("Messages get:");
JTextPane txtToSend = new JTextPane();
btnPanel.setLayout(new GridLayout(4, 1));
btnPanel.add(newMessage);
btnPanel.add(recievedMessages);
btnPanel.add(sendMessages);
btnPanel.add(refreshMessages);
c.gridx = 0;
c.gridy = 0;
messageDialog.add(clearPanel, c);
c.gridx = 1;
c.gridy = 0;
messageDialog.add(labelsPanel, c);
c.gridx = 0;
c.gridy = 1;
messageDialog.add(btnPanel, c);
c.gridx = 1;
c.gridy = 1;
messageDialog.add(txtPanel, c);
labelsPanel.add(recievedMessLab);
I don't know why I get some free space around all the panels and I can't figure out how to resize the grids. Oracle tutorial doesn't help too. What is the easiest way to resize this? How to rid of that free space?
You need to add weight and fill information to your GridBagConstraints so the layout manager knows which components to strech over the available space.
Try the following:
c.gridx = 0;
c.gridy = 0;
c.fill = c.NONE; // dont fill (strech)
messageDialog.add(clearPanel, c);
c.gridx = 1;
c.gridy = 0;
c.weightx = 1; // horizontal weight: 1
c.fill = c.HORIZONTAL; // fill (strech) horizontally
messageDialog.add(labelsPanel, c);
c.gridx = 0;
c.gridy = 1;
c.weightx = 0; // horizontal weight: back to 0
c.weighty = 1; // vertical weight: 1
c.fill = c.VERTICAL; // fill (strech) vertically
messageDialog.add(btnPanel, c);
c.gridx = 1;
c.gridy = 1;
c.weightx = 1; // both weights: 1
c.weighty = 1; // both weights: 1
c.fill = c.BOTH; // and fill both ways, vertically and horizontally
messageDialog.add(txtPanel, c);
Revisit the part about weightx, weighty and fill in the tutorial to get a clue how they work.
PS: txtPanel is empty and txtToSend is never used?
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.
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
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.