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.
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?
– user28595
I can’t put above the table and centered, just next.
– JavaTech
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?
– user28595
Not that way I did not, in case it separates the buttons and the table?
– JavaTech