JTable with JComboBox inside cell programmable 打开

JTable with JComboBox inside cell programmable opened

嘿,我花了几个小时才弄清楚这个问题。目前我必须点击单元格然后再次点击才能打开下拉菜单(这是一个日期选择器)。

我的目标是在代码检测到正确列中的单元格内单击后使用代码。

我试过的代码是:

comboBox.setPopupVisible(true);             

table.editCellAt(0, 4);

comboBox.showPopup();

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

我是这样定义我的组合框的:

public static JComboBox comboBox = new JComboBox();

我是从另一个 class 调用的。

class1:

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

现在我的另一个 class:

TblWithDropdown class:

 @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);
   }
}

它给我的错误是:

Exception in thread "AWT-EventQueue-0" java.awt.IllegalComponentStateException: component must be showing on the screen to determine its location at java.awt.Component.getLocationOnScreen_NoTreeLock(Unknown Source)

视觉上这就是我的 table 组合框的样子:

[ [

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

更新 1

更新 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);

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

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();
            }
        });
    }
});