How can you use Keyboard Shortcuts for a JComboBox within a JTable without using F2.

I am brand new to Java and I can't seem to figure this out.  I have a JTable with 4 columns.  The first column is a jCheckBox which is working fine.  The other three are JComboBoxes.  They work fine when you click them and select from the list, but our end users only want to navigate with the keyboard.  They want to be able to tab to the column and just start typing the first letter of the item that they want to choose from the list and have that pop up the list and scroll to the first item in the list that starts with the letter the typed.  Does anyone know how to do this?  I have been playing with this for a week now and I am getting frustrated.  Please help.  Below is the code for my table.  Any help would be appreciated greatly.  Thanks,
Lisa
private void LoadSTCGTable(){
        try {
                connection = ConnecttoDB.connect();
                // Tell me why I couldn't connect
            } catch (ClassNotFoundException ex) {
                ex.printStackTrace();
            } catch (SQLException ex) {
                ex.printStackTrace();
            }  
              
     try {
            // Get listing of Squad Types
            tblSTCG.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {new Boolean(false), null, null, null},
            },
            new String [] {
                "SELECT", "SQUAD TYPE", "SQUAD CLASS", "SQUAD GROUP"
            }
         ));
           
    
           tblSTCG.getColumnModel().getColumn(0).setCellEditor(tblSTCG.getDefaultEditor(Boolean.class)); 
           tblSTCG.getColumnModel().getColumn(0).setCellRenderer(tblSTCG.getDefaultRenderer(Boolean.class));       
           tblSTCG.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
           
           Whichquerytoread = 1;
           GetSql();
           sql = textfileinfo;
           stmt = connection.createStatement();
           rs = stmt.executeQuery(sql);
           md = rs.getMetaData();
           typelist = new ArrayList();
           // Loop Through Results
           typelist.add(new PairedDescriptionCodeDesc("",""));
           while (rs.next())
            {
              int i = 1;
             typelist.add( new PairedDescriptionCodeDesc(rs.getString(2),rs.getString(1))); 
                     
            }
          s1 = new TreeSet(typelist);
          
          typelist = new ArrayList(s1);
          AllTypeList = new PairedDescriptionCodeDesc[typelist.size()]; 
    	
          for (int i = 0; i<typelist.size(); i++)
             AllTypeList[i]=(PairedDescriptionCodeDesc)typelist.get(i); 
             rs.close(); 
         }
           catch (SQLException ex) {
           ex.printStackTrace();
         } 
        Vector typedata = new Vector();
         for (int i=0;i<typelist.size();i++) 
         {
            typedata.addElement((PairedDescriptionCodeDesc)typelist.get(i));
         }
         cmboType = new JComboBox();
         cmboType.setModel(new DefaultComboBoxModel(typedata));
         cmboType.putClientProperty("JComboBox.isTableCellEditor", Boolean.TRUE);
         cmboType = new JComboBox(AllTypeList);
         cmboType.setBorder(BorderFactory.createEmptyBorder()); 
         squadcol = tblSTCG.getColumnModel().getColumn(1);
         DefaultCellEditor cmboTypeEditor = new DefaultCellEditor(cmboType);
         cmboTypeEditor.addCellEditorListener(new NewRowCellEditorListener(tblSTCG));
         squadcol.setCellEditor(cmboTypeEditor);
     
        try {
            // Get listing of Squad Class
           Whichquerytoread = 2;
           GetSql();
           sql = textfileinfo;
           stmt = connection.createStatement();
           rs = stmt.executeQuery(sql);
           md = rs.getMetaData();
           classlist = new ArrayList();
           // Loop Through Results
           classlist.add(new PairedDescriptionCodeDesc("",""));
           while (rs.next())
            {
             classlist.add(new PairedDescriptionCodeDesc(rs.getString(2),rs.getString(1)));                     
            }
          s1 = new TreeSet(classlist);
          
          classlist = new ArrayList(s1);
          AllClassList = new PairedDescriptionCodeDesc[classlist.size()]; 
    	
          for (int i = 1; i<classlist.size(); i++)
             AllClassList[i]=(PairedDescriptionCodeDesc)classlist.get(i); 
             rs.close(); 
        }
           catch (SQLException ex) {
            ex.printStackTrace();
        } 
        Vector classdata = new Vector();
         for (int i=0;i<classlist.size();i++) 
         classdata.addElement((PairedDescriptionCodeDesc)classlist.get(i));
         cmboClass = new JComboBox();
         cmboClass.setModel(new DefaultComboBoxModel(classdata));
         cmboClass = new JComboBox(AllClassList);  
         classcol = tblSTCG.getColumnModel().getColumn(2);
         DefaultCellEditor cmboClassEditor = new DefaultCellEditor(cmboClass);
         classcol.setCellEditor(new DefaultCellEditor(cmboClass)); 
         cmboClassEditor.addCellEditorListener(new NewRowCellEditorListener(tblSTCG));
         try {
            // Get listing of Squad Group
            Whichquerytoread = 3;
           GetSql();
           sql = textfileinfo;
           stmt = connection.createStatement();
           rs = stmt.executeQuery(sql);
           md = rs.getMetaData();
           grouplist = new ArrayList();
           // Loop Through Results
           grouplist.add(new PairedDescriptionCodeDesc("",""));
           while (rs.next())
            {
              int i = 0;
             grouplist.add( new PairedDescriptionCodeDesc(rs.getString(2), rs.getString(1))); 
                     
            }
          s1 = new TreeSet(grouplist);
     
          grouplist = new ArrayList(s1);
          AllGroupList = new PairedDescriptionCodeDesc[grouplist.size()]; 
    	
          for (int i = 0; i<grouplist.size(); i++)
             AllGroupList[i]=(PairedDescriptionCodeDesc)grouplist.get(i); 
              rs.close(); 
         }
           catch (SQLException ex) {
            ex.printStackTrace();
         } 
        Vector groupdata = new Vector();
         for (int i=0;i<grouplist.size();i++) 
         groupdata.addElement((PairedDescriptionCodeDesc)grouplist.get(i));
         cmboGroup = new JComboBox();
         cmboGroup.setModel(new DefaultComboBoxModel(groupdata));
         cmboGroup = new JComboBox(AllGroupList);  
         groupcol = tblSTCG.getColumnModel().getColumn(3);
         DefaultCellEditor cmboEditor = new DefaultCellEditor(cmboGroup);
         cmboEditor.addCellEditorListener(new NewRowCellEditorListener(tblSTCG));
         groupcol.setCellEditor(cmboEditor);
    
         
         tblSTCG.setShowHorizontalLines(false);
         tblSTCG.setShowVerticalLines(false);
                  
         TableColumnModel columnModel = tblSTCG.getColumnModel();
         TableColumn column;
         tblSTCG.getColumnModel().getColumn(0).setPreferredWidth(5);
         tblSTCG.getColumnModel().getColumn(1).setPreferredWidth(100);
         tblSTCG.getColumnModel().getColumn(2).setPreferredWidth(100);
         tblSTCG.getColumnModel().getColumn(3).setPreferredWidth(100);
         scpSTCG.setViewportView(tblSTCG); 
                 
     }
 
 private class NewRowCellEditorListener implements CellEditorListener
    {
        JTable editingTable;
        public NewRowCellEditorListener(JTable table)
        {
            editingTable = table;
        }
        public void editingStopped(ChangeEvent e)
        {
            if(editingTable.getRowCount() == editingTable.getSelectedRow() + 1)
            {
                ((DefaultTableModel)editingTable.getModel()).addRow(new Object [] {null, null, null, null});                
            }
        }
        public void editingCanceled(ChangeEvent e)
        {}
    }

