Results 1 to 2 of 2
  1. #1
    markw8500's Avatar
    markw8500 is offline Senior Member
    Join Date
    Jul 2008
    Location
    Pennsylvania, USA
    Posts
    136
    Rep Power
    0

    Default JComboBox in JTable and Cant get Focus...

    I put an editable combo box in a Jtable and I can not transfer the focus to the combo box... I also overroad my combobox because I need to filter the input... I have searched for hours and cant find any solution...

    Here is my code:

    Java Code:
    /*
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     */
    package jtablecombobox;
    
    import comboBoxes.JTableComboBox;
    import comboBoxes.RowEditorModel;
    import java.awt.BorderLayout;
    import java.awt.Component;
    import javax.swing.AbstractCellEditor;
    import javax.swing.JComboBox;
    import javax.swing.JComponent;
    import javax.swing.JLabel;
    import javax.swing.JPanel;
    import javax.swing.JTable;
    import javax.swing.JTextField;
    import javax.swing.SwingUtilities;
    import javax.swing.table.DefaultTableModel;
    import javax.swing.table.TableCellEditor;
    import javax.swing.table.TableCellRenderer;
    import javax.swing.table.TableColumn;
    
    /**
     *
     * @author root
     */
    public class DisplayPanel extends JPanel {
    
        private String schema;
        private JTable jt;
        private DefaultTableModel myModel;
        private JTableComboBox jtcb;
    
        public DisplayPanel() {
            String[] items = new String[]{"", "1.85", "1.50", "1.35"};
            jtcb = new JTableComboBox(items, 2, 10);
    
            initComponents();
    
        }
    
        private void initComponents() {
    
            myModel = new DefaultTableModel();
            myModel.setColumnCount(3);
            myModel.setRowCount(10);
    
            jt = new JTable(myModel);
    
            //RowEditorModel rm = new RowEditorModel();
    
            //jt.setRowEditorModel(rm);
            //rm.addEditorForRow(0);
            //rm.addEditorForRow(1);
            //jt.setRowEditorModel(new RowEditorModel(2));
    
            //jt.setR
    
    
            TableColumn column = jt.getColumnModel().getColumn(2);
    
            String[] values = new String[]{"", "1.85", "1.50", "1.35"};
            column.setCellEditor(new MyComboBoxEditor(values));
            column.setCellRenderer(new MyComboBoxRenderer(values));
            //column.setCellRenderer(new DataFieldRenderer());
    
            JPanel jp = new JPanel(new BorderLayout());
    
    
            jp.add(jt, BorderLayout.CENTER);
    
            this.add(jp, BorderLayout.CENTER);
    
    
        }
    
    
        public class MyComboBoxRenderer extends JComboBox implements TableCellRenderer {
    
            JComponent component = new JTextField();
            private JTableComboBox cb;
    
            public MyComboBoxRenderer(String[] items) {
    
                //super(items);
    
                cb = new JTableComboBox(items, 2, 10);
            }
    
            public Component getTableCellRendererComponent(JTable table, Object value,
                    boolean isSelected, boolean hasFocus, int row, int column) {
                if (isSelected) {
    
                    jtcb.setForeground(table.getSelectionForeground());
                    jtcb.setBackground(table.getSelectionBackground());
                } else {
                    jtcb.setForeground(table.getForeground());
                    jtcb.setBackground(table.getBackground());
                }
    
    
                if (hasFocus) {
    
                    if (table.isEditing()) {
                        table.getCellEditor().cancelCellEditing();
                    }
    
                    //table.add(cb);
                    //table.transferFocus();
                    //cb.requestFocus();
                    forceFocus();
                    //jtcb.requestFocus();
                    //jtcb.getEditor().getEditorComponent().requestFocus();
                    System.out.println(table.hasFocus());
    
    
    
    
                }
    
                ///jtcb.getMyDF().setSetText(true);
                //jtcb.setText(true);
                jtcb.setSelectedItem(value);
    
    
    
                return jtcb;
            }
        }
    
        public class MyComboBoxEditor extends AbstractCellEditor implements TableCellEditor {
    
            JComponent component = new JTextField();
            private JTableComboBox cb;
    
            public MyComboBoxEditor(String[] items) {
    
                cb = new JTableComboBox(items, 2, 10);
                cb.setText(true);
            //cb.requestFocusInWindow();
    
            }
    
            public Object getCellEditorValue() {
    
                cb.setText(true);
                //System.out.println(cb.hasFocus());
    
                if (cb.getSelectedItem().toString().isEmpty()) {
    
                    return cb.getEditor().getEditorComponent().getAccessibleContext().getAccessibleEditableText().getTextRange(0, cb.getEditor().getEditorComponent().getAccessibleContext().getAccessibleEditableText().getCharCount());
                }
    
    
    
    
                return cb.getSelectedItem();
            //throw new UnsupportedOperationException("Not supported yet.");
            }
    
            public Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected, int row, int column) {
    
    
                cb.requestFocusInWindow();
    
                cb.getMyDF().clearDocument();
                cb.setSelectedIndex(0);
                cb.setText(false);
                return cb;
            //throw new UnsupportedOperationException("Not supported yet.");
            }
        }
    
        public void forceFocus() {
    
            SwingUtilities.invokeLater(new Runnable() {
    
                public void run() {
    
                    jt.add(jtcb);
    
    
                    jt.transferFocus();
                    jtcb.requestFocus();
                    jtcb.getEditor().getEditorComponent().requestFocus();
                //jt.revalidate();
                //jt.repaint();
    
                }
            });
    
    
    
        }
    }
    Any help is appreciated...

  2. #2
    markw8500's Avatar
    markw8500 is offline Senior Member
    Join Date
    Jul 2008
    Location
    Pennsylvania, USA
    Posts
    136
    Rep Power
    0

    Default

    Never Mind... I finally figured it out... Here is the solution:


    Java Code:
    public class MyComboBoxRenderer extends JComboBox implements TableCellRenderer {
    
            JComponent component = new JTextField();
            private JTableComboBox cb;
    
            public MyComboBoxRenderer(String[] items) {
    
                //super(items);
    
                cb = new JTableComboBox(items, 2, 10);
            }
    
            public Component getTableCellRendererComponent(JTable table, Object value,
                    boolean isSelected, boolean hasFocus, int row, int column) {
                if (isSelected) {
    
                    jtcb.setForeground(table.getSelectionForeground());
                    jtcb.setBackground(table.getSelectionBackground());
                } else {
                    jtcb.setForeground(table.getForeground());
                    jtcb.setBackground(table.getBackground());
                }
    
    
    
                if (hasFocus && column==2) {
    [B]
                    jt.editCellAt(jt.getSelectedRow(), 2);
                    Component myEditor = jt.getEditorComponent();
    
                    myEditor.requestFocus();
    [/B]
    
                }
    
                jtcb.setSelectedItem(value);
    
    
    
                return jtcb;
            }
        }

    From everything I have read, you must first put the cell into editing mode by "JTable.editValueAt(row, col)", then get the editor component and request focus... So the key code is:
    Java Code:
                    jt.editCellAt(jt.getSelectedRow(), columnOfComboBox);
                    Component myEditor = jt.getEditorComponent();
    
                    myEditor.requestFocus();

Similar Threads

  1. JComboBox in Jtable
    By maneesh in forum AWT / Swing
    Replies: 3
    Last Post: 12-09-2009, 08:37 PM
  2. Replies: 0
    Last Post: 11-14-2008, 04:14 AM
  3. Replies: 2
    Last Post: 05-15-2008, 10:15 AM
  4. JTable Focus Traversal
    By helios_lie in forum AWT / Swing
    Replies: 1
    Last Post: 12-20-2007, 10:27 AM
  5. Focus
    By Marty in forum AWT / Swing
    Replies: 1
    Last Post: 05-31-2007, 02:16 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •