簡體   English   中英

帶有JComboBox的JTable在單元格可編程內部打開

[英]JTable with JComboBox inside cell programmable opened

嘿,我花了幾個小時才弄清楚這一點。 當前,我必須單擊該單元格,然后再次單擊以打開下拉列表(這是一個日期選擇器)。

我的目標是-一旦檢測到正確列中的單元格內的單擊,就使用代碼。

我嘗試過的代碼是:

comboBox.setPopupVisible(true);             

table.editCellAt(0, 4);

comboBox.showPopup();

目前,我確實將該單元格設為可編輯狀態。 雖然當我運行程序並單擊這些單元格之一時,這是給我的錯誤:

我正在定義我的組合框,如下所示:

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

在此處輸入圖片說明

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

您可以嘗試將其作為編輯器JComboBox組件的focusGained偵聽器,而不是直接調用showPopup

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM