jtable组合框在别处单击时会丢失值

gkn4icbw  于 2021-06-30  发布在  Java
关注(0)|答案(2)|浏览(382)

我已经定了一个计划 jCombobox 作为 DefaultCellEditor 为了一个 JTable 牢房。
当我在单元格中键入一个值时,问题就出现了( 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;
    }

}
nwlqm0z1

nwlqm0z11#

尝试

JTable table = new yourTable();
table.putClientProperty("terminateEditOnFocusLost", Boolean.TRUE);

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

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

hxzsmxv22#

组合框通常存储了多个项目列表。在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) 
{

}

相关问题