我正在尝试用java做一些简单的应用程序。我在jTable中渲染了一些CheckBox和ComboBox。现在我正在尝试处理该项目,例如获取值、启用-禁用组合框。但我面临一些问题。
我现在面临的是什么
1.
我在 jTable 中渲染 ComboBox 和 CheckBox。当我单击相应行的复选框时,我尝试启用组合框。如果我的复选框未启用,则应禁用它的 ComboBox。
我尝试过但没有成功。
2
我试图单击复选框,但如果我使用 setSelected,则所有复选框都会被选中,但当我尝试取消选中它时,它却没有。
这是我的代码供您引用。
public class MyComboBoxRenderer extends JComboBox implements TableCellRenderer {
public MyComboBoxRenderer(String[] items) {
super(items);
}
public Component getTableCellRendererComponent(JTable table, Object value,
boolean isSelected, boolean hasFocus, int row, int column) {
if (isSelected) {
setForeground(Color.BLACK);
super.setBackground(Color.WHITE);
} else {
setForeground(Color.BLACK);
setBackground(Color.WHITE);
}
// Select the current value
setSelectedItem(value);
return this;
}
}
public class MyComboBoxEditor extends DefaultCellEditor {
public MyComboBoxEditor(String[] items) {
super(new JComboBox(items));
}
}
.
public class MyCheckBoxRenderer extends JCheckBox implements TableCellRenderer {
public MyCheckBoxRenderer(String[] items) {
super();
// setSelected(true);
}
public Component getTableCellRendererComponent(JTable table, Object value,
boolean isSelected, boolean hasFocus, int row, int column) {
if (isSelected) {
setForeground(Color.BLACK);
super.setBackground(Color.WHITE);
} else {
setForeground(Color.BLACK);
setBackground(Color.WHITE);
}
// setSelected(true);
// Select the current value
return this;
}
}
public class MyCheckBoxEditor extends DefaultCellEditor {
public MyCheckBoxEditor() {
super(new JCheckBox());
}
}
给我一些提示或引用。
提前致谢。
最佳答案
see my question about prepareEditor (and/or with prepareRenderer)
我建议使用
prepareRenderer
(easiest and more confortable, my view)并发送至check Boolean value came from Column contains (rendered as )JCheckBox
必须覆盖
convertXxxToModel
,因为ColumnModel
could be reordered and/or rows can be sorted or filtered ,在这种情况下渲染器无法正常工作
关于java - 通过单击复选框禁用 jtable 中的组合框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13159140/