Results 1 to 3 of 3
Thread: Problem with updating JTable
- 12-20-2009, 10:07 PM #1
Member
- Join Date
- Dec 2009
- Posts
- 4
- Rep Power
- 0
Problem with updating JTable
I'm not sure why my JTable want update. A few people have similar problem but i still cant find solution.
PHP Code:import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.JScrollPane; import javax.swing.JTable; import javax.swing.table.AbstractTableModel; import java.awt.Dimension; import java.awt.GridLayout; public class TableDemo extends JPanel { public TableDemo() { super(new GridLayout(1,0)); JTable table = new JTable(new MyTableModel()); table.setPreferredScrollableViewportSize(new Dimension(500, 70)); table.setFillsViewportHeight(true); JScrollPane scrollPane = new JScrollPane(table); add(scrollPane); } class MyTableModel extends AbstractTableModel { private String[] columnNames = {"First Name", "Last Name", "Sport", "# of Years", "Vegetarian"}; private Object[][] data = { {"Mary", "Campione", "Snowboarding", new Integer(5), new Boolean(false)}, {"Alison", "Huml", "Rowing", new Integer(3), new Boolean(true)}, {"Kathy", "Walrath", "Knitting", new Integer(2), new Boolean(false)}, {"Sharon", "Zakhour", "Speed reading", new Integer(20), new Boolean(true)}, {"Philip", "Milne", "Pool", new Integer(10), new Boolean(false)}, }; public int getColumnCount() { return columnNames.length; } public int getRowCount() { return data.length; } public String getColumnName(int col) { return columnNames[col]; } public Object getValueAt(int row, int col) { return data[row][col]; } public Class getColumnClass(int c) { return getValueAt(0, c).getClass(); } public void setValueAt(Object value, int row, int col) { data[row][col] = value; fireTableCellUpdated(row, col); } } private static void createAndShowGUI() { JFrame frame = new JFrame("TableDemo"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); TableDemo newContentPane = new TableDemo(); newContentPane.setOpaque(true); //content panes must be opaque frame.setContentPane(newContentPane); frame.pack(); frame.setVisible(true); } public static void main(String[] args) { TableDemo xxx = new TableDemo(); xxx.createAndShowGUI(); TableDemo.MyTableModel xxx2 = xxx.new MyTableModel(); xxx2.setValueAt(new Integer(0), 3, 3); Object nn = new Integer(21); nn = xxx2.getValueAt(2,2); System.out.println(nn); } }
-
This here:
creates a new MyTableModel object but has absolutely no effect on the TableModel object that is held by the displayed JTable, so you shouldn't be surprised that the displayed data doesn't change. I suggest that you use a table model that extends DefaultTableModel, not AbstractTableModel, and that you make updates to the table model object that is in fact held by the JTable.Java Code:TableDemo.MyTableModel xxx2 = xxx.new MyTableModel(); xxx2.setValueAt(new Integer(0), 3, 3); Object nn = new Integer(21); nn = xxx2.getValueAt(2,2); System.out.println(nn);
- 12-20-2009, 10:41 PM #3
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,547
- Rep Power
- 11
Similar Threads
-
Problem With AI and JTable
By elad_bj in forum New To JavaReplies: 8Last Post: 04-20-2009, 02:24 PM -
Is there a way for updating JTree and JTable with a same model?
By Melki in forum AWT / SwingReplies: 0Last Post: 08-29-2008, 12:49 PM -
Is there a way for updating JTree and JTable with a same model?
By Melki in forum AWT / SwingReplies: 0Last Post: 08-29-2008, 12:16 PM -
Updating database table from JTable
By yesjava in forum New To JavaReplies: 1Last Post: 08-16-2008, 10:16 PM -
Jtable duplicates through Hashtable (JTable condition problem) my assignment plz help
By salmanpirzada1 in forum Advanced JavaReplies: 2Last Post: 05-15-2008, 10:15 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks