简体   繁体   中英

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

In my first column in jtable I have set the setCellEditor (new DefaultCellEditor(comboBox).

All I want is when I open the the table, I find the first cell which has the editable Combobox with the cursor blinking ready for input.

Here's the code of what I have done what I have done.

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) {....}

Its pretty simple, JTable always has focus by default. To surrender the focus to the cells there is a method Jtable.setSurrendersFocusOnKeystroke(true).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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