简体   繁体   中英

How to make a scrollable JTable

Question 1) I have a JPanel ps, which has a null layout; a JScrolPane scrollPane, and a JTable table. I have added the table to the scrollPane, and then added the scrollPane to the Jpanel ps. The following is my code for doing that:-

ps = new JPanel();
ps.setLayout(null);
ps.setSize(1000,600);

scrollPane = new JScrollPane();
scrollPane.setBounds(10, 119, 975, 300);
        scrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
ps.add(scrollPane);

table = new JTable(data, columns);
table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
scrollPane.setViewportView(table);

It is supposed t create a scrollPane with horizontal scrollbars, and when the number of columns increases in such a way that, it cannot be handled on a single screen, the scrollbar helps to display the contents. See "AUTO_RESIZE_OFF".

But what happens is that, the the size of all the columns is fixed (I don't know how many characters. The width of all of them is same.). The name of the columns is thus not fully viewable.

My question is, is it possible to increase the width of the column shown, or to show all the columns with their own width ? Also, how to enable scrolling ?

  1. Yes. See TableColumn . Generally what I've done in the past is used the ResultSetMeta data to build the column model. Take a look at How to use Tables for more information
  2. Yes, but JTable can be a little trouble some. Take a look at Printing and Printing a JTable . There are some great examples on SO if you have the time to look.
  3. It's always better to filter the data at the database end. Once you have a set of results, of you wanted to filter that set, you could use its ResultSetMetaData to determine which columns you want to group by. Equally, if you don't have results yet, you could use the DatabaseMetaData to determine the columns of the tables in the database
  1. Regarding to your print job in question 1 & 2: I recommend you should have a look at jasper report
  2. For your question 3, I do not know what your database structure is, so it is hard to tell, but look like what you are looking for is row span in JTable. So, try to look around google for such thing

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM