[英]How to use JLists in JTable cells?
I would like a simple way to put a JList in a column of a JTable. 我想要一个简单的方法将JList放在JTable的列中。 I already have the JLists and the table, but when put in the table, the Jlists are displayed as Strings, which is normal, because I use DefaultTableModel.
我已经有了JLists和表,但是当放入表中时,Jlists显示为Strings,这是正常的,因为我使用DefaultTableModel。 I have overriden the getColumnClass() as:
我已经将getColumnClass()重写为:
public Class<? extends Object> getColumnClass(int c)
{
return getValueAt(0, c).getClass();
}
but this just formats the integer and float values. 但这只是格式化整数和浮点值。
I suppose that setValueAt() and getValueAt() should also be overriden, in order to return am array of Strings when I call JList.getSelectedValues() , but I can't figure out how. 我想也应该覆盖setValueAt()和getValueAt() ,以便在调用JList.getSelectedValues()时返回字符串数组,但我无法弄清楚如何。
I also want the cells to be editable, so the users can choose one or more option from the JList. 我还希望单元格可以编辑,因此用户可以从JList中选择一个或多个选项。 After editing a row, I use a Save button to save the changes in a database, so I don't think I need a ListSelectionListener, JList.getSelectedValues() works just fine.
编辑一行后,我使用Save按钮将更改保存在数据库中,所以我认为我不需要ListSelectionListener,JList.getSelectedValues()可以正常工作。
I know this is a common question, but I couldn't find an answer here. 我知道这是一个常见问题,但我在这里找不到答案。 If this is a duplicate, please let me know and I will delete it.
如果这是重复,请告诉我,我将删除它。
I've done it. 我做到了 For everyone who needs the same thing, here is what I've done:
对于每个需要同样事情的人来说,这就是我所做的:
1)I have created a JScrollTableRenderer, and set the column I needed to show the JList to use this renderer 1)我创建了一个JScrollTableRenderer,并设置了我需要显示JList以使用此渲染器的列
table.getColumnModel().getColumn(5).setCellRenderer(new JScrollTableRenderer());
The JScrollTableRenderer class content: JScrollTableRenderer类内容:
public class JScrollTableRenderer extends DefaultTableCellRenderer {
JScrollPane pane = new JScrollPane();
public JScrollTableRenderer()
{
super();
}
public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected,
boolean hasFocus, int row, int column)
{
pane = (JScrollPane) value;
return pane;
}
}
2)I have created a JScrollTableEditor, and set the column I needed to show the JList to use this editor 2)我创建了一个JScrollTableEditor,并设置我需要的列来显示JList以使用这个编辑器
table.getColumnModel().getColumn(5).setCellEditor(new JScrollTableEditor());
The JScrollTableEditor class content: JScrollTableEditor类内容:
public class JScrollTableEditor extends AbstractCellEditor implements TableCellEditor {
JScrollPane component = new JScrollPane();
public Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected,
int rowIndex, int vColIndex)
{
component = ((JScrollPane) value);
return ((JScrollPane) value);
}
public Object getCellEditorValue()
{
return component;
}
}
3)I added this method in the JTable model: 3)我在JTable模型中添加了这个方法:
public Class<? extends Object> getColumnClass(int c)
{
if(c == 5) return JScrollPane.class;
else return getValueAt(0, c).getClass();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.