-1

I'm programming a simple calculator in Java using Swing and i'm having trouble with the GridBagLayout.

Here's how i have sliced my components:

my window

enter image description here

So as you can see, the cos sin tan % log sqrt sqr and pi buttons are not supposed to be far away from the other ones.

Also, this is what happens when i reduced the size of the window when the program is launched :

enter image description here

The buttons are displayed the way i want them to be displayed but the JTable is displayed weirdly.

Here's some informations about the mainwindow:

preferred size : 600*600 Jtable : no size informations setted

and every component has his weight attribute setted to 0 in the GridBagConstraints object (i think it could come from this

EDIT: some code! this is the enum that i use to initialize my buttons, so you can see the GridBagConstraints.

//new GridBagConstraints(int gridx, int gridy, int gridwidth, int gridheight, double weightx, double weighty, 
//int anchor, int fill, Insets insets, int ipadx, int ipady);
ZERO("0", 96, new GridBagConstraints(5,5,1,1,0,0,GridBagConstraints.CENTER,
                GridBagConstraints.NONE,new Insets(5,5,5,5),0,0)), //
        ONE("1", 97, new GridBagConstraints(4,4,1,1,0,0,GridBagConstraints.CENTER,
                GridBagConstraints.NONE,new Insets(5,5,5,5),0,0)), //
        TWO("2", 98, new GridBagConstraints(5,4,1,1,0,0,GridBagConstraints.CENTER,
                GridBagConstraints.NONE,new Insets(5,5,5,5),0,0)), //
        THREE("3", 99, new GridBagConstraints(6,4,1,1,0,0,GridBagConstraints.CENTER,
                GridBagConstraints.NONE,new Insets(5,5,5,5),0,0)), //
        FOUR("4", 100, new GridBagConstraints(4,3,1,1,0,0,GridBagConstraints.CENTER,
                GridBagConstraints.NONE,new Insets(5,5,5,5),0,0)), //
        FIVE("5", 101, new GridBagConstraints(5,3,1,1,0,0,GridBagConstraints.CENTER,
                GridBagConstraints.NONE,new Insets(5,5,5,5),0,0)), //
        SIX("6", 102, new GridBagConstraints(6,3,1,1,0,0,GridBagConstraints.CENTER,
                GridBagConstraints.NONE,new Insets(5,5,5,5),0,0)), //
        SEVEN("7", 103, new GridBagConstraints(4,2,1,1,0,0,GridBagConstraints.CENTER,
                GridBagConstraints.NONE,new Insets(5,5,5,5),0,0)), //
        EIGHT("8", 104, new GridBagConstraints(5,2,1,1,0,0,GridBagConstraints.CENTER,
                GridBagConstraints.NONE,new Insets(5,5,5,5),0,0)), //
        NINE("9", 105, new GridBagConstraints(6,2,1,1,0,0,GridBagConstraints.CENTER,
                GridBagConstraints.NONE,new Insets(5,5,5,5),0,0)), //
        DOT(".", 110, new GridBagConstraints(6,5,1,1,0,0,GridBagConstraints.CENTER,
                GridBagConstraints.NONE,new Insets(5,5,5,5),0,0)), //
        PLUS("+", 107, new GridBagConstraints(7,2,1,1,0,0,GridBagConstraints.CENTER,
                GridBagConstraints.NONE,new Insets(5,5,5,5),0,0)), //
        MINUS("-", 109, new GridBagConstraints(7,3,1,1,0,0,GridBagConstraints.CENTER,
                GridBagConstraints.NONE,new Insets(5,5,5,5),0,0)), //
        DIVIDE("/", 111, new GridBagConstraints(7,5,1,1,0,0,GridBagConstraints.CENTER,
                GridBagConstraints.NONE,new Insets(5,5,5,5),0,0)), //
        MULTIPLY("*", 106, new GridBagConstraints(7,4,1,1,0,0,GridBagConstraints.CENTER,
                GridBagConstraints.NONE,new Insets(5,5,5,5),0,0)), //
        MODULO("%", 0, new GridBagConstraints(7,6,1,1,0,0,GridBagConstraints.CENTER,
                GridBagConstraints.NONE,new Insets(5,5,5,5),0,0)), //
        EQUAL("=", 10, new GridBagConstraints(4,5,1,1,0,0,GridBagConstraints.CENTER,
                GridBagConstraints.NONE,new Insets(5,5,5,5),0,0)), //
        CLEAR("C", 67, new GridBagConstraints(7,1,1,1,0,0,GridBagConstraints.CENTER,
                GridBagConstraints.NONE,new Insets(5,5,5,5),0,0)), //
        BACKSPACE("Bck", 8, new GridBagConstraints(7,0,1,1,0,0,GridBagConstraints.CENTER,
                GridBagConstraints.NONE,new Insets(5,5,5,5),0,0)), //
        CLEARHISTORY("Clear history", 72, new GridBagConstraints(0,7,4,1,0,0,GridBagConstraints.CENTER,
                GridBagConstraints.NONE,new Insets(5,5,5,5),0,0)), //
        COSINUS("cos", 72, new GridBagConstraints(4,6,1,1,0,0,GridBagConstraints.CENTER,
                GridBagConstraints.NONE,new Insets(5,5,5,5),0,0)), //
        SINUS("sin", 72, new GridBagConstraints(5,6,1,1,0,0,GridBagConstraints.CENTER,
                GridBagConstraints.NONE,new Insets(5,5,5,5),0,0)), //
        TANGENT("tan", 72, new GridBagConstraints(6,6,1,1,0,0,GridBagConstraints.CENTER,
                GridBagConstraints.NONE,new Insets(5,5,5,5),0,0)), //
        SQUAREROOT("sqrt", 72, new GridBagConstraints(5,7,1,1,0,0,GridBagConstraints.CENTER,
                GridBagConstraints.NONE,new Insets(5,5,5,5),0,0)), //
        SQUARE("sqr", 72, new GridBagConstraints(6,7,1,1,0,0,GridBagConstraints.CENTER,
                GridBagConstraints.NONE,new Insets(5,5,5,5),0,0)), //
        LOGARITHM("log", 72, new GridBagConstraints(4,7,1,1,0,0,GridBagConstraints.CENTER,
                GridBagConstraints.NONE,new Insets(5,5,5,5),0,0)), //
        PLUSMINUS("±", 72, new GridBagConstraints(7,7,1,1,0,0,GridBagConstraints.CENTER,
                GridBagConstraints.NONE,new Insets(5,5,5,5),0,0));

and this is the code where i initialize the View:

        CALCULATIONSHISTORYLABEL(new GridBagConstraints(0,0,4,1,0,0,GridBagConstraints.CENTER,
                GridBagConstraints.NONE,new Insets(5,5,5,5),0,0)),
        CALCULATIONSHISTORYTABLE(new GridBagConstraints(0,1,4,6,0,0,GridBagConstraints.CENTER,
                GridBagConstraints.NONE,new Insets(5,5,5,5),0,0)),
        CURRENTCALCULATIONLABEL(new GridBagConstraints(4,0,3,1,0,0,GridBagConstraints.LINE_END,
                GridBagConstraints.NONE,new Insets(5,5,5,5),0,0)),
        CURRENTRESULTLABEL(new GridBagConstraints(4,1,3,1,0,0,GridBagConstraints.LINE_END,
                GridBagConstraints.NONE,new Insets(5,5,5,5),0,0));

/*...*/

    public View() {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setTitle("simpleCount");
        setPreferredSize(new Dimension(800, 800));
        setLayout(new GridBagLayout());
        calculationsHistoryView = new JTable();

        GridBagConstraints c = new GridBagConstraints();
        add(new JLabel("Calculations History"), ComponentPlacement.CALCULATIONSHISTORYLABEL.gbc);
        add(new JScrollPane(calculationsHistoryView), ComponentPlacement.CALCULATIONSHISTORYTABLE.gbc);

        add(currentCalculationView, ComponentPlacement.CURRENTCALCULATIONLABEL.gbc);
        add(currentResultView, ComponentPlacement.CURRENTRESULTLABEL.gbc);
        for (int i = 0; i < Button.values().length; i++) {
            add(buttonsView.getButtons()[i], Button.values()[i].gbc);
        }
        pack();
        setVisible(true);
        requestFocus();
    }

Could anyone help me ?

Thanks in advance for your answer.

Devz
  • 563
  • 7
  • 23
  • 1
    For better help sooner, post a [MCVE] or [Short, Self Contained, Correct Example](http://www.sscce.org/). – Andrew Thompson Nov 19 '15 at 12:41
  • 1
    did you set the height and width of all the components to zero? assuming you are trying to evenly space everything out. – JungJoo Nov 19 '15 at 12:58

2 Answers2

3

Don't use a single layout manager for the entire frame.

The default layout manager is a BorderLayout for the frame and I would stick with this.

Then you can create a "buttons panel" with your GridBagLayout and add the panel using:

frame.add(buttonPanel, BorderLayout.LINE_END);

Then you can create another "left panel" which again might use a BorderLayout. Then you could

  1. add the top label to BorderLayout.PAGE_START
  2. add the table to BorderLayout.CENTER
  3. add the bottom label to BorderLayout.PAGE_END

and finally add the left panel to the frame:

frame.add(leftPanel, BorderLayout.CENTER);

Now all the extra space in the frame will go to the table.

camickr
  • 321,443
  • 19
  • 166
  • 288
-1

I think there could be two issues:

  1. Try to set stretch width and height of table scroll from 0.1 to 1.0 (more than zero) and set stretch strategy to GridBagLayout.BOTH
  2. Set minPrefferedHeight() and minPrefferedWidth() of JScrollPane which is table container.
Matt
  • 74,352
  • 26
  • 153
  • 180
  • See [Should I avoid the use of set(Preferred|Maximum|Minimum)Size methods in Java Swing?](http://stackoverflow.com/q/7229226/418556) (Yes.) The rest remaining seems really better suited to a comment. – Andrew Thompson Nov 19 '15 at 12:39
  • The choice is yours. Some swing components have 'strange' behavior and for these situations java api provides many methods, which we have to use not to hate them ;) – Georgi Penev Nov 19 '15 at 12:59
  • You missed the major point of that thread. The answers mention that in the rare case you need to change those values, it is typically a better design to **`@Override`** the relevant methods according to the need. Calling `set..Size` methods, we are usually just making a random guess as to the numbers required. – Andrew Thompson Nov 19 '15 at 14:21