Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie
Show Data from one JTable to Another
Options
-
08-02-2013 10:42pmI am using an SQL database, I am Connected to one Table and outputting Result to One JTable
i like to know how can i get the Information from Table 2 inside my SQL DB when i Click on a User inside my Java GUI App, To Output onto the Second JTable.0
Comments
-
import java.awt.BorderLayout; import java.awt.event.ActionListener; import java.awt.event.ActionEvent; import javax.swing.event.ListSelectionEvent; import javax.swing.event.ListSelectionListener; import java.awt.event.KeyEvent; import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; import java.sql.SQLException; import java.util.regex.PatternSyntaxException; import javax.swing.JFrame; import javax.swing.JMenu; import javax.swing.JScrollPane; import javax.swing.JTable; import javax.swing.JOptionPane; import javax.swing.JButton; import javax.swing.Box; import javax.swing.JLabel; import javax.swing.JTextField; import javax.swing.RowFilter; import javax.swing.table.TableRowSorter; import javax.swing.table.TableModel; import javax.swing.JMenuBar; import javax.swing.JCheckBoxMenuItem; import javax.swing.JToolBar; import javax.swing.JMenuItem; import javax.swing.JPanel; import javax.swing.GroupLayout; import javax.swing.GroupLayout.Alignment; import javax.swing.LayoutStyle.ComponentPlacement; public class test extends JFrame { // database URL, username and password static final String DATABASE_URL = "jdbc:mysql://localhost/Doctor"; static final String USERNAME = "dbuser"; static final String PASSWORD = "dbpassword"; // default query retrieves all data from authors table static final String DEFAULT_QUERY = "SELECT * FROM Patient"; private ResultSetTableModel tableModel; private JTable table; // create ResultSetTableModel and GUI public test() { super( "Doctors Records" ); // create ResultSetTableModel and display database table try { // create TableModel for results of query SELECT * FROM authors tableModel = new ResultSetTableModel( DATABASE_URL, USERNAME, PASSWORD, DEFAULT_QUERY ); // create JTable based on the tableModel final JTable resultTable = new JTable( tableModel ); JLabel filterLabel = new JLabel( "Filter :" ); final JTextField filterText = new JTextField(); JButton filterButton = new JButton( "Apply Filter" ); Box boxSouth = Box.createHorizontalBox(); boxSouth.add( filterLabel ); boxSouth.add( filterText ); boxSouth.add( filterButton ); // tracking table selection resultTable.getSelectionModel().addListSelectionListener( new ListSelectionListener() { public void valueChanged(ListSelectionEvent e) { firePropertyChange("recordSelected", !isRecordSelected(), isRecordSelected()); } private boolean isRecordSelected() { return resultTable.getSelectedRow() != -1; } }); JMenuBar menubar = new JMenuBar(); JMenu file = new JMenu("File"); file.setMnemonic(KeyEvent.VK_F); JMenuItem save = new JMenuItem("Save"); JMenuItem open = new JMenuItem("Open"); JMenuItem exit = new JMenuItem("Exit"); exit.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { System.exit(0); } }); file.add(save); file.add(open); file.add(exit); JMenu view = new JMenu("Help"); view.setMnemonic(KeyEvent.VK_V); JMenuItem about = new JMenuItem("About"); view.add(about); menubar.add(file); menubar.add(view); setJMenuBar(menubar); JScrollPane scrollPane = new JScrollPane( resultTable ); getContentPane().add( scrollPane, BorderLayout.CENTER ); getContentPane().add( boxSouth, BorderLayout.SOUTH ); final TableRowSorter< TableModel > sorter = new TableRowSorter< TableModel >( tableModel ); resultTable.setRowSorter( sorter ); JPanel panel = new JPanel(); getContentPane().add(panel, BorderLayout.NORTH); JScrollPane scrollPane_1 = new JScrollPane(); JLabel lblPatientHistory = new JLabel("Patient History"); JLabel lblPatientRecords = new JLabel("Patient Records"); GroupLayout gl_panel = new GroupLayout(panel); gl_panel.setHorizontalGroup( gl_panel.createParallelGroup(Alignment.LEADING) .addGroup(gl_panel.createSequentialGroup() .addContainerGap() .addGroup(gl_panel.createParallelGroup(Alignment.LEADING, false) .addComponent(scrollPane_1, GroupLayout.PREFERRED_SIZE, 605, GroupLayout.PREFERRED_SIZE) .addComponent(lblPatientHistory) .addComponent(lblPatientRecords)) .addGap(0)) ); gl_panel.setVerticalGroup( gl_panel.createParallelGroup(Alignment.LEADING) .addGroup(gl_panel.createSequentialGroup() .addComponent(lblPatientHistory) .addGap(2) .addComponent(scrollPane_1, GroupLayout.PREFERRED_SIZE, 87, GroupLayout.PREFERRED_SIZE) .addPreferredGap(ComponentPlacement.RELATED, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addComponent(lblPatientRecords)) ); table = new JTable(); scrollPane_1.setViewportView(table); panel.setLayout(gl_panel); setSize( 776, 561 ); // set window size setVisible( true ); // display window // create listener for filterButton filterButton.addActionListener( new ActionListener() { // pass filter text to listener public void actionPerformed( ActionEvent e ) { String text = filterText.getText(); if ( text.length() == 0 ) sorter.setRowFilter( null ); else { try { sorter.setRowFilter( RowFilter.regexFilter( text ) ); } // end try catch ( PatternSyntaxException pse ) { JOptionPane.showMessageDialog( null, "Bad regex pattern", "Bad regex pattern", JOptionPane.ERROR_MESSAGE ); } // end catch } // end else } // end method actionPerfomed } // end annonymous inner class ); // end call to addActionLister } // end try catch ( SQLException sqlException ) { JOptionPane.showMessageDialog( null, sqlException.getMessage(), "Database error", JOptionPane.ERROR_MESSAGE ); // ensure database connection is closed tableModel.disconnectFromDatabase(); System.exit( 1 ); // terminate application } // end catch // dispose of window when user quits application (this overrides // the default of HIDE_ON_CLOSE) setDefaultCloseOperation( DISPOSE_ON_CLOSE ); // ensure database connection is closed when user quits application addWindowListener( new WindowAdapter() { // disconnect from database and exit when window has closed public void windowClosed( WindowEvent event ) { tableModel.disconnectFromDatabase(); System.exit( 0 ); } // end method windowClosed } // end WindowAdapter inner class ); // end call to addWindowListener } // end DisplayQueryResults constructor // execute application public static void main( String args[] ) { new test(); } // end main } // end class DisplayQueryResults
0
Advertisement