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 3 of 3

Thread: JTable setValueAt function got the whole row in same value

  1. #1
    Junior Member
    Join Date
    Jul 2014
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question JTable setValueAt function got the whole row in same value

    Hi, people, I used the following code to create my JTable object:
    DefaultTableModel tableModel = new DefaultTableModel();
    String columnNames[] = {"A", "B", "C", "D", "E"};
    DefaultTableColumnModel columnModel = new DefaultTableColumnModel(); 
    for (int i = 0; i < 5; i++) { 
    	TableColumn column = new TableColumn(); 
    	column.setHeaderValue(columnNames[i]); 
            columnModel.addColumn(column); 
    	tableModel.addColumn(column);
    } 
    tableModel.addRow(new Object[5]);//for the first blank row
    MyTable table = new MyTable(tableModel, columnModel);
    MyTable class was defined below:
    public class MyTable extends JTable {		
    	public MyTable(TableModel dm, TableColumnModel cm) {
    		super(dm, cm);
    	}
     
    	@Override
    	public void changeSelection(int row, int column, boolean toggle, boolean extend) {
    		super.changeSelection(row, column, toggle, extend);
                    if (editCellAt(row, column))
                        getEditorComponent().requestFocusInWindow();
    	}
     
    	@Override
            /*only the first column is editable, the other columns' values are determined 
               by the corresponding first column using a query from dababase
            */
    	public boolean isCellEditable(int row, int column) {
    		if (column == 0) return true;
    		else return false;
    	}
    }
    then, I input "a1" to the cell(0, 0), did the query and got the resultset: a1, b1, c1, d1, e1(for example) and set the values to the table:
    table.setValueAt(b1, 0, 1);
    table.setValueAt(c1, 0, 2);
    table.setValueAt(d1, 0, 3);
    table.setValueAt(e1, 0, 4);
    finally, I got a table like this:
    A B C D E
    e1 e1 e1 e1 e1
    all the cells of this row have the same value e1, why comes out such weird thing?
    Any ideas or suggestions are welcomed, thanks in advance!
    Last edited by zhouhang921; July 9th, 2014 at 09:22 AM.


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: JTable setValueAt function got the whole row in same value

    Experiment. Try running the program with only one of the setValueAt() statements (just comment the others out). Try each one in order, one at a time, and see what results you get after each run. Maybe that will give you some insight into what's going on. If this doesn't help, then put together a short, runnable example that demonstrates the problem.

  3. #3
    Junior Member
    Join Date
    Jul 2014
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: JTable setValueAt function got the whole row in same value

    Quote Originally Posted by GregBrannon View Post
    Experiment. Try running the program with only one of the setValueAt() statements (just comment the others out). Try each one in order, one at a time, and see what results you get after each run. Maybe that will give you some insight into what's going on. If this doesn't help, then put together a short, runnable example that demonstrates the problem.
    I have tried your method, but everytime, only the last setValueAt sentence works, and the whole row always turns out the same value.
    I changed the initialization of the Table to: MyTable table = new Mytable(new Object[5][5], columnNames); then everything works fine.
    Thank you! GregBrannon

Similar Threads

  1. Replies: 1
    Last Post: February 5th, 2014, 09:22 AM
  2. Replies: 2
    Last Post: December 13th, 2013, 12:01 AM
  3. Replies: 21
    Last Post: November 27th, 2012, 10:58 PM