Results 1 to 4 of 4
  1. #1
    pratim is offline Member
    Join Date
    Mar 2011
    Posts
    8
    Rep Power
    0

    Default Could not able to refresh JTable.

    Sir, i need some suggestion regarding how to repaint a JTable with new values.

    i could able to retrieve data from database and displayed in a JTable, which is in a scrollPane. i have a JButton in a JPanel and now i need to replace those values in JTable with new values from the database by clicking in the JButton.

    i found little bit of problem. First i displayed 8 rows of values in the JTable. I need to replace those values with new 8 rows after clicking the JButton. But, instead of replacing the old values, when i clicked the JButton it adds new 8 rows with the old 8 rows, giving me 16 rows of values.

    could i able to explain you properly. If so, then please give me some suggestion to overcome my problem.

    thank you in advance.

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is online now Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,303
    Rep Power
    20

    Default

    To get better help sooner, post a SSCCE that clearly demonstrates your problem.

    Use code tags to post codes -- [code]CODE[/code] will display as
    Java Code:
    CODE
    Or paste your code between a pair of {code} tags.

    db

  3. #3
    pratim is offline Member
    Join Date
    Mar 2011
    Posts
    8
    Rep Power
    0

    Default

    I have 3 classes. Class Data where i query and returned the result as a jtable. here i have given only a part of my coding.


    Java Code:
    public class Data{
    public JTable getDataStatus(){
    	
    	try{
    		con=getConnection();
    		stmt=con.createStatement();
    		rs=stmt.executeQuery(SELECT * from ........);
    		
    		ResultSetMetaData md = rs.getMetaData();
    		
    		int columns = md.getColumnCount();
    		for (int i = 1; i <= columns; i++) {
    		columnNames.addElement( md.getColumnName(i) );
    		}
    		while (rs.next()) {
    
    		Vector row = new Vector(columns);
    		row = new Vector(columns);
    		for (int i = 1; i <= columns; i++) {
    		row.addElement( rs.getObject(i) );
    		}
    		data.addElement( row );
    		
    		}
    	}catch(Exception e){
    		e.printStackTrace();
    	
    	}finally{
    		closeConnection(con, stmt, rs);
    	}
    	JTable table= new JTable(data, columnNames);
    	return table;	
    } 
    }
    And the second Class is where i called the method of the Data class.

    Java Code:
    public class DaysPanel{
    
    JScrollPane scrollPane;
    JPanel dataPanel;
    JTable jtab;
    TableColumn column = null;
    
    Data dao=new Data();
    
    private JPanel getDataPanel(){
    dataPanel=new JPanel();
    dataPanel.setLayout(new BorderLayout());
    
    jtab=dao.getDataStatus();
    jtab.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
    jtab.setRowHeight(40);
    
    
    for (int i = 0; i < 7; i++) {
        column = jtab.getColumnModel().getColumn(i);
        if (i == 5) {
            column.setPreferredWidth(140); //sixth column is bigger
        }else if(i==2){
        	column.setPreferredWidth(80);
        }   
        else {
            column.setPreferredWidth(70);
        }
    }
    
    scrollPane= new JScrollPane( jtab );
    scrollPane.setBackground(Color.WHITE);
    
    JButton nb=new JButton("Next>>");
    nb.addActionListener(new ActionListener(){
    	public void actionPerformed(ActionEvent event)
    	{
    	 // here what to write? i have tried to change
            // this part so wrong codes are here in this part.
            // i am not sure about what to write in
            //actionPerformed method().....
    
                    jtab.invalidate();
    		jtab.revalidate();
    		jtab.repaint();
    		
    		scrollPane.invalidate();
    		scrollPane.revalidate();
    		scrollPane.repaint();
    
    		dataPanel.invalidate();
    		dataPanel.revalidate();
    		dataPanel.repaint(); 
    		
    		jtab=dao.getDataStatus();
    	}
    	});
     
    dataPanel.add(scrollPane,BorderLayout.CENTER);
    dataPanel.add(nb,BorderLayout.SOUTH);
    
    return dataPanel;
    }

    And the third class where i used the dataPanel to show in JFrame. The third class i am not showing.

    I could not able to perform the refreshing of JTable. i need to display new data by replacing old ones from the JTable. Please give me some suggestion. i am still trying. I can able to understand my problem but i do not have any idea to proceed.

    Thank you.

  4. #4
    DarrylBurke's Avatar
    DarrylBurke is online now Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,303
    Rep Power
    20

    Default

    That's not a SSCCE. did you go through the link?

    All you need to reproduce your problem are two sets of hardcoded data (String[][]) and a JTable in a JScrollPane in a JFrame, along with a JButton that attempts to substitute one set of data with the other and shows the new data being appended instead. Come up with that and your problem will be solved here.

    db

Similar Threads

  1. JTable refresh button.doClick()
    By javaevgny in forum AWT / Swing
    Replies: 6
    Last Post: 01-14-2011, 04:28 PM
  2. Ye Olde jTable Refresh Question...
    By Cynot in forum New To Java
    Replies: 4
    Last Post: 05-26-2010, 11:09 PM
  3. Inside a Timer thread loop,how to refresh a JTable in swing
    By neha_negi in forum Threads and Synchronization
    Replies: 3
    Last Post: 09-04-2009, 01:45 AM
  4. Dynamic Refresh in JTable every 5 Minutes
    By britto_bicsjohn in forum AWT / Swing
    Replies: 3
    Last Post: 08-07-2009, 05:16 AM
  5. how to refresh data of the JTable
    By paty in forum JDBC
    Replies: 3
    Last Post: 08-17-2008, 12:01 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •