Position Buttons Scaled Table Using Layouts Manager

Asked

Viewed 161 times

2

I’m having trouble positioning components. I have a table, and 3 buttons, and I’m trying to place the buttons above the table centrally.

Example: inserir a descrição da imagem aqui

I tried to use the FlowLayout, and the result was this:

import java.awt.Dimension;
import javax.swing.JButton;
import javax.swing.JComponent;
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) {
        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() {
        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;
    }
}
  • And what is the difficulty?

  • I can’t put above the table and centered, just next.

  • Just seeing the picture without even analyzing its code, I see that it is possible to do this in a simple way, merging only 2 layouts: borderlayout and flowlayout, with 2 panels. Have you ever tried to merge like this?

  • Not that way I did not, in case it separates the buttons and the table?

2 answers

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);
    }
}

That is the result:

resultado

About the EventQueue.invokeLater(...), see more about this in that other answer.

3


I got this in your code by 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, as you create a JPanel and return it but add nothing to it. I created a panel to return the JScrollPane just to illustrate but it is not even necessary, since this class is already a container by itself and can be added directly to the JFrame.

More information about Layout Managers can be found on Official guide to the oracle.

Browser other questions tagged

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