我是MigLayout的入门者,因此,我需要在一个JPanel中添加多个JTables,但是当我尝试添加多个表时,只会显示最后一个表,而其他表仅被标记为JScrollPane边框。我的代码在下面。

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


如果我犯了一些错误,请纠正我。

谢谢!!

最佳答案

任何Swing组件只能有一个父级。在这里,您要将相同的JTable添加到2个不同的JScrollPane容器中。结果是仅显示最后一个。要显示2个JTable组件,您必须创建2个单独的组件。

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

09-27 02:39