简体   繁体   中英

How to make JTable with JComboBox respond only on double click instead of single click

So this is the first time I tried using CellEditors for my JTable to embed JComboBox and JSpinner . Everything works fine as expected wherein I can see the values in JComboBox model as well as JSpinner 's model values.

However, I noticed that it always displays the JComboBox 's values as soon as I make a single click on JTable's column that has the JComboBox .

It's not very user friendly because I think the user would prefer to double click on a JTable 's column to get the dropdown box values and select values from it instead of a single click .

How can I change the JComboBox 's behaviour to only display itself on double click?

I thought I'd apply a MouseListener to the JComboBox but I don't know what to do next.

Here's what I've written so far.

public class ScheduleDayCellEditor extends DefaultCellEditor{
    private JComboBox jcmbDays;
    private JTable jtblSchedule;
    private DefaultComboBoxModel model;

    public ScheduleDayCellEditor(){
        super(new JComboBox());
        model = new DefaultComboBoxModel(new String[]{"Mon","Tue","Wed","Thu","Fri"});
        jcmbDays = new JComboBox(model);
        jcmbDays.setEditable(false);
        jcmbDays.setSelectedIndex(-1);

        jcmbDays.addMouseListener(new MouseListener() {
            @Override
            public void mouseClicked(MouseEvent e) {
                if(e.getClickCount() == 2){
                    //? ? ? ?
                }
            }

            @Override
            public void mousePressed(MouseEvent e) {
            }

            @Override
            public void mouseReleased(MouseEvent e) {
            }

            @Override
            public void mouseEntered(MouseEvent e) {
            }

            @Override
            public void mouseExited(MouseEvent e) {
            }
        });
    }

    @Override
    public Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected, int row, int column) {
        return jcmbDays;
    }

    @Override
    public Object getCellEditorValue() {
        return jcmbDays.getSelectedItem(); //To change body of generated methods, choose Tools | Templates.
    }

Here's a screenshot for additional description.

在此处输入图片说明

I'd appreciate any help.

Thank you.

Simply override isCellEditable by applying further criterion:

@Override
public boolean isCellEditable(EventObject aAnEvent) {
    boolean cellEditable = super.isCellEditable(aAnEvent);

    if (cellEditable && aAnEvent instanceof MouseEvent) {
        cellEditable = ((MouseEvent) aAnEvent).getClickCount() == 2;
    }

    return cellEditable;
}

If you don't need to extend DefaultCellEditor for some other reason, you can simply invoke its setClickCountToStart() method with a count of 2 .

DefaultCellEditor editor = new DefaultCellEditor(jcmbDays);
editor.setClickCountToStart(2);
jcmbColumn.setCellEditor(editor);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM