JTable与JComboBox内部单元可编程打开

问题描述 投票:0回答:1

嘿,我现在已经花了几个小时来解决这个问题。目前我必须单击单元格,然后再次单击以打开下拉列表(这是一个日期选择器)。

我的目标是 - 一旦检测到位于正确列中的单元格内的单击,就使用代码。

我试过的代码是:

comboBox.setPopupVisible(true);             

table.editCellAt(0, 4);

comboBox.showPopup();

目前我确实将/ cells作为可编辑的。虽然当我运行程序并单击其中一个单元格时,这是它给我的错误:

我正在定义我的组合框,如下所示:

public static JComboBox comboBox = new JComboBox();

我从另一个班级打电话给我。

1类:

if (selCol == 4) {
    try {
             TblWithDropdown.dropBox();
        } catch (InterruptedException e) {
             e.printStackTrace();
    }
} 

现在是我的另一堂课:

TblWithDropdown类:

 @SuppressWarnings("rawtypes")
 public static JComboBox comboBox = new JComboBox();

public class TblWithDropdown {
   public static void dropBox() throws InterruptedException {
      comboBox.showPopup();
      //table.editCellAt(0, 4);
      comboBox.setPopupVisible(true);
   }
}

它给我的错误是:

线程“AWT-EventQueue-0”中的异常java.awt.IllegalComponentStateException:必须在屏幕上显示组件以确定其在java.awt.Component.getLocationOnScreen_NoTreeLock(未知来源)中的位置

在视觉上这是我的combox表格:

[ [

为解决这个问题,帮助会很棒!

更新1

enter image description here

更新2

_al = alldata.fillInData("SELECT fname FROM users");
String[] testers = new String[_al.size()];
TblWithDropdown.comboBox = new JComboBox(_al.toArray(testers));
TblWithDropdown.table.getColumnModel().getColumn(3)
                      .setCellEditor(new DefaultCellEditor(TblWithDropdown.comboBox));
JXDatePicker res = new JXDatePicker();
res.setFormats(DateFormat.getDateInstance(DateFormat.MEDIUM));
res.setDate(new Date());
res.getMonthView().setDayForeground(Calendar.SUNDAY, Color.RED);

DatePickerCellEditor testser = new DatePickerCellEditor(new SimpleDateFormat("dd/MM/yyyy HH:mm:ssZ"));

testser.setClickCountToStart(0);
testser.setFormats(new SimpleDateFormat("dd/MM/yyyy HH:mm:ssZ"));
TableColumn dateColumn = TblWithDropdown.table.getColumnModel().getColumn(4);
dateColumn.setCellEditor(testser);
java swing jtable jcombobox
1个回答
0
投票

您可以尝试的不是直接调用showPopup,而是将其作为编辑器focusGained组件的JComboBox监听器:

public static JComboBox comboBox = new JComboBox();
// initialize editor component
comboBox.addFocusListener(new FocusAdapter() {
    public void focusGained(FocusEvent e) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                comboBox.showPopup();
            }
        });
    }
});
© www.soinside.com 2019 - 2024. All rights reserved.