Results 1 to 3 of 3

Thread: jtable refresh

  1. #1
    seredi is offline Member
    Join Date
    Sep 2011
    Posts
    14
    Rep Power
    0

    Default jtable refresh

    I am able to load new data to jtable but when I click jcheckbox, the old data comes up.
    pls run the prog and help me where i have gone wrong.
    Java Code:
        import java.awt.BorderLayout;
        import java.awt.EventQueue;
        import java.awt.event.ActionEvent;
        import java.awt.event.ActionListener;
        import javax.swing.JButton;
        import javax.swing.JComponent;
        import javax.swing.JFrame;
        import javax.swing.JPanel;
        import javax.swing.JScrollPane;
        import javax.swing.JTable;
        import javax.swing.table.DefaultTableModel;
    
    	public class MainWindow extends JFrame {
    	JTable table;
    	private static final int CHECK_COL = 3;
           String[] columnNames = {
                "Country", "Capital", "Population in Millions", "Democracy"};
            Object[][] data = {
                {"USA", "Washington DC", 280, false},
                {"Canada", "Ottawa", 32, false},
                {"United Kingdom", "London", 60, false},
                {"Germany", "Berlin", 83, false},
                {"France", "Paris", 60, false},
                {"Norway", "Oslo", 4.5, false},
                {"India", "New Deli", 1046, false}
            };
            DefaultTableModel dtm ;
          // static JScrollPane scrollpane= new JScrollPane();
         public MainWindow() {
            setTitle("Marking of data");
            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            JButton button1 = new JButton("load");
            button1.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                	if(dtm!=null){
                		System .out .println("NOT EMPTY");
                		int c = dtm.getRowCount();
           			// for (int i=c-1; i>=0; i--)
           			// {
           				// dtm.removeRow(i);
           				// table.revalidate();
           			// }
                	  	loadtable();
                         }
                	if(dtm==null){
                		System .out .println("EMPTY");
                		loadtable();
                	}
                }
            });
            
            JButton button = new JButton("check");
            button.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    for (int row = 0; row < table.getRowCount(); row++) {
                        Boolean b = ((Boolean) table.getValueAt(row, CHECK_COL));
                        if (b.booleanValue()) {
                            System.out.print("row " + row + " is " + b + ": ");
                            for (int col = 0; col < table.getColumnCount(); col++) {
                                System.out.print(table.getValueAt(row, col) + " ");
                            }
                            System.out.println();
                        }
                    }
                }
            });
            JPanel buttonpanel1 = new JPanel();
            buttonpanel1.add(button1);
            JPanel buttonpanel = new JPanel();
            buttonpanel.add(button);
            add(buttonpanel, BorderLayout.SOUTH);
            add(buttonpanel1, BorderLayout.NORTH);
            pack();
            setLocationByPlatform(true);
            setVisible(true);
        }
            public void loadtable(){
            	dtm = new DefaultTableModel(data, columnNames) {
                    @Override
                    public Class getColumnClass(int col) {
                        return getValueAt(0, col).getClass();
                    }
                    @Override
                    public boolean isCellEditable(int rowIndex, int colIndex) {
                        return (colIndex == CHECK_COL);
                    }
                };
                table = new JTable(dtm);
                JScrollPane scrollpane = new JScrollPane(table);
                add(scrollpane, BorderLayout.CENTER);
                pack();
                setVisible(true);
            }
        
    
        public static void main(String args[]) {
            EventQueue.invokeLater(new Runnable() {
    
                @Override
                public void run() {
                    new MainWindow();
                }
            });
        }
        }
    help needed. pls.
    Last edited by JosAH; 02-07-2012 at 09:11 AM. Reason: added [code] ... [/code] tags

  2. #2
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,000
    Blog Entries
    7
    Rep Power
    20

    Default Re: jtable refresh

    After the content of you table has changed (behind its back!) send a tableChanged( ... ) to your JTable.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,189
    Rep Power
    19

    Default Re: jtable refresh

    Moving to AWT/Swing

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

Similar Threads

  1. Refresh JTable data
    By pink123 in forum AWT / Swing
    Replies: 1
    Last Post: 03-31-2011, 06:18 PM
  2. Could not able to refresh JTable.
    By pratim in forum AWT / Swing
    Replies: 3
    Last Post: 03-28-2011, 01:52 PM
  3. JTable refresh button.doClick()
    By javaevgny in forum AWT / Swing
    Replies: 6
    Last Post: 01-14-2011, 04:28 PM
  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

Tags for this Thread

Posting Permissions

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