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 can't seem to make it work. Please help. Below is the code for my table. Any help would be appreciated greatly. Thanks,
Lisa
Code:
private void LoadSTCGTable(){
//Connect to database
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"
}
));
//Add Checkbox column
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)
{}
}