user2144555
user2144555

Reputation: 1313

Swing: Place jTables and jButtons

I have a jFrame with 2 jTables (inserted in 2 jScrollPanes). Then, I have 3 jButtons for each jTable. How can I place them to have the following result:

enter image description here

I don't know very well what Layout to use to manage it. Thanks!


I have this, but I can't see the buttons:

JButton addButton1 = new JButton();           
JButton deleteButton1 = new JButton();
JButton playButton1 = new JButton();
JButton addButton2 = new JButton();           
JButton deleteButton2 = new JButton();
JButton playButton2 = new JButton();


JFrame frame = new JFrame();
frame.setLayout(new GridLayout(1, 2));

JPanel panel1 = new JPanel();
panel1.setLayout(new FlowLayout());
panel1.setBorder(javax.swing.BorderFactory.createTitledBorder("List 1"));
JPanel panel2 = new JPanel();
panel2.setLayout(new FlowLayout());
panel2.setBorder(javax.swing.BorderFactory.createTitledBorder("List 2"));    

JPanel panel3 = new JPanel();
panel3.setLayout(new FlowLayout());
panel3.add(addButton1);
panel3.add(deleteButton1);
panel3.add(playButton1);
JPanel panel4 = new JPanel();
panel4.setLayout(new FlowLayout());
panel4.add(addButton2);
panel4.add(deleteButton2);
panel4.add(playButton2);

JScrollPane tableContainer1 = new JScrollPane(table1);
panel1.add(tableContainer1, BorderLayout.CENTER);
JScrollPane tableContainer2 = new JScrollPane(table2);
panel2.add(tableContainer2, BorderLayout.CENTER);

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new FlowLayout());

frame.getContentPane().add(panel1);
frame.getContentPane().add(panel2);

frame.pack();   
frame.setLocationRelativeTo(null);
frame.setVisible(true);

Upvotes: 0

Views: 329

Answers (2)

DannyMo
DannyMo

Reputation: 11984

Edit: Gah! Someone beat me to it...Here's a slightly different approach regardless!

There's many ways to do this, but I would try the following:

  • JFrame > BoxLayout using X_AXIS
    • JPanel #1 > BorderLayout
      • [BorderLayout.NORTH] JPanel for buttons > FlowLayout using FlowLayout.LEFT
      • [BorderLayout.CENTER] JScrollPane with Table #1
    • JPanel #2 > BorderLayout
      • [BorderLayout.NORTH] JPanel for buttons > FlowLayout using FlowLayout.LEFT
      • [BorderLayout.CENTER] JScrollPane with Table #2

Using BoxLayout and BorderLayout.CENTER will ensure that the tables resize with the frame and fill up as much space as they can. Here's a simple example:

public class TwoTableJFrameTest extends JFrame
{
  public TwoTableJFrameTest()
  {
    setTitle("Two Table Layout");
    setLayout(new BoxLayout(getContentPane(), BoxLayout.X_AXIS));

    JPanel table1Panel = new JPanel(new BorderLayout(5, 5));

    JPanel table1ButtonPanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
    table1ButtonPanel.add(new JButton("Button 1"));
    table1ButtonPanel.add(new JButton("Button 2"));
    table1ButtonPanel.add(new JButton("Button 3"));

    JTable table1 = new JTable(new DefaultTableModel(new Object[]{"Column 1", "Column 2"}, 10));

    table1Panel.add(table1ButtonPanel, BorderLayout.NORTH);
    table1Panel.add(new JScrollPane(table1));

    JPanel table2Panel = new JPanel(new BorderLayout(5, 5));

    JPanel table2ButtonPanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
    table2ButtonPanel.add(new JButton("Button 1"));
    table2ButtonPanel.add(new JButton("Button 2"));
    table2ButtonPanel.add(new JButton("Button 3"));

    JTable table2 = new JTable(new DefaultTableModel(new Object[]{"Column 1", "Column 2"}, 10));

    table2Panel.add(table2ButtonPanel, BorderLayout.NORTH);
    table2Panel.add(new JScrollPane(table2));

    add(table1Panel);
    add(table2Panel);

    pack();
  }
}

two table layout

Upvotes: 4

Name
Name

Reputation: 2045

Steps:

  1. Give the main JFrame a GridLayout that has two columns and one row.
  2. Add to this JFrame 2 JPanels.
  3. These 2 panels will each have a FlowLayout Y-Axis BoxLayout.
  4. Add to each of these 2 panels a JPanel that holds your 3 buttons.
  5. Add the table to each of the 2 panels.

This is why you couldn't see the buttons, you forgot to do this:

panel1.add(panel3);
panel2.add(panel4);

Anyway, here's the working code:

JButton addButton1 = new JButton();
JButton deleteButton1 = new JButton();
JButton playButton1 = new JButton();
JButton addButton2 = new JButton();
JButton deleteButton2 = new JButton();
JButton playButton2 = new JButton();


JFrame frame = new JFrame();
frame.setLayout(new GridLayout(1, 2));

JPanel panel1 = new JPanel();
panel1.setLayout(new BoxLayout(panel1, BoxLayout.Y_AXIS));
panel1.setBorder(javax.swing.BorderFactory.createTitledBorder("List 1"));
JPanel panel2 = new JPanel();
panel2.setLayout(new BoxLayout(panel2, BoxLayout.Y_AXIS));
panel2.setBorder(javax.swing.BorderFactory.createTitledBorder("List 2"));

JPanel panel3 = new JPanel();
panel3.setLayout(new FlowLayout());
panel3.add(addButton1);
panel3.add(deleteButton1);
panel3.add(playButton1);
JPanel panel4 = new JPanel();
panel4.setLayout(new FlowLayout());
panel4.add(addButton2);
panel4.add(deleteButton2);
panel4.add(playButton2);

panel1.add(panel3);
panel2.add(panel4);

JScrollPane tableContainer1 = new JScrollPane(table1);
panel1.add(tableContainer1, BorderLayout.CENTER);
JScrollPane tableContainer2 = new JScrollPane(table2);
panel2.add(tableContainer2, BorderLayout.CENTER);



frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new FlowLayout());

frame.getContentPane().add(panel1);
frame.getContentPane().add(panel2);

frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);

P.S.

You'll notice I changed the layout of panel1 & panel2 from FlowLayout to a Y-Axis BoxLayout. This is because the buttons appeared beside the tables, not above. Changing the layout to a Y-Axis BoxLayout fixed that.

Upvotes: 3

Related Questions