Java 在别处单击JTable ComboBox时会丢失值

Java 在别处单击JTable ComboBox时会丢失值,java,swing,jtable,jcombobox,Java,Swing,Jtable,Jcombobox,我已将JTable单元格的jCombobox设置为defaultcelleeditor 当我在单元格(jCombobox)中键入一个值时,当我单击其他位置时,该值就会丢失。有人知道我为什么以及如何解决这个问题 table.getColumnModel().getColumn(1).setCellEditor(new SpringJobTablePopupCellEditor()); public class SpringJobTablePopupCellEditor extends Abstr

我已将
JTable
单元格的
jCombobox
设置为
defaultcelleeditor

当我在单元格(
jCombobox
)中键入一个值时,当我单击其他位置时,该值就会丢失。有人知道我为什么以及如何解决这个问题

table.getColumnModel().getColumn(1).setCellEditor(new SpringJobTablePopupCellEditor());

public class SpringJobTablePopupCellEditor extends AbstractCellEditor implements TableCellEditor {

    JTextField jtf;

    DefaultCellEditor other;
    DefaultCellEditor checkbox;
    private DefaultCellEditor lastSelected;
    JComboBox cbox = null;
    public SpringJobTablePopupCellEditor() {
        jtf = new JTextField();
        jtf.setDocument(new JTextFieldLimit(1000));

        other = new DefaultCellEditor(jtf);
        checkbox = new DefaultCellEditor(generateBox("10"));
    }

    @Override
    public Object getCellEditorValue() {
        return lastSelected.getCellEditorValue();
    }

    @Override
    public Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected, int row, int column) {
    final JTable t = table;
    cbox.getEditor().getEditorComponent().addFocusListener(new FocusListener() {

        @Override
        public void focusGained(FocusEvent e) {
            throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
        }

        @Override
        public void focusLost(FocusEvent e) {
            if(t.isEditing()){
                t.getCellEditor().stopCellEditing();
            }
        }
    });
        String val = table.getModel().getValueAt(row, column - 1).toString();

        if("ak".equals(val)){
            lastSelected = checkbox;
            return checkbox.getTableCellEditorComponent(table, value, isSelected, row, column);
        }
        lastSelected = other;
        return other.getTableCellEditorComponent(table, value, isSelected, row, column);
    }

    private JComboBox generateBox(String type) {

        cbox = new JComboBox();
        cbox.setEditable(true);
        for (Map.Entry<String, String> entry : SpringJob.akMap.entrySet()) {
            cbox.addItem(entry.getValue());
        }
        return cbox;
    }

}
table.getColumnModel().getColumn(1.setCellEditor)(新的SpringJobTablePopupCellEditor());
公共类SpringJobTablePopupCellEditor扩展了AbstractCellEditor实现了TableCellEditor{
联合特遣部队;
违约;其他;
DefaultCellEditor复选框;
选择了私有的DefaultCellEditor;
JComboBox cbox=null;
公共SpringJobTablePopupCellEditor(){
jtf=新的JTextField();
jtf.setDocument(新的JTextFieldLimit(1000));
其他=新的DefaultCellEditor(jtf);
复选框=新的DefaultCellEditor(generateBox(“10”));
}
@凌驾
公共对象getCellEditorValue(){
返回lastSelected.getCellEditorValue();
}
@凌驾
公共组件getTableCellEditorComponent(JTable表、对象值、布尔isSelected、int行、int列){
最终JTable t=表格;
cbox.getEditor().getEditorComponent().addFocusListener(新FocusListener()){
@凌驾
获得公共无效焦点(焦点事件e){
抛出新的UnsupportedOperationException(“尚未受支持”);//若要更改生成的方法体,请选择“工具”“模板”。
}
@凌驾
公共无效焦点丢失(焦点事件e){
if(t.isEditing()){
t、 getCellEditor().stopCellEditing();
}
}
});
字符串val=table.getModel().getValueAt(行,列-1).toString();
如果(“ak”。等于(val)){
lastSelected=复选框;
return checkbox.getTableCellEditorComponent(表、值、isSelected、行、列);
}
lastSelected=其他;
返回other.getTableCellEditorComponent(表、值、isSelected、行、列);
}
专用JComboxGenerateBox(字符串类型){
cbox=新的JComboBox();
cbox.setEditable(true);
for(Map.Entry:SpringJob.akMap.entrySet()){
cbox.addItem(entry.getValue());
}
返回cbox;
}
}
试试看

当您从一个组合框转到另一个组合框时,需要强制表停止编辑 我通过在组合框中添加focusListener和focuslost中添加focusListener实现了类似的效果

public void focusLost(FocusEvent e) {
    if (table.isEditing())
        table.getCellEditor().stopCellEditing();        
    }
}

组合框通常存储了多个项目列表。当您单击jtable中的JCombo框并键入它不会直接添加到combobox列表项中的值时。您必须首先在组合框中添加值。组合框包含对象数组

尝试在Jtable组合框中动态添加该代码的值

