I am having trouble adding a result set to a scroll pane. no matter what I do using getText I get an error. Can someon give me a clue?
Code:
import java.util.*; import java.sql.*; import java.awt.*; import java.awt.event.*; import javax.swing.*; import javax.swing.border.*; import javax.swing.table.DefaultTableModel; public class MusicLibraryForm extends JFrame { //create music library frame private JFrame MusicLibrary = new JFrame(); //for north panel in border layout private JLabel SearchLabel = new JLabel("Enter search information below", SwingConstants.CENTER); private JTextField SearchText = new JTextField(); private JButton SearchButton = new JButton("Search"); private JButton NextRecord = new JButton("Next"); private JButton PreviousRecord = new JButton("Previous"); //for center panel in border layout private JScrollPane SearchResult = new JScrollPane(); private JTable ResultTable = new JTable(); private DefaultTableModel TableModel = new DefaultTableModel(); Border NotesBorder = new LineBorder(Color.GRAY, 1); Border SearchBorder = new LineBorder(Color.BLACK, 1); private MusicLibraryForm() { JPanel SearchInputPanel = new JPanel(new GridLayout(2, 1, 5, 5)); //add search criteria panel and search input panel to north panel SearchInputPanel.add(SearchLabel); SearchInputPanel.add(SearchText); SearchText.setBorder(SearchBorder); JPanel SearchButtonPanel = new JPanel(new BorderLayout(5, 5)); SearchButtonPanel.add(SearchButton, BorderLayout.WEST); SearchButtonPanel.add(NextRecord, BorderLayout.EAST); SearchButtonPanel.add(PreviousRecord, BorderLayout.CENTER); //create complete search panel JPanel NorthPanel = new JPanel(new BorderLayout(5, 5)); //add search criteria panel and search input panel to north panel NorthPanel.add(SearchInputPanel, BorderLayout.WEST); NorthPanel.add(SearchButtonPanel, BorderLayout.EAST); JPanel WestPanel = new JPanel(new GridLayout(1,1,5,5)); ResultTable.setModel(TableModel); SearchResult.add(ResultTable); WestPanel.add(SearchResult, BorderLayout.WEST); //set frame layout setLayout(new BorderLayout(5,5)); //add north, centereast and south panel to the frame add(NorthPanel, BorderLayout.NORTH); add(WestPanel, BorderLayout.CENTER); } public static void main(String[] args) { MusicLibraryForm frame = new MusicLibraryForm(); frame.setTitle("Music Library"); frame.setSize(500, 400); frame.setLocation(50, 50); frame.setResizable(false); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setVisible(true); } private void SearchTable() { Connection con = null; try { Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); String source = "jdbc:odbc:Driver={Microsoft Access Driver (*.mdb)};DBQ=Music Library.mdb"; con = DriverManager.getConnection(source); //create the statement Statement LibraryStatement = con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY); //use the statement to select the data from the MusciLibraryTable. final ResultSet rs = LibraryStatement.executeQuery("SELECT * " + "FROM MusicLibraryTable"); final String UserInput = SearchText.getText(); SearchButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { boolean found = false; try { //iterate over the ResultSet while(rs.next()) { /*I don't know exactly how to get the result set to show up here*/ } } catch (SQLException ex) { ex.printStackTrace(); } if(!found) System.out.println("information not found"); } }); //close the ResultSet rs.close(); } catch (SQLException sqle) {System.err.println(sqle.getMessage());} catch (ClassNotFoundException cnfe) {System.err.println(cnfe.getMessage());} catch (Exception e) {System.err.println(e.getMessage());} finally { try { if ( con != null ) { //close the connection no matter what con.close(); } } catch (SQLException sqle) {System.err.println(sqle.getMessage());} } } }
Comment