Java 行为 - JTable 和 TableCellRenderer
我有一个自定义 JTable(15 行 x 20 列),它是为所有 JComponent 工作而创建的。我目前将它用于 JComboBox、JTextField 和 JButton 的混合。哦,我正在使用 Java5(必需)。
我有两个问题:
1)第一个问题涉及 TableCellRenderer 类及其单一方法:
public Component getTableCellRendererComponent(final JTable table,
final Object value,
final boolean isSelected,
final boolean hasFocus,
final int row,
final int column) {...}
它所做的就是将 Object value
参数强制转换为 JComponent,并可能更改背景颜色。没什么大不了的。我的问题是为什么这个方法被如此频繁地调用。选择单个单元格时,调用23次。当在两个不相关的应用程序之间使用 Alt-Tabbing 时(我使用 Win7),此方法被调用超过 200 次(并且仅适用于 JButtons 和 JTextFields)! 这是否有必要?如果没有,我怎样才能阻止不必要的渲染调用?
2)第二个问题是关于JTable本身的。当我编辑一个单元格(JTextField 中的光标并闪烁)并单击另一个单元格时,仅选择该单元格。但是,如果我单击另一个单元格,我就会开始编辑该单元格。我所能想到的是,从最初的编辑组件中,我选择 JTable,然后选择其中的组件。有什么办法可以改变这个吗?要么采用一种方式(始终在第一次单击时选择 jtable),要么采用另一种方式(始终在第一次单击时输入单元格)。如果可能的话,我更喜欢第一个选择。
感谢任何可以提供一些见解/帮助的人!
I have a custom JTable (15 rows by 20 cols) that was created to work for all JComponents. I'm currently using it for a mixture of JComboBoxes, JTextFields, and JButtons. Oh, and I'm using Java5 (a requirement).
I have two questions:
1) The first regards the TableCellRenderer class, and its single method:
public Component getTableCellRendererComponent(final JTable table,
final Object value,
final boolean isSelected,
final boolean hasFocus,
final int row,
final int column) {...}
All it does is cast the Object value
argument to a JComponent, and potentially change the background color. No big deal. The question I have is why is this method is called sooooo often. When selecting a single cell, it is called 23 times. When Alt-Tabbing between two UNRELATED applications (I use Win7), this method is called over 200 times (and only for JButtons and JTextFields)!
Is this in any way necessary, and if not, how can I put a stop to unnecessary rendering calls?
2) The second question regards the JTable itself. When I'm editing a cell (cursor in a JTextField and blinking) and I click on another cell, that cell is only selected. If I then click on another cell, however, I start editing that cell. All I can think is that from the initial editing component, I'm selecting the JTable, and then selecting the component within. Is there any way to change this? Either going one way (always selects the jtable on first click) or the other (always enters cell on first click). I would prefer the first option, if possible.
Thansk to anyone who can grant some insight/help!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
1)为什么要将组件存储在TableModel中?这效率不高。您应该将数据存储在 TableModel 中。 JTable 旨在使用单个组件呈现数据。默认渲染器通常是 JLabel。然后,当您编辑单元格时,将使用不同的组件。
当您单击某个单元格时,您可能需要重新渲染前一行(以删除行选择),然后使用所选内容渲染当前行。因此,每个受影响的可见单元都会调用渲染器。按 Tab 键切换到应用程序可能会导致所有可见单元格重新渲染。
2)很难回答,因为这不是默认行为。您还必须使用自定义编辑器,但我不知道您的自定义代码是什么样的。默认编辑器有一个 setClickCountToStart() 方法,默认为 2。也许您将其设置为 1。
更改您的解决方案以使用 TableModel 中的数据(而不是组件)并发布您的 SSCCE。
1) Why are you storing Components in the TableModel? That is not efficient. You should be storing data in the TableModel. JTable was designed to render data using a single component. The default renderer is generally a JLabel. Then when you edit a cell a different component is used.
When you click on a cell you may need to re-render the previous row (to remove the row selection) and then render the current row with the selection. So the renderer is called for each visible cell that is affected. Tabbing to the application probably causes all the visible cells to be re-rendererd.
2) Hard to answer since this is not the default behavour. You must also be using custom editors and I don't know what your custom code looks like. The default editor has a setClickCountToStart() method which defaults to 2. Maybe you set this to 1.
Change your solution to use data (not Components) in the TableModel and post your SSCCE if you have further questions.