Results 1 to 3 of 3
  1. #1
    kwaspl is offline Member
    Join Date
    Dec 2009
    Posts
    4
    Rep Power
    0

    Default 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);
            }
        
    }

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    This here:
    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);
    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.

  3. #3
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

    Default

    ... too slow ;(
    Last edited by pbrockway2; 12-20-2009 at 11:43 PM.

Similar Threads

  1. Problem With AI and JTable
    By elad_bj in forum New To Java
    Replies: 8
    Last Post: 04-20-2009, 03:24 PM
  2. Replies: 0
    Last Post: 08-29-2008, 01:49 PM
  3. Replies: 0
    Last Post: 08-29-2008, 01:16 PM
  4. Updating database table from JTable
    By yesjava in forum New To Java
    Replies: 1
    Last Post: 08-16-2008, 11:16 PM
  5. Replies: 2
    Last Post: 05-15-2008, 11:15 AM

Posting Permissions

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