JTABLE Issue

JTABLE Issue

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.

View Answers

May 31, 2013 at 6:57 PM

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...


May 31, 2013 at 6:58 PM

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.









Related Tutorials/Questions & Answers:
JTABLE Issue
JTABLE Issue  Hi Eveyone, I am developing a small application on Swing-AWT. I have used JTABLE to show data. There is "input field" and "search... on basis of input data provided in input field. For JTABLE is on some other
JTable
JTable  Values to be displayed in JTextfield when Clicked on JTable Cells
Advertisements
JTable
JTable  i want to delete record from JTable using a MenuItem DELETE. and values of JTable are fetched from database....please reply soon
JTable
JTable   how to select a definite cell which containing a similar text containg to the one which the user entering from a jtable at runtime in java
JTable
JTable  need to add values to a JTable having 4 coloumns ,2 of them are comboboxes
JTable
JTable  Hello, i cannot display data from my table in the database to the cells of my JTable. please help me
jtable
jtable  how to get the values from database into jtable and also add a checkbox into it and then when selected the checkbox it should again insert into database the selected chewckbox.plzz help
jtable
jtable  hi Sir i am working netbeans IDE,I have a jtable when i insert values in jtable then i am unable to print all inserted values,For eg if i insert 1,2,3,4,5,6,7,8 values then , i am getting output
jtable
jtable  hey i have build a form and i m also able to add data from database to jtable along with checkbox.the only problem is that if i select multiple checkboxes the data doesnt get inserted into new database and if only one
JTable
JTable  Hi I have problems in setting values to a cell in Jtable which is in a jFrame which implements TableModelListener which has a abstract method tableChanged(TableModelEvent e) . I'll be loading values from data base when
JTable
"}; JTable table=new JTable(data,labels); JScrollPane pane=new JScrollPane
jtable problem
jtable problem  how to make a cell text hypertext
JTABLE OF JAVA
JTABLE OF JAVA  i have a jtable in java,i have used checkbox in jtable. now i want to add(submit) only those records that i have checked by checkbox how? i want small example with coding
Jtable-Java
Jtable-Java  Hi all,I have a Jtable And i need to clear the data in the table .I only Need to remove the data in the table.not the rows.Please help me
Swings JTable
Swings JTable  add values to JTable with four coloums,two of them are comboboxes
sum in JTable
sum in JTable  how to calculate sum from JTable's one field like total
JFormattedTextField issue
JFormattedTextField issue  Can someone help me to understand how to place a JFormattedTextField inside a JComboBox. The JComboBox is in one of the JTable columns. The JComboBox holds a list of item and I also want the JComboBox
jtable query
jtable query  I need a syntax...where i could fetch the whole data from the database once i click the cell in jtable...and that must be displayed in the nearby text field which i have set in the same frame
java jtable
java jtable  Hello Sir, I am developing a desktop application in which i have to display database records in jtable .now I want to read only... that in jtable. plz help me with the code
regarding jtable...
regarding jtable...  sir, im working with jtables. i wanted to populate a jtable from the database and when i click any row it should add a container... a container on the jtable. kindly help me sir. thanks in advance regards, rajahari
ABOUT Jtable
ABOUT Jtable  My Project is Exsice Management in java swing Desktop Application. I M Use Netbeans & Mysql . How can retrive Data in Jtable from Mysql Database in Net Beans
JTable duplicate values in row
JTable duplicate values in row  JTable duplicate values in row
How to add JTable in JPanel
How to add JTable in JPanel  How to add JTable in JPanel
jtable insert row swing
jtable insert row swing  How to insert and refresh row in JTable?   Inserting Rows in a JTable example
issue on jcombobox
issue on jcombobox  i have JTextfield and JComboBox. there are several values in combobox.when i select a value from combobox how to make textfiled as a combobox.only few values in the combobox need this functionality. need
jdialogbox issue.
jdialogbox issue.  i have one button.when clicked on it has to show a dialog box.but even click on it several times it has to open dialog box only once. if it is already not opened atleast once,then only it has to open dialog
Issue with Javascript
Issue with Javascript  Hi I created arrays in javascript in following way var myimages= new Array(); myimages[0]="iphone_pushups.png"; myimages[1]= "cricket.png"; myimages[2]= "july.png"; myimages[3]= "matrix.png
REPORT WITH JTABLE
(Exception e){} JTable table = new JTable(data, columnNames); JScrollPane scrollPane
JTable - JDBC
JTable   Hello..... I have Jtable with four rows and columns and i have also entered some records in MsSql database. i want to increase Jtable's... { JFrame f; JPanel p; JLabel l; JTextField tf; JButton btn; JTable tb
jtable
JTable - Swing AWT
JTable row selection event   Hi, it will be great if someone can share an example of row selection event in JTable
JTable hold different components
JTable hold different components  create Jtable with two coloums,one coloumn is combobox and other normal
JTable Pagination
JTable Pagination  Hello , I have the following Code. I am able to fetch the Data from the Database. But i need to implement pagination for the same. Can someone please help me out with this ? I have tried out many things from
JTable in java - Java Beginners
JTable in java  Sir , I have created an application with a JTable showing the records of an MS Access database table. On the same frame I have... given one , JTable table; ....... .... table.print(); Here the error
JTable - Java Beginners
JTable  can we merge two cells of one row in jtable
restrict jtable editing
restrict jtable editing  How to restrict jtable from editing or JTable disable editing?   public class MyTableModel extends...){ return false; } }   Disabling User Edits in a JTable Component
JTable - Java Beginners
JTable search example  I've problem about JTable, i wan to search records when i types words in JTable cell,can u give the source code.I'm Beginner and i start begins learning java programming.Please send the example into my
regarding JTable - JDBC
regarding JTable  how to populate a JTable with mysql data after clicking JButton please explain with the example
JAVA DATABASE CONNECTION WITH JTABLE
JAVA DATABASE CONNECTION WITH JTABLE  HOw To Load Database Contents From Access Database to JTable without using Vector
jtable query compare with date
jtable query compare with date  how to transfer daytable data to monthtable when complete a month
Jtable with servlet - Java Beginners
Jtable with servlet   Actually I have embedded the html... time I want the JTable to display the record saved in the database,the JTable... of the page and the bottom half will consists of JTable to display the record information
Jbutton in JTable cells
Jbutton in JTable cells  I am a Java Beginner... I want a code in which I display data in Jtable from database and correspondingly a JButton for each cell and when i click on that button a new window should open
JTable populate with resultset.
JTable populate with resultset.  How to diplay data of resultset using JTable?   JTable is component of java swing toolkit. JTable class is helpful in displaying data in tabular format. You can also edit data. JTable
JTable with Date Picker
JTable with Date Picker  Hi, I'd like to implement the following but I have no idea where to start: I have a JTable with 1 column containing... in the backing object(that populated the JTable
jtable query compare with date
jtable query compare with date  how to transfer daytable data... in which i have to display database records in jtable .now I want to transfer particular data which month is over to another jtable. plz sir give me the code
jtable query compare with date
jtable query compare with date  how to transfer daytable data... in which i have to display database records in jtable .now I want to transfer particular data which month is over to another jtable. plz sir give me the code
jtable query compare with date
jtable query compare with date  how to transfer daytable data... in which i have to display database records in jtable .now I want to transfer particular data which month is over to another jtable. plz sir give me the code
jtable displays search results
jtable displays search results   hi sir can u send me full source code for displaying search results into jtable from database n jtable n search button must be within same frame but in different Panel and the size of the frame
JTable - Swing AWT
JTable  Hi Deepak, i want to display the Jtable data... how could i show jtable data on the console. Thanks, Prashant   Hi...*; public class JTableToConsole extends JFrame { public Object GetData(JTable
add XMl to JTable
add XMl to JTable  Hi.. i saw the program of adding add XMl to JTable using DOM parser,but i need to do that in JAXB ,is it possible to do? help me

Ads