Search code examples
javaswingintellij-ideajtable

Trying to add two tables into one JFrame, but only one appears


I'm trying to add two tables into my frame, but I only get one. I tried to use different positions in BorderLayouts, but still don't get the final result. My code is below:

     private JFrame f = new JFrame("List of cars");

    //           [SIZE]
    f.setSize(700, 600);

    //           [TABLE]
    DefaultTableModel model = new DefaultTableModel();
    JTable table = new JTable(model);

    model.addColumn("GROUP 1");

    table.setPreferredScrollableViewportSize(new Dimension(30, 20));

    JScrollPane jScrollPane1 = new JScrollPane(table);

    JPanel listPahel = new JPanel();
    listPahel.setLayout(new BorderLayout());
    listPahel.add(jScrollPane1, BorderLayout.CENTER);
    listPahel.setBorder(BorderFactory.createEmptyBorder(50, 10, 400, 500));
    listPahel.validate();

    //-----------------

    DefaultTableModel model2 = new DefaultTableModel();
    JTable table2 = new JTable(model2);

    model2.addColumn("GROUP 2");

    table2.setPreferredScrollableViewportSize(new Dimension(30, 20));

    JScrollPane jScrollPane2 = new JScrollPane(table2);

    JPanel listPahel2 = new JPanel();
    listPahel2.setLayout(new BorderLayout());
    listPahel2.add(jScrollPane2, BorderLayout.SOUTH);
    listPahel2.setBorder(BorderFactory.createEmptyBorder(200, 20, 20, 20));
    listPahel2.validate();

    f.add(listPahel);
    f.add(listPahel2);


    f.setVisible(true);

I always get the second table, but I need to get both.


Solution

  • Oracle has a helpful tutorial, Creating a GUI With Swing. Skip the Learning Swing with the NetBeans IDE section. Pay particular attention to the Laying Out Components Within a Container section.

    You didn't say how you wanted the JTables arranged on your page, so I put them side by side. Here's the example GUI I came up with.

    Example

    All Swing applications must start with a call to the SwingUtilities invokeLater method. This method ensures that all Swing components are created and executed on the Event Dispatch Thread.

    I separated the creation of the JFrame from the creation of the two JPanels that hold the JTables. The JFrame has a default BorderLayout. I defined each of the two JPanels to have a BorderLayout.

    The two JPanels each have a JScrollPane placed in the CENTER of their BorderLayout.

    The two JPanels are placed in the WEST and EAST of the JFrame BorderLayout.

    Here's the complete runnable code.

    import java.awt.BorderLayout;
    
    import javax.swing.BorderFactory;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import javax.swing.JScrollPane;
    import javax.swing.JTable;
    import javax.swing.SwingUtilities;
    import javax.swing.table.DefaultTableModel;
    
    public class TwoJTablesGUI implements Runnable {
    
        public static void main(String[] args) {
            SwingUtilities.invokeLater(new TwoJTablesGUI());
        }
    
        @Override
        public void run() {
            JFrame frame = new JFrame("Two JTables GUI");
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    
            frame.add(createJTable1Panel(), BorderLayout.WEST);
            frame.add(createJTable2Panel(), BorderLayout.EAST);
    
            frame.pack();
            frame.setLocationByPlatform(true);
            frame.setVisible(true);
        }
    
        private JPanel createJTable1Panel() {
            JPanel panel = new JPanel(new BorderLayout());
            panel.setBorder(BorderFactory.createEmptyBorder(0, 5, 5, 5));
    
            DefaultTableModel model = new DefaultTableModel();
            model.addColumn("GROUP 1");
    
            JTable table = new JTable(model);
    
            JScrollPane scrollPane = new JScrollPane(table);
            panel.add(scrollPane, BorderLayout.CENTER);
    
            return panel;
        }
    
        private JPanel createJTable2Panel() {
            JPanel panel = new JPanel(new BorderLayout());
            panel.setBorder(BorderFactory.createEmptyBorder(0, 5, 5, 5));
    
            DefaultTableModel model = new DefaultTableModel();
            model.addColumn("GROUP 2");
    
            JTable table = new JTable(model);
    
            JScrollPane scrollPane = new JScrollPane(table);
            panel.add(scrollPane, BorderLayout.CENTER);
    
            return panel;
        }
    
    }