I am trying to make a keyboard GUI with Java Swing, not JavaFX, so I am using a GridBagLayout with GridBagConstraint.
The problem is that the keys that I want to span three columns, such as the TAB key, end up pushing the keys in the row above it. I am being careful to have a consistent 30 columns in the entire layout and the cell positions that I'm adding JButtons to look correct to me.
I am using 30 columns so I have enough precision to span three columns and end the button halfway under the key above it. so, each normal key spans two columns, TAB spans 3, backspace spans 4, etc.
private final int KEYBOARD_NUM_ROWS = 5;
private final int KEYBOARD_NUM_COLS = 30;
Keyboard() {
setLayout(new GridBagLayout());
GridBagConstraints gc = new GridBagConstraints();
// Constraint Setup
gc.fill = GridBagConstraints.VERTICAL; // changing to BOTH just stretches the buttons
gc.anchor = GridBagConstraints.WEST;
gc.weightx = 0;
gc.weighty = 0;
// Button Size Types
final Dimension twoSpan = new Dimension(45, 40);
final Dimension threeSpan = new Dimension(65, 40);
final Dimension fourSpan = new Dimension(85, 40);
// Keyboard First Row
int colNum = 0;
int rowNum = 0;
while (colNum < KEYBOARD_NUM_COLS) {
JButton key = new JButton();
key.setFocusPainted(false);
if (colNum == 26) {
key.setPreferredSize(fourSpan);
gc.gridwidth = 4;
} else {
key.setPreferredSize(twoSpan);
gc.gridwidth = 2;
}
gc.gridy = rowNum;
gc.gridx = colNum;
colNum += gc.gridwidth;
add(key, gc);
}
// Keyboard Second Row
colNum = 0;
rowNum = 1;
while (colNum < KEYBOARD_NUM_COLS) {
JButton key = new JButton();
key.setFocusPainted(false);
if (colNum == 0) {
key.setPreferredSize(threeSpan);
gc.gridwidth = 3;
} else if (colNum == 27) {
key.setPreferredSize(threeSpan);
gc.gridwidth = 3;
} else {
key.setPreferredSize(twoSpan);
gc.gridwidth = 2;
}
gc.gridy = rowNum;
gc.gridx = colNum;
colNum += gc.gridwidth;
add(key, gc);
}
The first row seems to be pushed by the button that spans three rows.
Anyone know how to fix this?