0

I'm trying to add a table to my program that has the column names at the top and a scroll bar down the side. For some odd reason everything works, but the column names don't show nor does the scroll bar.

Here's my code, if you need a running program let me know, but you should be able to just add this to an empty JFrame:

String[] columns = {"Sport", "Location", "Date", "Result"};
    String[][] data = {{"Football", "AQA Highschool", "12.11.13", "5 - 0"},
            {"Tennis", "Wembley", "26.11.14.", "TBC"}};

listTable = new JTable(data, columns);
            listTable.setPreferredScrollableViewportSize(new Dimension(450, 750));
            listTable.setFillsViewportHeight(false);
            listTable.setBounds(25, 100, 450, 640);
            JScrollPane scroll = new JScrollPane(listTable);
            guestFixturesPanel.add(listTable);

2 Answers2

1
guestFixturesPanel.add(listTable);

is wrong ! you must add the scroll like this:

guestFixturesPanel.add(scroll);
giovybus
  • 349
  • 1
  • 3
  • 10
  • +1, You should NOT be using listTable.setBounds(). Also, make sure you are NOT using a null layout on the guestFixturesPanel. Swing was designed to be used with layout managers NOT a null layout. The scrollpane won't work if you use null layouts. – camickr Feb 21 '14 at 21:11
  • I'm using CardLayout but don't know what is best for the panels them selves. I tried Grid but didn't like the way I can't choose where the components go without placing "Ghost" components. –  Feb 21 '14 at 21:13
  • @HarryKitchener, Typically you don't add a JScrollPane to a panel using a CardLayout. You add another panel that contains the scrollpane to the panel using the CardLayout. A BordLayout sounds like the best bet (I see no reason for a CardLayout). Create a panel for the components to be placed ad the top and add this panel to the NORTH. Then add the scrollpane to the CENTER. Again read the tutorial, you don't appear to understand layout managers. – camickr Feb 21 '14 at 21:17
  • Ok, so I changed it all to BorderLayout, but when I try to position a component it doesn't move it: `guestFixturesPanel.add(scroll, BorderLayout.PAGE_END);` –  Feb 21 '14 at 21:24
0

For some odd reason everything works, but the column names don't show nor does the scroll bar.

First of all you're not adding the scroll pane but the table directly to guestFixturesPanel:

guestFixturesPanel.add(listTable);

Second, you must avoid the use of setBounds() method and use a suitable LayoutManager instead. Take a look to A visual guide to Layout Managers. You might also consider third party options suggested here. As @AndrewThompson always says:

Java GUIs might have to work on a number of platforms, on different screen resolutions & using different PLAFs. As such they are not conducive to exact placement of components. To organize the components for a robust GUI, instead use layout managers, or combinations of them, along with layout padding & borders for white space.

Community
  • 1
  • 1
dic19
  • 17,821
  • 6
  • 40
  • 69
  • Well I'm using CardLayout but what is the best layout to use for each panel, grid? I tried grid but I don't like the idea of nopt being able to choose where the components go. –  Feb 21 '14 at 21:12
  • To deal with layout managers you might consider use a combination of them as exemplified here: [Nested Layout Example](http://stackoverflow.com/questions/5621338/how-to-add-jtable-in-jpanel/5630271#5630271). It depends on the components you'll have on each panel but you always have a way to achieve the positioning you want without using `setBounds()`. @HarryKitchener – dic19 Feb 21 '14 at 21:17