Results 1 to 3 of 3
Thread: jtable refresh
- 02-07-2012, 08:36 AM #1
Member
- Join Date
- Sep 2011
- Posts
- 14
- Rep Power
- 0
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.
help needed. pls.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(); } }); } }Last edited by JosAH; 02-07-2012 at 09:11 AM. Reason: added [code] ... [/code] tags
- 02-07-2012, 09:13 AM #2
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,405
- Blog Entries
- 7
- Rep Power
- 17
Re: jtable refresh
After the content of you table has changed (behind its back!) send a tableChanged( ... ) to your JTable.
kind regards,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 02-07-2012, 09:36 AM #3
Similar Threads
-
Refresh JTable data
By pink123 in forum AWT / SwingReplies: 1Last Post: 03-31-2011, 06:18 PM -
Could not able to refresh JTable.
By pratim in forum AWT / SwingReplies: 3Last Post: 03-28-2011, 01:52 PM -
JTable refresh button.doClick()
By javaevgny in forum AWT / SwingReplies: 6Last Post: 01-14-2011, 04:28 PM -
Dynamic Refresh in JTable every 5 Minutes
By britto_bicsjohn in forum AWT / SwingReplies: 3Last Post: 08-07-2009, 05:16 AM -
how to refresh data of the JTable
By paty in forum JDBCReplies: 3Last Post: 08-17-2008, 12:01 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks