繁体   English   中英

我如何将焦点设置到jtable的第一个单元格以及我也将CellEditor设置在comboBox的第一列中

[英]How do i set focus to the first cell of jtable and which the first column I have also set the CellEditor to a comboBox

在jtable的第一列中,我设置了setCellEditor(新的DefaultCellEditor(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) {....}

JTable非常简单,默认情况下始终具有焦点。 要使焦点移到单元格上,可以使用Jtable.setSurrendersFocusOnKeystroke(true)方法。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM