Java Jtable电子表格格式 [英] Java Jtable spreadsheet like format
问题描述
我在实现JTable
,特别是AbstractTableModel
时遇到问题.我可以从JTable
创建和显示数据,但是我当前的场景与通常的场景不同.
I have problems regarding implementing JTable
, specifically the AbstractTableModel
. I can create and show data from JTable
but my current scenario is different from my usually scenario.
我想做的是使我的JTable
接受用户的输入,我想将其他组件放入JTable
What I want to do is to make my JTable
accept an input from user and I want to put other components inside the JTable
让我们说每一行都有一个JComboBox
,JTextField
和一个JCheckBox
,但是当我开始实现JTable
模型(AbstractTableModel
)时,我不知道如何放置它们我的JTable
的组件?
Lets say for every row there is a JComboBox
, JTextField
and a JCheckBox
, however when I start to implement the JTable
model (AbstractTableModel
) I don't know how can I put those components to my JTable
?
由于返回类型AbstractTableModel
的方法getValue
是对象数据类型.您是否有任何解决此类问题的方法,以及我讨厌使用DefaultTableModel
的方式,因为它尽可能缺乏灵活性,所以我非常希望AbstractTableModel
:)帮助.
Since the method getValue
from AbstractTableModel
return type is of object data types. Do you have any workaround for this kind of problems and by the ways I hate using DefaultTableModel
since it lacks flexibility as much as possible I want an AbstractTableModel
:) help is really appreciated.
推荐答案
您绝对应该从 如何使用表格 并研究示例显示了如何使用JComboBox
更新同一行中的其他单元格,而这些
You should definitely start with How to Use Tables and study the examples there. While a JTable
ins't a spreadsheet, you can calculate derived values in your table's model. This example shows how to use a JComboBox
to update other cells in the same row, and these examples similarly contrast the two TableModel
implementations you mention. Finally, you can use an available ScriptEngine
to evaluate simple arithmetic expressions. For example, the example below prints 42.0
.
ScriptEngine engine = mgr.getEngineByExtension("js");
try {
System.out.println(engine.eval("5 * 8 + 2"));
} catch (ScriptException ex) {
ex.printStackTrace(System.err);
}
这篇关于Java Jtable电子表格格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!