noobProgrammer
noobProgrammer

Reputation: 477

Using JTable - specifying size

I am using a JTable in a program. The problem is that when I set the size of the whole JTable, I am using this method:

setPreferredScrollableViewportSize(Dimension size)

The question I have is that when using this method, you enter in the width and length in pixels. Will there be issues (formatting-wise) when the program is run on different computers/machines?

Upvotes: 0

Views: 53

Answers (1)

MadProgrammer
MadProgrammer

Reputation: 347334

Should I avoid the use of set(Preferred|Maximum|Minimum)Size methods in Java Swing? - Short answer is yes (you should avoid it)

When setting the preferredSize of the table, you are not actually taking into account the number of rows or columns which the table might have, depending on the layout manager, it may NEVER be larger than the dimensions you provide, regardless of the amount of data

The question I have is that when using this method, you enter in the width and length in pixels. Will there be issues (formatting-wise) when the program is run on different computers/machines?

Generally, yes.

JTable was designed to be shown within a JScrollPane, which allows you some leeway with this, as the JTable can grow and shrink, within the JScrollPane, but the JScrollPane can remain the same.

Having said that, you should NOT be using setPreferredSize and especially not messing with JTables, JTable calculates it's own preferred size based on the needs of the data...and it does a reasonably good job.

If you want to change the size the JScrollPane, then you will need to change the result of getPreferredScrollableViewportSize which lets the JScrollPane know how big a component would like the basic viewable area to be, under optimal conditions

See How to Use Tables and How to Use Scroll Panes

Upvotes: 1

Related Questions