Positioning components vertically with GridBagLayout

1

I created a java code to position objects in the window of type JComboBox and JTextField and I want them to be one underneath the 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);

        });
}
}
    
asked by anonymous 21.10.2017 / 22:46

1 answer

1

Update

I found that positioning with gridX and gridY is not necessary to position 2 components vertically. Just 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);
        });
    }
}

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

Just by reading the link that I had passed 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, you can define where each component will stay, how many cells of the grid it will occupy, among other details explained in the link, and with gridx and gridy you define the "coordinates" of the component in the grid, where the first represents the horizontal position (column) and the second the vertical (line) position of the component in 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 begins with a capital letter , creator does not follow the java convention.

    
21.10.2017 / 23:02