JTable in JScrollPane fixed size with resizeable J

2020-05-09 01:32发布

问题:

I have a JTable inside of a JScrollPane. I want to get the columns to stay fixed when I resize it. The rows stay the same size, and there is a scrollbar to move up and down. I can't get the scrollbar to work the same way on the vertical though.

Here is a picture of my project, where the y axis of Duke is perfectly normal, and has a scrollbar to scroll to the bottom of the image, the horizontal part is clearly messed up, and should not have expanded that far.

Also, if the frame was made smaller horizontally, there should be a scrollbar just like the vertical.

So my question basically ends up like this; How do you fix the size of a JTable to not resize within the JScrollPane, and then if it's too large for it, display scroll bars.

Btw, Each cell has an image, making up the big image.

回答1:

Set the table's auto resize mode to "off"

table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);

Take a look at JTable#setAutoResizeMode for more details.

Update

I should mention, this will mean you will become responsible for determine the size of each column.

Take a look at How to use tables and Setting and Changing Column Widths