Position components vertically with Gridbaglayout

Asked

Viewed 229 times

1

I created code in java to position objects in the type window JComboBox and JTextField and I want them to be below each other. The code I tried is this:

/*Biblioteca */
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.text.*;

class creator {

public static void main(String args[]){

 EventQueue.invokeLater(() -> {

            /*Cria o layout*/
            GridBagLayout layout = new GridBagLayout();
            GridBagConstraints c = new GridBagConstraints();

            /* Gera os campos, tela, e configurações */
            JFrame frame = new JFrame();
            JPanel panel = new JPanel(layout);
            JTextField texto = new JTextField();
            texto.setColumns(15);
            JComboBox combo = new JComboBox();

            /* Cria as opções e configurações do JComboBox */
            combo.setBackground(Color.WHITE);
            combo.addItem("op\u00e7\u00e3o1");
            combo.addItem("op\u00e7\u00e3o2");
            combo.addItem("op\u00e7\u00e3o3");
            combo.addItem("op\u00e7\u00e3o4");
            combo.addItem("op\u00e7\u00e3o5");
            combo.addItem("op\u00e7\u00e3o6");
            combo.addItem("op\u00e7\u00e3o7");

            /* Adiciona as coisas na tela */
            panel.add(texto);
            panel.add(combo, c);

            /* Configurações da janela */
            frame.setTitle("Layout");
            frame.setSize(500, 500);
            frame.getContentPane().add(panel);

            /*seta como o arquivo fecha e sua visibilidade */
            frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
            frame.setVisible(true);

        });
}
}
  • It’s not the same doubt as the other? - quero deixar eles no meio da janela sem estarem grudados na borda da janela e um no outro e sem que fiquem pegando a janela inteira

  • no and guy I managed to apply the layout but I could not leave the buttons one below the other and I have researched in thousands of places and ngm explains how it does this.

  • How are you not? I made your example rightly basing de la, everything you need to understand this layout has la.

  • I understood only that the buttons don’t stay under each other.

  • But it doesn’t mention any of this in the link. It explains how to use this layout.

  • then that’s why I created this topic to know if it is possible using this layout to leave 1 object below the other.

Show 1 more comment

1 answer

1


Updating

I found that it is not necessary to inform the positioning with gridX and gridY to position 2 components vertically. Simply change the class as below:

import java.awt.*;
import javax.swing.*;

public class PosicaoDeCompTest {

    public static void main(String args[]) {

        EventQueue.invokeLater(() -> {

            JFrame frame = new JFrame();
            JPanel panel = new JPanel(new GridBagLayout());

            GridBagConstraints gbc1 = new GridBagConstraints();
            gbc1.gridwidth = GridBagConstraints.REMAINDER;
            JTextField texto = new JTextField();
            texto.setColumns(10);
            panel.add(texto, gbc1);

            JComboBox combo = new JComboBox();
            combo.setBackground(Color.WHITE);
            combo.addItem("opção1");
            combo.addItem("opção2");
            combo.addItem("opção3");
            panel.add(combo);

            frame.setSize(500, 500);
            frame.getContentPane().add(panel);

            frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
            frame.setVisible(true);
        });
    }
}

The gridwidth serves to define how many cells the component needs to be displayed on that line, and when we pass the constant GridBagConstraints.REMAINDER, We are defining that this component should be the last of the line. In the above code, this change forces the layout to play what comes after the text field on the next line, so that the combo can be added directly (without setting an instance ofGridBagConstraints).


Just reading the link that had passed to him I was able to modify the code to display the elements vertically:

import java.awt.*;
import javax.swing.*;

public class PosicaoDeCompTest {

    public static void main(String args[]) {

        EventQueue.invokeLater(() -> {

            JFrame frame = new JFrame();
            JPanel panel = new JPanel(new GridBagLayout());

            GridBagConstraints gbc1 = new GridBagConstraints();
            gbc1.gridx = 0;
            gbc1.gridy = 0;
            JTextField texto = new JTextField();
            texto.setColumns(10);
            panel.add(texto, gbc1);

            JComboBox combo = new JComboBox();
            combo.setBackground(Color.WHITE);
            combo.addItem("opção1");
            combo.addItem("opção2");
            combo.addItem("opção3");
            GridBagConstraints gbc2 = new GridBagConstraints();
            gbc2.gridx = 0;
            gbc2.gridy = 1;
            panel.add(combo, gbc2);


            frame.setSize(500, 500);
            frame.getContentPane().add(panel);

            frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
            frame.setVisible(true);
        });
    }
}

Highlighting what is explained in the documentation:

gridx, gridy
Specify the Row and column at the upper left of the Component. The leftmost column has address gridx=0 and the top Row has address gridy=0

As this layout works as a grid, it is possible to define where each component will be, how many grid cells it will occupy, among other details explained in the link, and with gridx and gridy you define the "coordinates" of the component on the grid, where the first represents the horizontal position (column) and the second the vertical position (line) of the component on the grid. By changing the default position of the combo line to 1, it is already displayed below the text field.

Another detail: class name starts with uppercase letter, creator does not follow the java convention.

  • That’s what I was trying to make you leave the way you left one object underneath the other.

Browser other questions tagged

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