user3026473
user3026473

Reputation: 23

JTable does not show columns

The columns on my JTabel arnt showing up :/ I cant find out why, aslo, my scrollbar isnt showing up. Being novice I cant find out why.

Here is my code:

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

Upvotes: 0

Views: 48

Answers (2)

PeterYoshi
PeterYoshi

Reputation: 122

you have to add the jtable to the scrollpanes viewport. then add the scrollPane to your panel.

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

Upvotes: 0

Reimeus
Reimeus

Reputation: 159864

A component can only have one parent container. Remove this statement which is displacing the scrollPane component

add(flightTable, BorderLayout.CENTER);

Upvotes: 6

Related Questions