static JComboBox combo = new JComboBox();
static JTable table = new JTable();
public static void main(String[] args) 
{

    JFrame frame = new JFrame();

    JPanel topPanel = new JPanel();
    JPanel middlepanel = new JPanel();

    combo.addItem("First");
    combo.addItem("Second");
    combo.addItem("Third");

    JButton button = new JButton("Add Item");
    topPanel.add(button);        
    frame.setLayout(new BorderLayout());
    frame.setSize(500, 500);
    frame.add(topPanel, BorderLayout.NORTH);

    DefaultTableModel model = (DefaultTableModel) table.getModel();
    model.addColumn("A", new Object[] { "item1" });
    middlepanel.setLayout(new BorderLayout());
    middlepanel.add(table,BorderLayout.CENTER);
    String str[] = new String[combo.getItemCount()];
    for(int i=0;i<combo.getItemCount();i++){
        str[i] = combo.getItemAt(i).toString();
    }
    table.getColumnModel().getColumn(0).setCellEditor(new MyComboBoxEditor(combo));
    table.getColumnModel().getColumn(0).setCellRenderer(new MyComboBoxRenderer(str));
    table.setRowHeight(25);

    frame.add(middlepanel,BorderLayout.CENTER);
    frame.setVisible(true);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    button.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {
            String cmd = e.getActionCommand();
            switch(cmd)
            {
                case "Add Item":
                    combo.addItem("Fourth");
                    combo.addItem("Fifth");
                    String str[] = new String[combo.getItemCount()];
                    for(int i=0;i<combo.getItemCount();i++){
                        str[i] = combo.getItemAt(i).toString();
                    }
                    table.getColumnModel().getColumn(0).setCellEditor(new MyComboBoxEditor(combo));
                    table.getColumnModel().getColumn(0).setCellRenderer(new MyComboBoxRenderer(str));
                    break;
            }
        }
    });
}

@Override
public void actionPerformed(ActionEvent e) 
{

}
staticjcombobox组合=newjcombobox();
静态JTable表=新JTable();
公共静态void main(字符串[]args)
{
JFrame=新JFrame();
JPanel-topPanel=新的JPanel();
JPanel middlepanel=新的JPanel();
组合添加项(“第一”);
组合附加项(“第二”);
附加条款组合(“第三”);
JButton按钮=新JButton(“添加项”);
topPanel.add(按钮);
frame.setLayout(新的BorderLayout());
框架。设置尺寸(500500);
添加框架(topPanel,BorderLayout.NORTH);
DefaultTableModel=(DefaultTableModel)table.getModel();
addColumn(“A”,新对象[]{“item1”});
setLayout(新的BorderLayout());
middlepanel.add(表,BorderLayout.CENTER);
String str[]=新字符串[combo.getItemCount()];

对于(int i=0;ii如果我回忆正确,失去焦点的表格编辑器的默认行为是取消编辑查看并验证
setValueAt()
的实现。不起作用!我在组合中输入一个值,然后单击下面的单元格,组合的值设置为默认值(空).正确,表格没有失去焦点,因此额外属性没有帮助。对我来说没有任何更改!
static JComboBox combo = new JComboBox();
static JTable table = new JTable();
public static void main(String[] args) 
{

    JFrame frame = new JFrame();

    JPanel topPanel = new JPanel();
    JPanel middlepanel = new JPanel();

    combo.addItem("First");
    combo.addItem("Second");
    combo.addItem("Third");

    JButton button = new JButton("Add Item");
    topPanel.add(button);        
    frame.setLayout(new BorderLayout());
    frame.setSize(500, 500);
    frame.add(topPanel, BorderLayout.NORTH);

    DefaultTableModel model = (DefaultTableModel) table.getModel();
    model.addColumn("A", new Object[] { "item1" });
    middlepanel.setLayout(new BorderLayout());
    middlepanel.add(table,BorderLayout.CENTER);
    String str[] = new String[combo.getItemCount()];
    for(int i=0;i<combo.getItemCount();i++){
        str[i] = combo.getItemAt(i).toString();
    }
    table.getColumnModel().getColumn(0).setCellEditor(new MyComboBoxEditor(combo));
    table.getColumnModel().getColumn(0).setCellRenderer(new MyComboBoxRenderer(str));
    table.setRowHeight(25);

    frame.add(middlepanel,BorderLayout.CENTER);
    frame.setVisible(true);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    button.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {
            String cmd = e.getActionCommand();
            switch(cmd)
            {
                case "Add Item":
                    combo.addItem("Fourth");
                    combo.addItem("Fifth");
                    String str[] = new String[combo.getItemCount()];
                    for(int i=0;i<combo.getItemCount();i++){
                        str[i] = combo.getItemAt(i).toString();
                    }
                    table.getColumnModel().getColumn(0).setCellEditor(new MyComboBoxEditor(combo));
                    table.getColumnModel().getColumn(0).setCellRenderer(new MyComboBoxRenderer(str));
                    break;
            }
        }
    });
}

@Override
public void actionPerformed(ActionEvent e) 
{

}