You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:
have access to post topics
communicate privately with other members (PM)
not see advertisements between posts
have the possibility to earn one of our surprises if you are an active member
access many other special features that will be introduced later.
I'm fairly new to building GUIs with Java. I'm trying to display the contents of a database table using a JTable object. The table has 13 columns. So I want the user to be able to scroll horizontally to see all the columns in the JTable object. Can anyone give me an example code segment that fits this scenario.
(All the data retrieval tasks are working fine. I just want the user to be able to scroll. The code I have written displays all the 13 columns in the viewable area of the JTable by reducing the column widths.)
Mount the JTable in a JScrollPane. Since JTable implements the Scrollable interface a parent scrollPane will have access to all the information it needs to properly show the table You can specify the preferred viewport size with the JTable setPreferredViewportSize method.
Code:
JTable table = yourTable
// provide a size hint
Dimension d = table.getPreferredSize();
d.width = as you like
d.height = ...
table.setPreferredScrollableViewportSize(d);
getContentPane().add(new JScrollPane(table));