JTable Swing:我无法通过在标题中拖动光标来调整列的大小 [英] JTable Swing: I cannot resize column by dragging cursor in header
问题描述
尽管我已将setResizingAllowed()
设置为true
,但是我无法通过在标题列之间移动光标并拖动分隔线来调整列宽的大小.
I cannot resize the column width by moving the cursor between header columns and drag the divider, despite of that I have set setResizingAllowed()
to true
.
tbResumen = new JTable();
tbResumen.setEnabled(false);
tbResumen.setRowHeight(20);
tbResumen.setBackground(UIManager.getColor("Table.selectionBackground"));
tbResumen.setBorder(new BevelBorder(BevelBorder.LOWERED, null, null, null, null));
tbResumen.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
tbResumen.setFont(new Font("Arial", Font.PLAIN, 12));
tbResumen.setDefaultRenderer(Object.class, new RenderTablaInfo());
tbResumen.setOpaque(false);
tbResumen.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
tbResumen.setColumnSelectionAllowed(false);
tbResumen.setRowSelectionAllowed(true);
tbResumen.setSize(new Dimension(840, 450));
tbResumen.setPreferredScrollableViewportSize(new Dimension(840, 450));
tbResumen.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
tbResumen.setModel(new DefaultTableModel(new Object[][] {{ label1, label2, label3, label4} },
new String[] { "foo", "bar", "foobar", "barfoo" }));
final JTableHeader headerResumen = new JTableHeader();
headerResumen.setReorderingAllowed(false);
headerResumen.setResizingAllowed(true);
headerResumen.setColumnModel(tbResumen.getColumnModel());
headerResumen.setTable(tbResumen);
tbResumen.setTableHeader(headerResumen);
如果有人需要更多代码来澄清这种情况,我会尽快添加.谢谢.
If someone needs more code to clarify the situation I'll add them soon. Thanks.
推荐答案
- 确保您的标头是您要配置的标头.
所以,用:
JTableHeader headerResumen = tbResumen.getTableHeader();
headerResumen.setResizingAllowed(true);
我只是使用与表相同的model
创建另一个标头,并在其中进行配置,但没有意识到我创建的新标头不是我的表.我必须调用table.getTableHeader()
来获取所需的标题,并且配置可以正常工作.很抱歉如此愚蠢.
I just create another header with the same model
of my table and do config there, not realizing that the new header I created is not that of my table. I must call table.getTableHeader()
to get the header I want and the config can work. Sorry for the stupidity.
-
确保将光标悬停在此表中列之间的边缘时将其设置为默认光标,以便可以看到它变成了双向箭头,并且您知道它们是可调整的.
Make sure you have set the cursor to be default cursor when hovering onto the edges between the columns in this table, so you can see it becomes a two-headed arrow and you are aware of that they are adjustable.
tbResumen.setCursor(Cursor.getPredefinedCursor(CURSOR_DEFAULT));
如果将其设置为CURSOR_HAND
,将不会显示双向光标.
If you set it to CURSOR_HAND
, no two-way cursor will not be shown.
-
autoResizingMode
也可以触发问题" ,这使得调整宽度变得更加困难.设置为OFF
时,拖动列边缘时,它可能会移动,但不能自由移动.确保将autoResizingMode
设置为JTable.AUTO_RESIZE_SUBSEQUENT_COLUMNS
或JTable.AUTO_RESIZE_NEXT_COLUMN
,以查看较大的更改.
autoResizingMode
can also trigger "problems", making it harder to adjust width. When it's setOFF
, when you drag column edge, it may be moved, but not freely. Be sure to set theautoResizingMode
toJTable.AUTO_RESIZE_SUBSEQUENT_COLUMNS
orJTable.AUTO_RESIZE_NEXT_COLUMN
to see considerable changes.
(半年后,当我回到我的答案时,我仍然发现它很有用...)
如果禁用了包含表的JScrollPane
,光标将不会更改,也不会显示双向光标.
(half a year later when I come back to my answer, I still find it useful...)
If you disable the JScrollPane
containing the table, the cursor will not change neither, and not two-way cursor will be shown.
这篇关于JTable Swing:我无法通过在标题中拖动光标来调整列的大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!