![](/img/trans.png)
[英]Java Swing setEditable(boolean) not working in JComboBox with custom renderer
[英]JTable JComboBox SetEditable not working
我試圖使一個可編輯的JComboBox
在表中工作,但到目前為止,還沒有運氣。 在表中的單元格內進行操作時,設置cbo.setEditable(true)
似乎無效。 我有什么想念的嗎? 請幫忙。
示例代碼演示該問題:
public class ComboBoxTest {
private JFrame frame;
private JTable table;
private JComboBox<?> cboFrm = null;
private JComboBox<?> cboTbl = null;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
ComboBoxTest window = new ComboBoxTest();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the application.
*/
public ComboBoxTest() {
initialise();
}
/**
* Initialise the contents of the frame.
*/
private void initialise() {
frame = new JFrame();
frame.setBounds(100, 100, 455, 233);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(null);
String[] cboData = { "tspn", "tblspn", "gram", "Kg" };
cboFrm = new JComboBox<String>(cboData);
cboFrm.setBounds(10, 26, 86, 20);
cboFrm.setEditable(true);
frame.getContentPane().add(cboFrm);
JScrollPane scrollPane = new JScrollPane();
scrollPane.setBounds(10, 57, 414, 127);
frame.getContentPane().add(scrollPane);
String columnNames[] = { "Qty", "Measure", "Ingrediant" };
// @formatter:off
Object[][] tableData =
{
{ 1, "Kg", "Sugar" },
{ 1, "pinch", "Salt" },
{ 2, "handfuls", "Peanuts" },
{ 1, "Litre", "Milk"}
};
// @formatter:on
table = new JTable(tableData, columnNames);
cboTbl = new JComboBox<String>(cboData);
cboTbl.setEditable(true);
table.getColumnModel().getColumn(1)
.setCellEditor(new DefaultCellEditor(cboTbl));
scrollPane.setViewportView(table);
}
}
此輸出對我來說很好用:
回答我自己的問題。
JComboBox與JTable交互的方式似乎存在一個錯誤。 解決方法是在更改新文本后按“輸入”。 跳出表格JComboBox單元格不起作用,這與標准文本單元格的行為不一致。
有一個錯誤報告可以追溯到1999-2001年,據說該錯誤已得到修復,但再次出現。 請參考以下鏈接:
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.