如何使带有 JComboBox 的 JTable 仅在双击而不是单击时响应
How to make JTable with JComboBox respond only on double click instead of single click
所以这是我第一次尝试使用 CellEditors
为我的 JTable
嵌入 JComboBox
和 JSpinner
。一切都按预期正常工作,其中我可以看到 JComboBox
模型中的值以及 JSpinner
的模型值。
但是,我注意到它总是在我单击具有 JComboBox
.
的 JTable 列时立即显示 JComboBox
的值
它对用户不太友好,因为我认为用户更愿意双击 JTable
的列以获取下拉框值和 select 值 而不是单击.
如何将 JComboBox
的行为更改为仅在 双击 单击时显示?
我想我应该将 MouseListener
应用到 JComboBox
,但我不知道下一步该做什么。
这是我到目前为止所写的内容。
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.
}
这里是附加说明的屏幕截图。
如有任何帮助,我将不胜感激。
谢谢。
通过应用进一步的标准简单地覆盖 isCellEditable:
@Override
public boolean isCellEditable(EventObject aAnEvent) {
boolean cellEditable = super.isCellEditable(aAnEvent);
if (cellEditable && aAnEvent instanceof MouseEvent) {
cellEditable = ((MouseEvent) aAnEvent).getClickCount() == 2;
}
return cellEditable;
}
如果您不需要使用 2
的 count
扩展 DefaultCellEditor
for some other reason, you can simply invoke its setClickCountToStart()
方法。
DefaultCellEditor editor = new DefaultCellEditor(jcmbDays);
editor.setClickCountToStart(2);
jcmbColumn.setCellEditor(editor);
所以这是我第一次尝试使用 CellEditors
为我的 JTable
嵌入 JComboBox
和 JSpinner
。一切都按预期正常工作,其中我可以看到 JComboBox
模型中的值以及 JSpinner
的模型值。
但是,我注意到它总是在我单击具有 JComboBox
.
JComboBox
的值
它对用户不太友好,因为我认为用户更愿意双击 JTable
的列以获取下拉框值和 select 值 而不是单击.
如何将 JComboBox
的行为更改为仅在 双击 单击时显示?
我想我应该将 MouseListener
应用到 JComboBox
,但我不知道下一步该做什么。
这是我到目前为止所写的内容。
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.
}
这里是附加说明的屏幕截图。
如有任何帮助,我将不胜感激。
谢谢。
通过应用进一步的标准简单地覆盖 isCellEditable:
@Override
public boolean isCellEditable(EventObject aAnEvent) {
boolean cellEditable = super.isCellEditable(aAnEvent);
if (cellEditable && aAnEvent instanceof MouseEvent) {
cellEditable = ((MouseEvent) aAnEvent).getClickCount() == 2;
}
return cellEditable;
}
如果您不需要使用 2
的 count
扩展 DefaultCellEditor
for some other reason, you can simply invoke its setClickCountToStart()
方法。
DefaultCellEditor editor = new DefaultCellEditor(jcmbDays);
editor.setClickCountToStart(2);
jcmbColumn.setCellEditor(editor);