JComboBox 未在 jTable 中打开
我创建了一个表格,其中每行的单元格中显示一个组合框。我分别使用以下两个类作为单元格编辑器和单元格渲染器。不知何故,当显示表格时,单击单元格中的每个组合框都不会打开。谁能给我提示吗?提前致谢
public class CellEditor extends DefaultCellEditor{
private static final long serialVersionUID = 1L;
public CellEditor(String[] items) {
super(new JComboBox(items));
}
}
public class ComboBoxRenderer extends JComboBox implements TableCellRenderer {
/****/
private static final long serialVersionUID = 1L;
public ComboBoxRenderer(String[] items) {
super(items);
}
public Component getTableCellRendererComponent(JTable table, Object value,
boolean isSelected, boolean hasFocus, int row, int column) {
if (isSelected) {
this.setForeground(table.getSelectionForeground());
super.setBackground(table.getSelectionBackground());
} else {
this.setForeground(table.getForeground());
this.setBackground(table.getBackground());
}
this.setSelectedItem(value);// Select the current value
return this;
}
}
I have created a table where in a cell of each row a combo box is displayed. I have used the following two classes as cell editor and cell renderer respectively. Somehow when the table is displayed, every combo box in a cell doesn't open when it's clicked. Can anyone give me a hint? Thanks in advance
public class CellEditor extends DefaultCellEditor{
private static final long serialVersionUID = 1L;
public CellEditor(String[] items) {
super(new JComboBox(items));
}
}
public class ComboBoxRenderer extends JComboBox implements TableCellRenderer {
/****/
private static final long serialVersionUID = 1L;
public ComboBoxRenderer(String[] items) {
super(items);
}
public Component getTableCellRendererComponent(JTable table, Object value,
boolean isSelected, boolean hasFocus, int row, int column) {
if (isSelected) {
this.setForeground(table.getSelectionForeground());
super.setBackground(table.getSelectionBackground());
} else {
this.setForeground(table.getForeground());
this.setBackground(table.getBackground());
}
this.setSelectedItem(value);// Select the current value
return this;
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
请阅读JTable的教程,有编辑器和渲染器 和 使用组合框作为编辑器,此论坛上的一些示例(包括 JTable 中的 AutoCompleted JComboBox)或 此处 或此处
但基本上是你的问题,(检查你是否设置了)
please reads JTable's tutorial, there are Editors and Renderers and Using a Combo Box as an Editor, some examples on this Forum (inc AutoCompleted JComboBox in the JTable) or here or here
but basically is your question about, (check if you set that)