JTable不显示列

时间:2014-03-15 18:32:58

标签: java swing layout jtable

我的JTabel上的列没有出现:/ 我无法找到原因,因为我的滚动条没有显示出来。作为新手,我无法找到原因。

这是我的代码:

 String [] columnNames = {"From", "Depature","To","Arrvial",};
     Object[][] data =  {
            {"Orlando, FL", "3/15/2014 @ 8:00 AM", "Atlanta, GA", "3/15/2014 @ 9:05 AM"},
            {"Atlanta, GA", "3/16/2014 @ 11:49 AM", "Chicago, IL", "3/16/2014 @ 1:05 PM"},
            {"Chicago, IL", "3/17/2014 @ 1:25 PM", "San Francisco, CA", "3/17/2014 @ 6:05 PM"},
            {"San Francisco, CA", "3/18/2014 @ 2:10 PM", "Seattle, WA", "3/18/2014 @ 5:35 PM"},
            {"Seattle, WA", "3/19/2014 @ 4:35 PM", "Atlanta, GA", "3/19/2014 @ 10:45 PM"},
            {"Orlando, FL", "3/15/2014 @ 8:00 AM", "Atlanta, GA", "3/15/2014 @ 9:05 AM"},
            {"Atlanta, GA", "3/16/2014 @ 11:49 AM", "Chicago, IL", "3/16/2014 @ 1:05 PM"},
            {"Chicago, IL", "3/17/2014 @ 1:25 PM", "San Francisco, CA", "3/17/2014 @ 6:05 PM"},
            {"San Francisco, CA", "3/18/2014 @ 2:10 PM", "Seattle, WA", "3/18/2014 @ 5:35 PM"},
            {"Seattle, WA", "3/19/2014 @ 4:35 PM", "Atlanta, GA", "3/19/2014 @ 10:45 PM"},
            {"Orlando, FL", "3/15/2014 @ 8:00 AM", "Atlanta, GA", "3/15/2014 @ 9:05 AM"},
            {"Atlanta, GA", "3/16/2014 @ 11:49 AM", "Chicago, IL", "3/16/2014 @ 1:05 PM"},
            {"Chicago, IL", "3/17/2014 @ 1:25 PM", "San Francisco, CA", "3/17/2014 @ 6:05 PM"},
            {"San Francisco, CA", "3/18/2014 @ 2:10 PM", "Seattle, WA", "3/18/2014 @ 5:35 PM"},
            {"Seattle, WA", "3/19/2014 @ 4:35 PM", "Atlanta, GA", "3/19/2014 @ 10:45 PM"},
            {"Orlando, FL", "3/15/2014 @ 8:00 AM", "Atlanta, GA", "3/15/2014 @ 9:05 AM"},
            {"Atlanta, GA", "3/16/2014 @ 11:49 AM", "Chicago, IL", "3/16/2014 @ 1:05 PM"},
            {"Chicago, IL", "3/17/2014 @ 1:25 PM", "San Francisco, CA", "3/17/2014 @ 6:05 PM"},
            {"San Francisco, CA", "3/18/2014 @ 2:10 PM", "Seattle, WA", "3/18/2014 @ 5:35 PM"},
            {"Seattle, WA", "3/19/2014 @ 4:35 PM", "Atlanta, GA", "3/19/2014 @ 10:45 PM"},
        };


     flightTable = new JTable(data, columnNames);
     scrollPane = new JScrollPane(flightTable);
      borderLayout = new BorderLayout();
     setLayout(borderLayout);
     add(scrollPane, BorderLayout.CENTER);
     flightTable.setFillsViewportHeight(true);
     add(flightTable, BorderLayout.CENTER);

2 个答案:

答案 0 :(得分:6)

组件只能有一个父容器。删除替换scrollPane组件

的此语句
add(flightTable, BorderLayout.CENTER);

答案 1 :(得分:0)

您必须将jtable添加到scrollpanes视口。然后将scrollPane添加到面板中。

table = new JTable(columns, data);
table.setFillsViewportHeight(true);
scrollPane.setViewportView(table);
this.add(scrollPane);