Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 11 months ago.
Improve this question
Today is my first day using Swing in Java, and I don't understand why this is happening. The problem is that I'm using GridBagLayout, with two columns, and then in the right panel I'm using two rows, with the upper row being a JLabel. But when I try it, I see that the JLabel doesn't respect the grid size and proportions, and it's not 50/50. I know there are better options to do this, but the point is that I don't need 50/50, I need something like 30/70, but I think that the 50/50 example is better to see the problem.
Here is my (messy) code:
JFrame ventana = new JFrame("Prueba");
Toolkit tools = Toolkit.getDefaultToolkit();
ventana.setLayout(new GridBagLayout());
Dimension dim = tools.getScreenSize();
ventana.setUndecorated(false);
ventana.setResizable(true);
ventana.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
ventana.setSize(1641, 1027);
ventana.setVisible(true);
ventana.setLocation(dim.width/2 - ventana.getWidth()/2 , dim.height/2 - ventana.getHeight()/2);
JPanel left = new JPanel();
left.setVisible(true);
left.setBackground(new Color(242, 205, 151));
JPanel right = new JPanel(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
right.setVisible(true);
right.setBackground(new Color(245, 253, 198));
JLabel label = new JLabel("TEXT");
label.setFont(customFont.deriveFont(70f));
c.weighty = 0.4;
c.insets = new Insets(20, 20, 70, 20);
c.anchor = GridBagConstraints.PAGE_END;
right.add(label, c);
JPanel sesion = new JPanel();
sesion.setVisible(true);
sesion.setBackground(new Color(174, 182, 112));
c = new GridBagConstraints();
c.gridy = 1;
c.gridwidth = 1;
c.gridheight = 1;
c.weightx = 1;
c.weighty = 1;
c.fill = GridBagConstraints.BOTH;
c.insets = new Insets(0, 100, 150, 100);
right.add(sesion, c);
c = new GridBagConstraints();
c.gridx = 0;
c.gridwidth = 1;
c.weightx = 1;
c.weighty = 1;
c.fill = GridBagConstraints.BOTH;
ventana.add(left, c);
c = new GridBagConstraints();
c.gridx = 1;
c.gridwidth = 1;
c.weightx = 1;
c.weighty = 1;
c.fill = GridBagConstraints.BOTH;
ventana.add(right, c);
And here is the result:
Lets look at the relationship between the label and the sesion panel...
JLabel label = new JLabel("TEXT");
label.setFont(customFont.deriveFont(70f));
c.weighty = 0.4;
c.insets = new Insets(20, 20, 70, 20);
c.anchor = GridBagConstraints.PAGE_END;
right.add(label, c);
c = new GridBagConstraints();
c.gridy = 1;
c.gridwidth = 1;
c.gridheight = 1;
c.weightx = 1;
c.weighty = 1;
c.fill = GridBagConstraints.BOTH;
c.insets = new Insets(0, 100, 150, 100);
right.add(sesion, c);
So, the label is using a weighty of 0.4 and the panel is using weighty of 1.0, which is suggesting that you need 140% of the available space 🤨
So, what you actually want to do, is balance the weighty value of both components, for example weighty = 0.5, for example...
JLabel label = new JLabel("TEXT");
label.setFont(customFont.deriveFont(70f));
c.weighty = 0.5;
c.insets = new Insets(20, 20, 70, 20);
c.anchor = GridBagConstraints.PAGE_END;
right.add(label, c);
c = new GridBagConstraints();
c.gridy = 1;
c.gridwidth = 1;
c.gridheight = 1;
c.weightx = 1;
c.weighty = 0.5;
c.fill = GridBagConstraints.BOTH;
c.insets = new Insets(0, 100, 150, 100);
right.add(sesion, c);
So, if we modify the existing code, removing the insets, for example...
JLabel label = new JLabel("TEXT");
c.weighty = 0.5;
c.anchor = GridBagConstraints.PAGE_END;
right.add(label, c);
JPanel sesion = new JPanel();
sesion.setBackground(new Color(174, 182, 112));
c = new GridBagConstraints();
c.gridy = 1;
c.weightx = 1;
c.weighty = 0.5;
c.fill = GridBagConstraints.BOTH;
right.add(sesion, c);
We can end up with a screen looking something more like...
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.
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.
I'm using GridBagLayout and I want to position my JLabel and textfield on the top left corner of my JPanel. I know this question might probably be a duplicate but I couldn't find one that could explain why my code ended up centered.
JLabel user = new JLabel(ss[0]);
JLabel av = new JLabel(ss[1]);
JTextField ufield = new JTextField("");
user.setBorder(BorderFactory.createLineBorder(Color.black));
av.setBorder(BorderFactory.createLineBorder(Color.black));
ufield.setBorder(BorderFactory.createLineBorder(Color.black));
//User Label
c.anchor = GridBagConstraints.NORTHWEST;
c.gridx = 0;
c.gridy = 0;
c.weightx = 1;
c.insets = new Insets(2, 0, 0, 2);
p.add(user, c);
//User TextField
c.fill = GridBagConstraints.BOTH;
c.gridx = 1;
c.gridy = 0;
c.gridwidth = 5;
c.gridheight = 1;
c.weightx = 2;
c.insets = new Insets(5, 1, 1, 5);
p.add(ufield, c);
//Available Label
c.fill = GridBagConstraints.NORTHWEST;
c.gridx = 0;
c.gridy = 1;
c.gridwidth = 1;
c.gridheight = 1;
c.weightx = 1;
c.insets = new Insets(5, 1, 1, 5);
p.add(av, c);
f.repaint();
f.revalidate();
This ends up looking like this :
I want it to be near the top instead of at the center
The JLabel and textfield are in fact aligned at the top left of the JPanel, however, the JPanel is not at the top left of the JFrame (which I'm guessing is the variable f), therefore overall it appears centered rather than on the top left.
You have to control the layout of the JFrame when you add the JPanel to it. You could do it using a BorderLayout like:
f.getContentPane().setLayout(new BorderLayout());
f.add(p, BorderLayout.PAGE_START);
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);
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.