在jtable的第一列中,我设置了setCellEditor(new DefaultCellEditor(comboBox)。
我想要的只是当我打开桌子时,我发现第一个具有可编辑Combobox的单元格,光标闪烁准备输入。
以下是我所做的事情的代码。
try {
//Already registered so open mainWindow
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
final mainWindow mainn = new mainWindow();
//get the institution name and display it as the title
String companyName = dbutils.checker.getCompanyName();
//disable all items on system
disable();
mainn.setTitle("[" + companyName + "]");
mainn.setExtendedState(Frame.MAXIMIZED_BOTH);
TableColumn items = mainWindow.salesTable.getColumnModel().getColumn(0);
final JComboBox comboBox = new JComboBox();
comboBox.addItem("250AZ0002C20140725");
comboBox.addItem("250AZ0001C20140725");
comboBox.addItem("250AZ0003C20140725");
comboBox.addItem("250AZ0004C20140725");
comboBox.setEditable(true);
items.setCellEditor(new DefaultCellEditor(comboBox));
ComboBoxEditor editor = comboBox.getEditor();
JTextField textField = (JTextField)editor.getEditorComponent();
textField.setCaretPosition(0);
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
mainWindow.salesTable.requestFocus();
comboBox.requestFocus();
mainWindow.salesTable.changeSelection(0, 0, false, false);
mainWindow.salesTable.editCellAt(0, 0);
}
});
readBarcode();
mainn.setVisible(true);
} catch (Exception ex) {....}
答案 0 :(得分:0)
非常简单,JTable默认情况下始终具有焦点。要将焦点交给单元格,有一个方法Jtable.setSurrendersFocusOnKeystroke(true)。