3
I’m making a panel and I want to have the following layout:
The width of the panel takes up the entire width of the frame, and I want to have 4 buttons in the right corner of that panel.
I created a panel like container, and within that panel I created another panel with the 4 buttons. The idea is that this panel with the buttons be in the right corner of the "container panel".
I managed to create the panel with the buttons with the layout I want, but I can’t put it in the right corner of mine container.
Follows my code:
/**
* @return {@link JPanel} com botões de ação.
*/
private JPanel createActionButtonPanel() {
JPanel buttonPanel = new JPanel();
GridBagLayout grid = new GridBagLayout();
GridBagConstraints cons = new GridBagConstraints();
cons.fill = GridBagConstraints.NONE;
cons.insets = new Insets(1,1,1,1);
cons.gridy = 0;
buttonPanel.setBackground(backgroundColor);
buttonPanel.setLayout(grid);
cons.gridx = 0;
buttonPanel.add(new JButton("Pesquisar"), cons);
cons.gridx = 1;
buttonPanel.add(new JButton("Novo"), cons);
cons.gridx = 2;
buttonPanel.add(new JButton("Editar"), cons);
cons.gridx = 3;
buttonPanel.add(new JButton("Excluir"),cons);
JPanel panel = new JPanel();
panel.setBackground(backgroundColor);
cons.anchor = GridBagConstraints.EAST;
cons.fill = GridBagConstraints.NONE;
cons.gridwidth = 1;
panel.setLayout(grid);
panel.add(buttonPanel,cons);
return panel;
}
Making this way the panel with the buttons stay centered.
I’m digging to learn how to use the GridBagLayout
, then I may be making some primary mistake.
Excellent reply. Thank you very much for your help. I will read this article you indicated. Thank you very much.
– user8078