2

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.

enter image description here

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?

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Astinog
  • 1,151
  • 3
  • 12
  • 35
  • You have to specify the Swing components of a GridBagLayout in column, then row, layout order. You create space between the components by using Insets. – Gilbert Le Blanc Feb 09 '15 at 19:19
  • 1
    See [this answer](http://stackoverflow.com/a/17876938/418556) to [Providing white space in a Swing GUI](http://stackoverflow.com/questions/17874717/providing-white-space-in-a-swing-gui). – Andrew Thompson Feb 09 '15 at 20:32
  • 1
    `cost = new JTextArea(); .. cost.setPreferredSize(new Dimension(80, 18));` should be more like `cost = new JTextArea(3,40);`. See [Should I avoid the use of set(Preferred|Maximum|Minimum)Size methods in Java Swing?](http://stackoverflow.com/q/7229226/418556) (Yes.) – Andrew Thompson Feb 09 '15 at 20:35
  • You've set the gridWidth property, but haven't reset it before using the constraints again, be careful with that – MadProgrammer Feb 09 '15 at 20:40
  • @MadProgrammer: the gridwidth for the first two components is not set and then it's set for every component. So I think that's not the problem – Astinog Feb 09 '15 at 21:00
  • I think it's "part" of the problem, but none-the-less, you just need to beware of it ;) – MadProgrammer Feb 09 '15 at 21:03

2 Answers2

0

You can not create a new instance of the GridBagConstrains. I did so. I execute your code. Please see screenshot. enter image description here

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?

Eugene Roldukhin
  • 637
  • 5
  • 14
  • I'm not creating a new instance of GridBagConstrains. This is strange. It's the same code, but it's "better" on you computer. I can't understand... – Astinog Feb 09 '15 at 20:56
  • What is Frame Layout? Can you use Look&Feel? Try paste it: try { UIManager.setLookAndFeel("DefaultMetal"); } catch (ClassNotFoundException e) { e.printStackTrace(); } catch (InstantiationException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); } catch (UnsupportedLookAndFeelException e) { e.printStackTrace(); } – Eugene Roldukhin Feb 09 '15 at 21:07
-3

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.

Jayfray
  • 415
  • 3
  • 12
  • 1
    If I do so I loose all the informations about columns and rows. And then, in the oracle's doc they don't create a new instance each time http://docs.oracle.com/javase/tutorial/uiswing/layout/gridbag.html – Astinog Feb 09 '15 at 19:38
  • 1
    @Jayfray That is not required. – Gábor Bakos Feb 09 '15 at 20:31