Positioning of components using Gridbaglayout

Asked

Viewed 2,686 times

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.

1 answer

2


Running your code got this:

inserir a descrição da imagem aqui

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 my container.

A simple solution is to play your Gridbaglayout inside a Borderlayout by choosing the EAST position, also known as LINE_END.

inserir a descrição da imagem aqui

Basically your code was like this:

contentPane = createActionButtonPanel();
setContentPane(contentPane);    

where the Panel created by its method was the son of Jframe. To make Borderlayout be the son of Jframe and father of Gridbaglyout do so:

contentPane = new JPanel(new BorderLayout());
setContentPane(contentPane);
contentPane.add(createActionButtonPanel(), BorderLayout.EAST);

You will get it:

inserir a descrição da imagem aqui

I’m searching to learn how to use Gridbaglayout, so if I’m making a primary mistake let me know.

Basically what you have to do is know the time to use the best layout for each occasion, and in many occasions you will need to use a composition of layouts.

I advise you to start studying here: A Visual Guide to Layout Managers

  • Excellent reply. Thank you very much for your help. I will read this article you indicated. Thank you very much.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.