Open in new window

lisaelmoreAsked:
Who is Participating?
 
lisaelmoreConnect With a Mentor Author Commented:
A kind person on another forum helped me figure this out.  Here is what I had to do.

combobox = new JComboBox();
combobox.addAncestorListener(new AncestorListener() {
   public void ancestorAdded(AncestorEvent event) {
         //make sure combobox handles key events
    combobox.requestFocusInWindow();
}

                  public void ancestorMoved(AncestorEvent event) {

                  }

                  public void ancestorRemoved(AncestorEvent event) {
                  }
            });

0
 
CEHJCommented:
0
 
lisaelmoreAuthor Commented:
This doesn't seem to be exactly what I am looking for, My JComboBox is a cell inside a JTable.
Thanks for the help though.
0
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 
CEHJCommented:
It doesn't matter where the combo box is situated
0
 
lisaelmoreAuthor Commented:
My problem seems to be getting the focus set to the ComboBox in the cell.  I have used comboboxes outside a JTable and when you get focus on the combobox and type a letter or hit the down arrow, the list pops up to select from.  This is what I can't seem to duplicate from within the JTable.  My combo box works fine when I click on it to get focus, but not when I tab to it.   I am probably not wording my question correctly (Please forgive me, I am new to the Java world.  I don't even know what to ask for.)  
0
 
CEHJCommented:
Take a look at this example - it works as you expect:

http://www.java2s.com/Code/Java/Swing-JFC/TabinandoutJComboBoxinJTable.htm
0
 
lisaelmoreAuthor Commented:
Thanks so much.  I will give it a try.
0
 
lisaelmoreAuthor Commented:
I tried the example that you gave me and it does the exact same thing that mine does.  When you tab to the JComboBox within the table (The Gender Column) you cannot just press the down arrow to get the popup to show.  You have to either click the cell or hit the F2 key before the column gets focus.  Any other ideas?
0
 
CEHJCommented:
Sorry i missed that bit about the down arrow. Actually on my machine it moves to another row (the cell below) when down arrow is hit. Is it the same for you?
0
 
lisaelmoreAuthor Commented:
That's what happens to me too.  It is like the focus is on the table and not the combobox.
This thing is driving me crazy.
0
 
CEHJCommented:
Well you could be right about that - i'm not sure. In order to fix the problem, i think you need to first determine whether you *are* right
0
 
objectsCommented:
> It doesn't matter where the combo box is situated

It very mucjh matters where your combo is situated.

>  It is like the focus is on the table and not the combobox.

The focus is on the table.
In a table there isn't actually even a combo there until the editor is activated.

You need to add a listener that activated the editor when the cell gets focus.
0
 
lisaelmoreAuthor Commented:
I'm confused!  (New to Java)  Since my cell editor is overridden by the ComboBoEditor, how can I add a listener to activate the editor.  Where do I add the listener?  To the table?
Sorry for the stupid questions.
Thanks
Lisa
0
All Courses

From novice to tech pro — start learning today.