2
I’m trying to position some components on a screen, but it’s not working very well. So I created a kind of simple example to illustrate the problem. I accept subjects to perfect the way I’m doing it.
Observing: Whenever I add a panel inside a tab, I can’t get it near the edge of the tab, it always stays away.
package layout;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import javax.swing.BorderFactory;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;
import javax.swing.JTextField;
public class TesteLayout extends JFrame {
    public JPanel jpCampos = new JPanel();
    private final JTabbedPane tabbedPane = new JTabbedPane();
    private final MeuJTextField a = new MeuJTextField("");
    private final MeuJTextField b = new MeuJTextField("");
    private final MeuJTextField c = new MeuJTextField("");
    private final MeuJTextField d = new MeuJTextField("");
    private final MeuJTextField e = new MeuJTextField("");
    private final MeuJTextField aa = new MeuJTextField("");
    private final MeuJTextField bb = new MeuJTextField("");
    private final MeuJTextField cc = new MeuJTextField("");
    private final MeuJTextField dd = new MeuJTextField("");
    private final MeuJTextField ee = new MeuJTextField("");
    class MeuJTextField extends JTextField {
        public MeuJTextField(String nome) {
            //setColumns(120);
            setText("teste de layouts");
        }
    }
    public TesteLayout() {
        add(adicionaPaines());
        //setSize(600, 300);
        pack();
        setLocationRelativeTo(null);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
    }
    private JComponent adicionaPaines() {
        JPanel adicionaPaines = new JPanel();
        adicionaPaines.setLayout(new GridBagLayout());
        JPanel painel = new JPanel();
        JLabel label = new JLabel("Painel superior");
        painel.add(label);
        JPanel painel2 = new JPanel();
        JLabel label2 = new JLabel("Aqui vai um label");
        painel2.add(label2);
        label2.setForeground(new Color(99, 49, 131));
        adicionaComponente(1, 1, 1, 1, painel, adicionaPaines);
        adicionaComponente(2, 1, 1, 1, painel2, adicionaPaines);
        adicionaComponente(3, 1, 1, 1, adionaAba(), adicionaPaines);
        return adicionaPaines;
    }
    private JComponent adionaAba() {
        JPanel painelAbas = new JPanel();
        painelAbas.setLayout(new GridBagLayout());
        tabbedPane.addTab("Teste", tela());
        tabbedPane.setPreferredSize(new Dimension(550, 250));
        add(tabbedPane, BorderLayout.BEFORE_LINE_BEGINS);
        return painelAbas;
    }
    private JComponent tela() {
        JPanel tela = new JPanel();
        JPanel painel1 = new JPanel();
        painel1.setLayout(new GridBagLayout());
        painel1.setBorder(BorderFactory.createTitledBorder("Painel 01"));
        adicionaComponente(1, 1, 1, 1, painel1, tela);
        adicionaComponente(2, 1, 1, 1, a, painel1);
        adicionaComponente(3, 1, 1, 1, b, painel1);
        adicionaComponente(4, 1, 1, 1, c, painel1);
        adicionaComponente(4, 3, 1, 1, d, painel1);
        adicionaComponente(5, 1, 1, 1, e, painel1);
        JPanel painel2 = new JPanel();
        painel2.setLayout(new GridBagLayout());
        painel2.setBorder(BorderFactory.createTitledBorder("Painel 02"));
        adicionaComponente(1, 1, 1, 1, painel2, tela);
        adicionaComponente(2, 1, 1, 1, aa, painel2);
        adicionaComponente(3, 1, 1, 1, bb, painel2);
        adicionaComponente(4, 1, 1, 1, cc, painel2);
        adicionaComponente(4, 3, 1, 1, dd, painel2);
        adicionaComponente(5, 1, 1, 1, ee, painel2);
        return tela;
    }
    public void adicionaComponente(int linha, int coluna, int linhas, int colunas, JComponent componente, JPanel painel) {
        GridBagConstraints gbc = new GridBagConstraints();
        gbc.gridy = linha;
        gbc.gridx = coluna;
        gbc.gridheight = 1;
        gbc.gridwidth = 1;
        gbc.anchor = GridBagConstraints.EAST;
        gbc.insets = new Insets(5, 5, 5, 5); // ↑, ←, ↓, →
        if (painel == null) {
            painel = jpCampos;
        }
        gbc.anchor = GridBagConstraints.WEST;
        gbc.gridx++;
        gbc.gridheight = linhas;
        gbc.gridwidth = colunas;
        painel.add(componente, gbc);
    }
    public static void main(String[] args) {
        EventQueue.invokeLater(()
                -> {
            TesteLayout layout = new TesteLayout();
            layout.setVisible(true);
        });
    }
}
						

I don’t understand what the problem is. Is it positioning internally or the external panels? Because the code doesn’t look like in the image.
– user28595
@diegofm this is problem, I’m trying to do as in the picture, but I’m not getting.
– Royal Java
Need to be with gridbaglayout? You just chose the most complex of all.
– user28595
@diegofm if possible, this method of adding components saves me a lot when extending to other screens
– Royal Java
But is it mandatory? This layout I do not know much, I believe it is possible to do this mixing the others and become something simpler and easy to maintain(gridbaglayout is complicated for layout maintenance).
– user28595
@diegofm can do with another yes ! anything I try to do something similar to the add components, only that ai using other managers.
– Royal Java
Do you use this method to create other screens? This is a bad practice, each screen should be the only one responsible for you, using an external method to build another screen can cause you problems.
– user28595
I have a class, to add buttons and repeat other common methods between screens, this method is only in it, then each screen that inherits I just set the position. More to simplify I did this way in the example. So it is well oriented to objects.
– Royal Java