Positioning tabs on top of table using layout manager

2

I'm having problems positioning components. I have a table, and 3 buttons, and I'm trying to position the buttons above the table centrally.

Example:

ItriedtouseFlowLayout,andtheresultwasthis:

importjava.awt.Dimension;importjavax.swing.JButton;importjavax.swing.JComponent;importjavax.swing.JFrame;importjavax.swing.JPanel;importjavax.swing.JScrollPane;importjavax.swing.JTable;publicclassPosicionaTabelaextendsJFrame{publicPosicionaTabela(){Tabelatab=newTabela();add(tab);setSize(700,400);setDefaultCloseOperation(EXIT_ON_CLOSE);setLocationRelativeTo(null);}publicstaticvoidmain(String[]args){newPosicionaTabela().setVisible(true);}}classTabelaextendsJPanel{privateJScrollPanejsp=newJScrollPane();privateJTabletabela=newJTable();privateJButtonbotao1=newJButton("1");
    private JButton botao2 = new JButton("2");
    private JButton botao3 = new JButton("3");

    public Tabela() {
        confgTabela();
    }

    private JComponent confgTabela() {
        JPanel painel = new JPanel();
        jsp.setViewportView(tabela);
        jsp.setPreferredSize(new Dimension(400, 200));
        add(jsp);
        add(botao1);
        add(botao2);
        add(botao3);
        return painel;
    }
}
    
asked by anonymous 03.09.2017 / 02:58

2 answers

3

I got this in your code modifying as below:

import java.awt.BorderLayout;
import java.awt.Dimension;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;

public class PosicionaTabela extends JFrame {

    private JScrollPane jsp = new JScrollPane();

    private JTable tabela = new JTable();

    private JButton botao1 = new JButton("1");
    private JButton botao2 = new JButton("2");
    private JButton botao3 = new JButton("3");

    public PosicionaTabela() {

        getContentPane().add(getButtonsPanel(), BorderLayout.NORTH);
        getContentPane().add(getTabelaPanel(), BorderLayout.CENTER);
        setSize(700, 400);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setLocationRelativeTo(null);
    }

    private JPanel getTabelaPanel() {
        JPanel painel = new JPanel();
        jsp.setViewportView(tabela);
        jsp.setPreferredSize(new Dimension(400, 200));
        painel.add(jsp);
        return painel;
    }

    private JPanel getButtonsPanel(){       
        JPanel painel = new JPanel();
        painel.add(botao1);
        painel.add(botao2);
        painel.add(botao3);
        return painel;
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(() -> new PosicionaTabela().setVisible(true));
    }
}

There are some problems in the code, such as you create a JPanel and return it but do not add anything to it. I created a panel to return JScrollPane just to exemplify but it is neither necessary, since this class is already a container by itself and can be added directly to JFrame .

More information about Layout Managers can be found in the Official oracle Guide .

    
03.09.2017 / 03:08
3

I did so using a BorderLayout to help:

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.EventQueue;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;

public class PosicionaTabela extends JFrame {

    public PosicionaTabela() {
        Tabela tab = new Tabela();
        add(tab);
        setSize(700, 400);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setLocationRelativeTo(null);
    }

    public static void main(String[] args) {
        EventQueue.invokeLater(() -> new PosicionaTabela().setVisible(true));
    }
}

class Tabela extends JPanel {

    private JScrollPane jsp = new JScrollPane();

    private JTable tabela = new JTable();

    private JButton botao1 = new JButton("1");
    private JButton botao2 = new JButton("2");
    private JButton botao3 = new JButton("3");

    public Tabela() {
        JPanel painel = new JPanel();
        jsp.setViewportView(tabela);
        jsp.setPreferredSize(new Dimension(400, 200));
        this.setLayout(new BorderLayout());
        add(jsp, BorderLayout.CENTER);
        add(painel, BorderLayout.NORTH);
        painel.add(botao1);
        painel.add(botao2);
        painel.add(botao3);
    }
}

Here's the result:

AboutEventQueue.invokeLater(...),seemoreaboutthis on this other answer .

    
03.09.2017 / 03:08