开发者

Changing the size of columns in a JTable

I am creating a GUI program and I need a table with different sized columns. How do I开发者_如何学编程 change it so that the 1st column is smaller than the 2nd and 3rd column?


You need to get a handle to the TableColumnModel, and from there you are able to set the individual column widths. For example:

JTable tbl = new JTable();
TableColumnModel colMdl = tbl.getColumnModel();
colMdl.getColumn(0).setPreferredWidth(100);
colMdl.getColumn(1).setPreferredWidth(150);
colMdl.getColumn(2).setPreferredWidth(150);

Also, here's some useful example code showing how to "pack" a given column to be wide enough to show all values in the JTable. I typically use a modified version of this in my GUIs and will "pack" the table when the first row is added to it - After this I allow the user to control the widths.


Have you read the Swing tutorial yet? All you questions are basic and are covered in the tutorial.

You will find the tutorial has a working example that does exactly this. We should not have to spend time reminding you to read the tutorial FIRST before posting a question.

"Teach somebody to fish and they eat for life. Give somebody a fish and they eat for a day."

(and then they come back and ask another question, which is why spoonfeeding answers is not a good idea.)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