Reputation:
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);
Upvotes: 0
Views: 210
Reputation: 17971
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.
Upvotes: 0
Reputation: 359
guestFixturesPanel.add(listTable);
is wrong ! you must add the scroll like this:
guestFixturesPanel.add(scroll);
Upvotes: 1