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:
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 :
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.