Question

I'm beeginer in MigLayout so, I need to add multiples JTables in one JPanel, but when I try to add more than one table, just the last table appears, and the others is marked just the JScrollPane border. My code is in below.

 Test() {
    //Panels
    JPanel globalPanel = new JPanel(new MigLayout("fillx","[]","[]50[]"));
    JPanel topPanel = new JPanel (new MigLayout("fillx","40px[]15[grow]","40px[]"));
    JPanel tablePanel = new JPanel (new MigLayout("fillx","[center]","[]"));
    //Components
    JComboBox boxProj;
    JTable table;
    JScrollPane scroll;


    //Top Panel        
    topPanel.add(new JLabel("Project Name:"));
    String listString[] = {"test"};
    boxProj= new JComboBox(listString);
    topPanel.add(boxProj);

    //Table Panel
    //Tables
    table = new JTable();
    createTable(table); //my table

    //Adding Multiples Tables
    tablePanel.add( new JScrollPane(table),"growx,wrap,hmax 300");
    tablePanel.add( new JScrollPane(table),"growx,wrap,hmax 300");

    //Scroll to TablePanel
    scroll = new JScrollPane(tablePanel);
    scroll.setBorder(BorderFactory.createTitledBorder(null, "Project", TitledBorder.LEFT, TitledBorder.TOP, new Font("null", Font.BOLD, 12), Color.BLACK));
    scroll.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);

    //Global Panel
    globalPanel.add(topPanel, "dock north");
    JSeparator separator = new JSeparator();
    globalPanel.add(separator,"growx");
    globalPanel.add(scroll,"dock south, growx");

    getContentPane().add(globalPanel);
    pack();
    setSize(1024,768);
}

If I made some mistake, correct me please.

Thank you!!

Was it helpful?

Solution

Any Swing component can only have one parent. Here you are adding the same JTable to 2 different JScrollPane containers. The result is that only the last one will be displayed. For 2 JTable components to appear you have to create 2 separate components.

table2 = new JTable();
...
tablePanel.add(new JScrollPane(table2), "growx,wrap,hmax 300");  

OTHER TIPS

It would seem that you are trying to add the same component twice. You can only have a component visible in one container:

table = new JTable(); createTable(table); //my table

//Adding Multiples Tables
tablePanel.add( new JScrollPane(table),"growx,wrap,hmax 300");
tablePanel.add( new JScrollPane(table),"growx,wrap,hmax 300");

Try with:

JTable table1 = new JTable();
JTable table2 = new JTable();
createTable(table1); //my table
createTable(table2);

//Adding Multiples Tables
tablePanel.add( new JScrollPane(table1),"growx,wrap,hmax 300");
tablePanel.add( new JScrollPane(table2),"growx,wrap,hmax 300");
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top