[英]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.