更改 JTable 中列的大小
我正在创建一个 GUI 程序,我需要一个具有不同大小列的表。如何更改它以使第一列小于第二列和第三列?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您需要获取
TableColumnModel
的句柄,然后您就可以设置各个列的宽度。例如:另外,这里有一些有用的 示例代码 展示了如何将给定列“打包”到足够宽以显示
JTable
中的所有值。我通常在 GUI 中使用此修改版本,并在添加第一行时“打包”表格 - 之后我允许用户控制宽度。You need to get a handle to the
TableColumnModel
, and from there you are able to set the individual column widths. For example: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.您读过 Swing 教程了吗?您所有的问题都是基本问题,并且都包含在教程中。
您会发现本教程有一个工作示例正是执行此操作。我们不必花时间提醒您在发布问题之前先阅读教程。
“授人以鱼,食终生。授人以鱼,食一日。”
(然后他们回来问另一个问题,这就是为什么用勺子喂答案不是一个好主意。)
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.)