JTable 中有多个具有不同值的 JComboBox
我想创建一个 JTable,其中应在名为“Tools”的特定列中容纳 JComboBox。每行的 JComboBox 值应该不同。
在构造函数中,我有以下代码:
public Table(List<Software> software_list) throws TransformerFactoryConfigurationError, TransformerException, Exception {
//****** create an 2D array object containing the emulators list data to be sent to the table model ******
software_list_to_array = new Object[software_list.size()][5];
for(int x = 0; x < software_list_to_array.length; x++){
software_list_to_array[x][0] = software_list.get(x).getNumber();
software_list_to_array[x][1] = software_list.get(x).getAuthor();
software_list_to_array[x][2] = software_list.get(x).getReleaseDate();
software_list_to_array[x][3] = software_list.get(x).getName();
app_str = new String[software_list.get(x).getApp().size()];
System.out.println(app_str.length);
for(int k = 0; k < app_str.length; k++){
app_str[k] = software_list.get(x).getApp().get(k).getName()
+ " " + software_list.get(x).getApp().get(k).getVersion();
}
software_list_to_array[x][4] = app_str;
}
tableModel = new SftTableModel(software_list_to_array, software_list_to_array.length);
this.setModel(tableModel);
this.setSize(new Dimension(1000,500));
this.setRenderingProperties(app_str);
}
我创建了以下方法:
public void setRenderingProperties(String[] str){
this.setRowHeight(25);
this.getTableHeader().setFont(new Font("Arial", Font.BOLD,12));
this.getTableHeader().setAlignmentY(Component.RIGHT_ALIGNMENT);
this.setAutoCreateRowSorter(true);
this.setShowGrid(false);
this.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
...........
if(app_str.length > 1){
this.getColumn("Tools").setCellRenderer(new ComboBoxRenderer());
JComboBox comboBox = new JComboBox();
for(int k=0;k<app_str.length;k++){
comboBox.addItem(app_str[k]);
}
this.getColumn("Tools").setCellEditor(new DefaultCellEditor(comboBox));
}else{
this.getColumn("Tools").setCellRenderer(new CellsRenderer());
}
this.getColumn("Tools").setResizable(true);
this.getColumn("Tools").setPreferredWidth(250);
}
这段代码的问题是,在“工具”列中,所有组合框都包含相同的值。有人对这个问题有建议吗?
I want to create a JTable where in a specific column named "Tools" a JComboBox should be accommodated. the values of the JComboBox should differ for each row.
In the constructor I have the following piece of code:
public Table(List<Software> software_list) throws TransformerFactoryConfigurationError, TransformerException, Exception {
//****** create an 2D array object containing the emulators list data to be sent to the table model ******
software_list_to_array = new Object[software_list.size()][5];
for(int x = 0; x < software_list_to_array.length; x++){
software_list_to_array[x][0] = software_list.get(x).getNumber();
software_list_to_array[x][1] = software_list.get(x).getAuthor();
software_list_to_array[x][2] = software_list.get(x).getReleaseDate();
software_list_to_array[x][3] = software_list.get(x).getName();
app_str = new String[software_list.get(x).getApp().size()];
System.out.println(app_str.length);
for(int k = 0; k < app_str.length; k++){
app_str[k] = software_list.get(x).getApp().get(k).getName()
+ " " + software_list.get(x).getApp().get(k).getVersion();
}
software_list_to_array[x][4] = app_str;
}
tableModel = new SftTableModel(software_list_to_array, software_list_to_array.length);
this.setModel(tableModel);
this.setSize(new Dimension(1000,500));
this.setRenderingProperties(app_str);
}
I have created the following method:
public void setRenderingProperties(String[] str){
this.setRowHeight(25);
this.getTableHeader().setFont(new Font("Arial", Font.BOLD,12));
this.getTableHeader().setAlignmentY(Component.RIGHT_ALIGNMENT);
this.setAutoCreateRowSorter(true);
this.setShowGrid(false);
this.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
...........
if(app_str.length > 1){
this.getColumn("Tools").setCellRenderer(new ComboBoxRenderer());
JComboBox comboBox = new JComboBox();
for(int k=0;k<app_str.length;k++){
comboBox.addItem(app_str[k]);
}
this.getColumn("Tools").setCellEditor(new DefaultCellEditor(comboBox));
}else{
this.getColumn("Tools").setCellRenderer(new CellsRenderer());
}
this.getColumn("Tools").setResizable(true);
this.getColumn("Tools").setPreferredWidth(250);
}
The problem with this code is that in the Tools column all the comboboxes contain all the same value. Does anyone have suggestion for this issue?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以重写 JTable 的
getCellEditor(...)
以返回特定的编辑器。像这样的东西:You can override the
getCellEditor(...)
of the JTable to return a specific editor. Something like:因为这个 代码 太旧了,但我不记得了我看到了另一种更好、更简单的用法,例如 此处< /a>
since this code is too old, but I not remember that I seen another better, simple usage with example here
我认为主要问题是您的 app_str 字符串仅实例化一次。最后,所有内容都设置为该实例的最后一个值。
I think that the main problem is that your app_str String is only instantiated once. Then at the end, everything is set to the last value of this instance.