【发布时间】:2015-02-15 12:25:53
【问题描述】:
我正在尝试让可编辑的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);
}
}
【问题讨论】:
-
避免使用
null布局,像素完美的布局是现代用户界面设计中的一种错觉。影响组件单个尺寸的因素太多,您无法控制。 Swing 旨在与核心布局管理器一起工作,丢弃这些将导致无穷无尽的问题和问题,您将花费越来越多的时间来尝试纠正 -
对我来说很好,你能描述一下你的实际问题和你的期望......
-
对我也很好。
-
感谢您的回复。我想做的是(比如说)添加“doggy”作为衡量标准,但是当我离开牢房时,它会恢复为“Kg”。我希望它保持“小狗”的状态。换句话说,我不想仅限于查找中的那些项目。相反,组合框列表是最常用的措施,但并不限制用户使用这些选项。 Ray2U
标签: java swing jtable jcombobox tablecelleditor