[英]disable Combobox in jtable by click on checkbox
我试图在Java中做一些简单的应用程序。 我在jTable中渲染了一些CheckBox和ComboBox。 现在,我正在尝试在该项目上进行工作,例如获取价值,启用-禁用组合框。 但是我面临一些问题。
我现在面对的是
1。
我在jTable中渲染ComboBox和CheckBox。 当我单击相应行的复选框时,我试图启用ComboBox。 如果我的复选框未启用,则应禁用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());
}
}
给我一些提示或参考。
提前致谢。
我建议使用prepareRenderer
(我的视图最简单,更舒适),并检查布尔值来自Column contains(渲染为) JCheckBox
必须重写convertXxxToModel
,因为ColumnModel
可以重新排序和/或行可以排序或过滤 ,在这种情况下,Renderer无法正常工作
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.