I am still having problems with regrds to my code. When I select the first combo box and select a table Name that begins with 0 it thows an Exception but when I reselect the combo Box to change the table Name it thows the same Exceptiomn again before I can change the table Name. How can I get my code so that it thows the Exception only once (the first Exception) ? I have posted on this topic before so I have been looking into it, but just can not find the answer.
Please could someone help me
Thankyou
Ben
import javax.swing.*; import javax.swing.table.*; import java.awt.event.*; import java.awt.*; import java.awt.event.*; import javax.swing.*; import java.sql.*; public class ComboBoxTable{ static Connection conn2; static DatabaseMetaData DMD; static JComboBox group = new JComboBox(); static JComboBox groupList = new JComboBox(); public static void main(String[] args) {
} catch (SQLException se ) { System.out.println( "SQL Exception: " + se ) ; } catch (Exception e ) { System.out.println( e ) ; }
group.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { String tbl = (String)group.getSelectedItem(); try { Statement st=conn2.createStatement(); st.execute("SELECT * FROM "+tbl+" WHERE 1=0"); ResultSet rs=st.getResultSet(); ResultSetMetaData md=rs.getMetaData(); groupList.removeAllItems(); for (int i=1;i<=md.getColumnCount();i++) { groupList.addItem(md.getColumnName(i)); } st.close() ;
} catch (SQLException se ) { System.out.println( "SQL Exception: " + se ) ; } catch (Exception ex ) { System.out.println( ex ) ; } } });
int j=10; String[][] data = new String[j][2]; for (int k=0; k<j; k++){ String[] row = {"",""}; data[k] = row; }
String[] colNames = {"Column A", "Column B"}; JTextField jtf = new JTextField(); JTable tbl = new JTable(data, colNames); // Create the combo box editor DefaultCellEditor editor = new DefaultCellEditor(groupList); // ADDED IN MY ATTEMPT TO HAVE TO COLUMNS WITH COMBOBOXES DefaultCellEditor editor2 = new DefaultCellEditor(group); // Assign the editor to the second column TableColumnModel tcm = tbl.getColumnModel(); tcm.getColumn(0).setCellEditor(editor2); // ADDED tcm.getColumn(1).setCellEditor(editor); // Set column widths tcm.getColumn(0).setPreferredWidth(200); tcm.getColumn(1).setPreferredWidth(100); // Set row heighht tbl.setRowHeight(20); tbl.setAutoResizeMode(JTable.AUTO_RESIZE_OFF); tbl.setPreferredScrollableViewportSize(tbl.getPreferredSize()); JFrame f = new JFrame("ComboBoxDemo"); f.getContentPane().add(new JScrollPane(tbl), "Center");