Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 7 of 7

Thread: Table with CustomModel when clicked shows old entry

  1. #1
    Member
    Join Date
    Oct 2011
    Posts
    53
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Table with CustomModel when clicked shows old entry

    Hi,

    I have a problem with a Table using a CustomModel I have created.
    The first time everything works fine, but whenever I re-create the Table and the user clicks on it, the content of the table changes into the old ones.

    Here is a part of the code.
    I cannot provide a SSCE for the moment, but I can give an idea of the whole thing.

    private static void showResults() {
     
    		// Creating JTable
    		CustomTable Table = new CustomTable();
     
    	}
     
     
    	public static class CustomTable extends JPanel {
     
    		public CustomTable() {
     
     
    			CustomTableModel TableModel = new CustomTableModel();
     
     
    			JTable Table = new JTable(TableModel);
     
    			// Create the scroll pane and add the table to it.
    			JScrollPane scrollPane = new JScrollPane(Table);
     
    			Table.setFocusable(false);
     
    			// Add the scroll pane to this frame
    			Frame.add(scrollPane, BorderLayout.CENTER);
    			scrollPane.setBounds(100, 300, 600, 250);
     
    		}
     
    		class CustomTableModel extends AbstractTableModel {
     
     
     
    			private String rows[][] = Search();
     
    			private String headers[] = { "", "", "", "", ""};
     
    			public int getColumnCount() {
    				return headers.length;
    			}
     
    			public int getRowCount() {
    				return rows.length;
    			}
     
    			public String getColumnName(int col) {
    				return headers[col];
    			}
     
    			public Object getValueAt(int row, int col) {
    				return rows[row][col];
    			}
     
    			// Table not editable
    			public boolean isCellEditable(int row, int col) {
     
    				return false;
     
    			}
     
     
    		}


    Basically I recreate the Table every time the user clicks on a button for searching. The results are correct but whenever I click on the table, the cell/row I click on changes its values on the first one.

    I also tried to remove the possibility of selecting an item of the Table, but the problem still remains.


    I also tried to remove it from the Frame (using Frame.remove(Table) ) and revalidate it. Also repaint and so on. Also tried to remove all the content of the Table before recreating a new one, but nothing...
    Last edited by Nesh108; November 6th, 2011 at 09:45 AM.


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Table with CustomModel when clicked shows old entry

    the cell/row I click on changes its values on the first one
    Try overriding the setValueAt in AbstractTableModel to set the appropriate value

  3. #3
    Member
    Join Date
    Oct 2011
    Posts
    53
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: Table with CustomModel when clicked shows old entry

    Tried, but same problem:

     
    public void setValueAt(int row, int col)
    {
     
    super.setValueAt(Table, row, col);
    }

  4. #4
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Table with CustomModel when clicked shows old entry

    See the API for AbstractTableModel - that is not overriding the setValueAt method.

  5. #5
    Member
    Join Date
    Oct 2011
    Posts
    53
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: Table with CustomModel when clicked shows old entry

    I have googled for a while and tried some ways to override the setValueAt method, I tried with:

    			public void setValueAt(Object value, int row, int col) {
     
    				   rows[row][col] = value.toString();
    			       this.fireTableCellUpdated(row, col);
    			}

    But the result is still the same.
    I just wanted to give an update, I will try to search for more...

  6. #6
    Member
    Join Date
    Oct 2011
    Posts
    42
    Thanks
    0
    Thanked 3 Times in 3 Posts

    Default Re: Table with CustomModel when clicked shows old entry

    Quote Originally Posted by Nesh108 View Post
    Hi,

     
    		class CustomTableModel extends AbstractTableModel {
     
    			private String rows[][] = Search();
     
     
    		}
    The above section caused the problem. The rows object is initialized only once by the Search() function. You should re-design the CustomTableModel, for example, adding some methods to update the search result.

    java exception
    Last edited by hns1984; January 11th, 2012 at 06:58 PM.

  7. #7
    Member
    Join Date
    Oct 2011
    Posts
    53
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: Table with CustomModel when clicked shows old entry

    Thanks!!!!
    Oh finally!!!

    here how I fixed the problem:

     
    			//Update
    			public void updateResult(){
     
    				rows =Search();
     
    				this.fireTableDataChanged();
     
     
    			}

    In this way I do not re-create the table each time I search for new results.
    Last edited by Nesh108; November 9th, 2011 at 06:42 AM.

Similar Threads

  1. GUI: JButtons aren't visible until clicked
    By Staticity in forum What's Wrong With My Code?
    Replies: 5
    Last Post: August 7th, 2011, 02:49 PM
  2. button update when clicked
    By prettynew in forum AWT / Java Swing
    Replies: 4
    Last Post: March 13th, 2011, 04:47 PM
  3. Replies: 2
    Last Post: January 7th, 2011, 09:10 PM
  4. need help w/ entry level homework...
    By rbread80 in forum What's Wrong With My Code?
    Replies: 6
    Last Post: October 3rd, 2010, 10:24 PM
  5. [SOLVED] Executable .jar file isn’t launched after being double-clicked
    By voltaire in forum Java Theory & Questions
    Replies: 6
    Last Post: May 18th, 2010, 03:37 PM