[英]disable Combobox in jtable by click on checkbox
I am trying to do some simple application in java. 我试图在Java中做一些简单的应用程序。 I rendered some CheckBox and ComboBox in jTable.
我在jTable中渲染了一些CheckBox和ComboBox。 Now i am trying to do work on that item like getting value, Enable-disable combobox.
现在,我正在尝试在该项目上进行工作,例如获取价值,启用-禁用组合框。 But i am facing some problem.
但是我面临一些问题。
What i am facing now 我现在面对的是
1. 1。
I render ComboBox and CheckBox in jTable. 我在jTable中渲染ComboBox和CheckBox。 I am trying to enable ComboBox when i click on checkbox of respective row.
当我单击相应行的复选框时,我试图启用ComboBox。 If my checkBox is not enable then it ComboBox should be disable.
如果我的复选框未启用,则应禁用ComboBox。
I tried it but didn't success. 我尝试过,但是没有成功。
2 2
I am trying to click on check box but if i use setSelected then all checkbox are checked but when i am trying to uncheck it, it didn't. 我试图单击复选框,但是如果我使用setSelected,则所有复选框都被选中,但是当我试图取消选中它时,它没有。
Here is my code for your reference. 这是我的代码供您参考。
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());
}
}
Give me some hint or reference. 给我一些提示或参考。
Thanks in Advance. 提前致谢。
see my question about prepareEditor (and/or with prepareRenderer) 请参阅我关于prepareEditor(和/或prepareRenderer)的问题
I'd suggest to use prepareRenderer
(easiest and more confortable, my view) and to check Boolean value came from Column contains (rendered as ) JCheckBox
我建议使用
prepareRenderer
(我的视图最简单,更舒适),并检查布尔值来自Column contains(渲染为) JCheckBox
have to override convertXxxToModel
, because ColumnModel
could be reordered and/or rows can be sorted or filtered , in this case Renderer doesn't works correctly 必须重写
convertXxxToModel
,因为ColumnModel
可以重新排序和/或行可以排序或过滤 ,在这种情况下,Renderer无法正常工作
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.