JTable 或其他具有高级单元格选择功能的 Java 表类?
现在我正在使用 JTable 来完成我正在做的事情。 在浏览了 Java API 和各种 Web 资源之后,我认为 JTable 不再能解决这个问题了。 我正在寻找的是一个可以指定非常严格的选择程序的表格。 我希望不仅能够选择行和列,而且还能够选择对角线方向的单元格。 更重要的是,我需要能够指定在另一个单元格中时可以选择哪些单元格的整体能力。
例如,如果我有一个 10x10 的表格,并且位于单元格 (4, 3) [(行,列)] 中,我希望能够说,好吧,您可以从此处选择以下间隔:
- (4, 3) 至 (4, 10)
- (4, 3) 至 (4, 1)
- (4, 3) 至 (10, 4)
- (4, 3) 至 (1, 4)
- (4, 3) 至 (10, 10) [对角线]
- (4, 3) 到 (1, 1) [对角线]
- (4, 3) 到 (1, 6) [对角线]
- (4, 3) 到 (6, 1) [对角线]
任何想法我怎么能做到这一点?
Right now I'm using a JTable for what I'm doing. After looking through the Java API and various web resources, I don't think a JTable is going to cut it anymore. What I'm looking for is a table for which I can specify very strict selection procedures. I want to be able to not only select rows and columns, but also select cells in a diagonal direction. More than that, I need the overall ability to be able to specify which cells can be selected when in another cell.
For example, if I have a 10x10 table and I'm in cell (4, 3) [(row, column)], I want to be able to say, okay, you can select the following intervals from here:
- (4, 3) to (4, 10)
- (4, 3) to (4, 1)
- (4, 3) to (10, 4)
- (4, 3) to (1, 4)
- (4, 3) to (10, 10) [diagonally]
- (4, 3) to (1, 1) [diagonally]
- (4, 3) to (1, 6) [diagonally]
- (4, 3) to (6, 1) [diagonally]
Any ideas for how I could do this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
听起来你并不是真的在为“桌子”建模。 (JTable 假定表语义并使用列表选择模型。)但是,如果您愿意破解 JTable 代码,我认为它与矩阵相差并不远。
另一种选择是您自己的(是的)组件:包含矩阵单元的 JPanel。 所有键盘/鼠标事件处理都需要委托给父 JPanel。 我当然会建议从 JTable 克隆相关子集和设计(数据模型、选择模型等)。
所以,基本上,您将需要 3 个类:
JMatrix、JMatrixModel、JMatrixSelectionModel。
JMatrix 是扩展的 JPanel 及其子组件。 JMatrixSelectionModel 是将实现选择规则的类。 JMatrix 应在选择事件上调用选择模型(在矩阵单元上注册,委托给父 JMatrix 上的处理程序)。 数据模型相当简单——您甚至可以使用现有的 JTableModel。
Doesn't sound like you are really modeling a 'table'. (JTable assumes table semantics and uses a List selection model.) However, I don't think it's that far removed from a matrix, if you are willing to hack the JTable code.
An alternative is your own (yep) component: A JPanel that contains the matrix cells. All keyboard/mouse event processing needs to be delegated to the parent JPanel. I would certainly recommend cloning the relevant subsets and design from JTable (data model, selection model, etc.).
So, basically, you will need 3 classes:
JMatrix, JMatrixModel, JMatrixSelectionModel.
The JMatrix is the extended JPanel with its child components. The JMatrixSelectionModel is the class that will implement the selection rules. The JMatrix should call the selection model on selection events (registered on matrix cells, delegated to handler on the parent JMatrix). The data model is fairly straightforward -- you may even be able to use existing JTableModel.
我也有类似的情况。 我的解决方案(抱歉,不想编写一个庞大的类)是为所有列添加一个单元格渲染器,它是表的鼠标侦听器。 由于渲染器知道哪些按钮已被选择,因此它可以以不同的方式渲染它们。
}
I am in a similar situation. My solution (sorry, don't want to write a huge class) was to add a Cell Renderer for all the columns which was a mouse listener for the table. Since the Renderer knows which buttons have been selected, it can render them differently.
}