Hi Eveyone,
I am developing a small application on Swing-AWT. I have used JTABLE to show data. There is "input field" and "search button " on a frame , by clicking this search button data will be retrived from DB on basis of input data provided in input field. For JTABLE is on some other frame which gets open on click of search button. this JTable also has a back button to go back for another search.
Now issue is -- when I put some input data for eg. Customer's name and click to search button data comes on JTable according to customer name. But when I click on Back button and provide some other name to get new data on JTable it shows old customer's data on JTable till the time I don't close the whole application.
Please help me to get it work properly.
hi friend,
Try the following code may this will be helpful for you, may this will be helpful for you.
package net.roseindia.jtableExample; import javax.swing.*; import javax.swing.table.DefaultTableModel; import java.awt.*; import java.sql.*; import java.awt.event.*; public class SearchResultWithBackButton implements ActionListener{ JFrame frame; JInternalFrame internalFrame1; JTextField textbox; JLabel label; JButton button, button1; JPanel panel; static JTable table; JDesktopPane desktop = new JDesktopPane(); String driverName = "com.mysql.jdbc.Driver"; String url = "jdbc:mysql://localhost:3306/record"; String userName = "root"; String password = "root"; String[] columnNames = {"Roll No", "Name", "Class", "Section"}; public void createUI() { frame = new JFrame("Database Search Result"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setLayout(null); textbox = new JTextField(); textbox.setBounds(120,30,150,20); label = new JLabel("Enter your roll no"); label.setBounds(10, 30, 100, 20); button = new JButton("search"); button.setBounds(120,130,150,20); button.addActionListener(this); frame.add(textbox); frame.add(label); frame.add(button); frame.setVisible(true); frame.setSize(500, 400); } public void actionPerformed(ActionEvent ae) { button = (JButton)ae.getSource(); System.out.println("Showing Table Data......."); showTableData(); }
Continue...
public void showTableData() { internalFrame1 = new JInternalFrame("Database Search Result", true, true, true, true); internalFrame1.setLayout(new GridLayout(0,1)); //TableModel tm = new TableModel(); DefaultTableModel model = new DefaultTableModel(); model.setColumnIdentifiers(columnNames); //DefaultTableModel model = new DefaultTableModel(tm.getData1(), tm.getColumnNames()); //table = new JTable(model); button1 = new JButton("Back"); button1.setBounds(200, 200, 100, 20); button1.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent ae) { internalFrame1.dispose(); createUI(); } }); JPanel panel = new JPanel(); panel.add(button1); table = new JTable(); table.setModel(model); table.setAutoResizeMode(JTable.AUTO_RESIZE_ALL_COLUMNS); table.setFillsViewportHeight(true); int width = internalFrame1.getWidth(); int height = internalFrame1.getHeight(); table.setSize(width, height); JScrollPane scroll = new JScrollPane(table); scroll.setHorizontalScrollBarPolicy( JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED); scroll.setVerticalScrollBarPolicy( JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED); String textvalue = textbox.getText(); String roll= ""; String name= ""; String cl = ""; String sec = ""; try { Class.forName(driverName); Connection con = DriverManager.getConnection(url, userName, password); String sql = "select * from student where rollno = "+textvalue; PreparedStatement ps = con.prepareStatement(sql); ResultSet rs = ps.executeQuery(); int i =0; if(rs.next()) { roll = rs.getString("rollno"); name = rs.getString("name"); cl = rs.getString("class"); sec = rs.getString("section"); model.addRow(new Object[]{roll, name, cl, sec}); i++; } if(i <1) { JOptionPane.showMessageDialog(null, "No Record Found","Error", JOptionPane.ERROR_MESSAGE); } if(i ==1) { System.out.println(i+" Record Found"); } else { System.out.println(i+" Records Found"); } } catch(Exception ex) { JOptionPane.showMessageDialog(null, ex.getMessage(),"Error", JOptionPane.ERROR_MESSAGE); } internalFrame1.add(scroll); //internalFrame1.add(button1); internalFrame1.add(panel); internalFrame1.setVisible(true); internalFrame1.setSize(400,300); desktop.add(internalFrame1); frame.add(desktop); frame.setContentPane(desktop); desktop.setDragMode(JDesktopPane.OUTLINE_DRAG_MODE); } public static void main(String args[]) { SearchResultWithBackButton sr = new SearchResultWithBackButton(); sr.createUI(); } }
Thanks.
Ads