Im在设置JComboBox时遇到问题。用户在一个separte面板上有几个选项可以确定JComboBox是否应该被启用/禁用 - 我遇到的问题是用户仍然可以从JComboBox中选择,即使它被禁用(由于组合框被禁用,它被禁用)! JComboBox使用自定义TableCellRenderer和自定义DefaultCellEditor。此外,JComboBox是一行JTable中的单元格/列。禁用后仍然可以从JComboBox中选择
所以这里是代码的崩溃:
*prepareRenderer JTable中*
public Component prepareRenderer(TableCellRenderer renderer, int row, int column) {
JComponent component = (JComponent) super.prepareRenderer(renderer, row, column);
//Code which checks to see if component should be enabled
enableComponent = false;
component.setEnabled(enableComponent);
}
*设置组合框的*
public void setupUserCombo(){
TableColumn col = getColumnModel().getColumn(0);
List<String> comboUsers = new String["Adam", "Ben"]
MyComboBoxRenderer jComboBox = (new MyComboBoxRenderer((String[])values.toArray(comboUsers));
col.setCellEditor(new MyComboBoxEditor((String[])values.toArray(new String[0])));
col.setCellRenderer(jComboBox);
repaint();
}
* *的TableCellRenderer
public class MyComboBoxRenderer extends JComboBox implements TableCellRenderer {
private static final long serialVersionUID = 1L;
public MyComboBoxRenderer(String[] items) {
super(items);
repaint();
}
public Component getTableCellRendererComponent(JTable table, Object value,
boolean isSelected, boolean hasFocus, int row, int column) {
setSelectedItem("");
if (isSelected) {
super.setBackground(table.getSelectionBackground());
} else {
setForeground(table.getForeground());
setBackground(table.getBackground());
}
setSelectedItem(value);
return this;
}
}
* DefaultCellEditor *
public class MyComboBoxEditor extends DefaultCellEditor {
private static final long serialVersionUID = 1L;
public MyComboBoxEditor(String[] items) {
super(new JComboBox(items));
}
}
任何指针,以什么即时做错了将不胜感激!
感谢,
非常好 - 感谢您的帮助!问题是我已经在模型中覆盖该方法,以便在选择组合框时始终返回true。现在修复只在其启用时才返回true。 – maloney