[英]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
. 所以这是我第一次尝试对JTable
使用CellEditors
嵌入JComboBox
和JSpinner
。 Everything works fine as expected wherein I can see the values in JComboBox
model as well as JSpinner
's model values. 一切都按预期工作,其中我可以看到JComboBox
模型中的值以及JSpinner
的模型值。
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
. 但是,我注意到,只要单击具有JComboBox
JTable列,它就总是显示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 . 它不是非常用户友好,因为我认为用户希望双击JTable
的列以获取下拉框值并从中选择值, 而不是单击 。
How can I change the JComboBox
's behaviour to only display itself on double click? 如何更改JComboBox
的行为,使其仅在双击时显示?
I thought I'd apply a MouseListener
to the JComboBox
but I don't know what to do next. 我以为我可以将MouseListener
应用于JComboBox
但我不知道下一步该怎么做。
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: 通过应用其他条件,只需覆盖isCellEditable:
@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
,则可以简单地调用其setClickCountToStart()
方法setClickCountToStart()
count
为2
。
DefaultCellEditor editor = new DefaultCellEditor(jcmbDays);
editor.setClickCountToStart(2);
jcmbColumn.setCellEditor(editor);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.