Results 1 to 7 of 7
Like Tree2Likes
  • 1 Post By Redefine12
  • 1 Post By DarrylBurke

Thread: Conditionally retrieve rowdata into 2d array

  1. #1
    Redefine12 is offline Member
    Join Date
    Feb 2012
    Posts
    66
    Rep Power
    0

    Default Conditionally retrieve rowdata into 2d array

    The JTable I'm working with implements Boolean.FALSE in a column to create a checkbox. I've created the necessary TableModelListener to update the table model when a box is checked. I need to iterate through the model and retreive row data for the rows with the box checed. I've currently got a method that is attempting to store the data as a 2d array. When I print the array, all values are null. I've stepped through the code and can't find where I've gone wrong. Here's my SSCCE

    Java Code:
    	 public void  returnReportData(){
    		 
    		 TableModel tm2 = table.getModel();
    //Get count of rows with Print Box checked (counter = z)	 
    		 int z = 0; 
    		 
    		 for(int x = 0; x < tm2.getRowCount(); x++){
    			 if(((Boolean)tm2.getValueAt(x, 0)) == true){
    				  z++;				  				  
    			 }
    		 }
    		 //Test print counter of checked items
    		 //System.out.println(z);
    		 		 			 
    		 int nRow = tm2.getRowCount(), nCol = tm2.getColumnCount();
    		 Object[][] reportData = new Object[z][54];  
    		 int tRow = reportData.length, tCol = reportData[0].length;
    		 System.out.println(reportData.length);
    		 System.out.println(reportData[0].length);
    		 //System.out.println(nCol);	 
    		 
    		 for (int i = 0 ; i < nRow ; i++){  
    	         if(((Boolean)tm2.getValueAt(i, 0)) == true){  
    	           for (int j = 0 ; j < nCol ; j++){     
    	                 for (int A = 0 ; A < reportData.length ; A++){  
    	                      for (int B = 0 ; B < reportData[0].length ; B++){                  
    	               reportData[A][B] = tm2.getValueAt(i,j);
    	                     }  
    	                 }  
    	             }  
    	         }  
    		 }
    		 
    		//Print array in rectangular form
    		 int ROWS = reportData.length;
    		 int COLS = reportData[0].length;
    
    		 for (int L = 0; L < ROWS; L++) {
    		 for (int M = 0; M < COLS; M++) {
    
    		 System.out.print(" " + reportData[L][M]);
    		 }
    		 System.out.println("");
    		 }
    		 
    		 
    		 
    	}

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

    Default Re: Conditionally retrieve rowdata into 2d array

    Quote Originally Posted by Redefine12 View Post
    The JTable I'm working with implements Boolean.FALSE in a column to create a checkbox. I've created the necessary TableModelListener to update the table model when a box is checked.
    That seems backwards to me, as I've always thought that a TableModelListener was for listening to changes in the table model's data, not for making changes to the table model's data.

    I need to iterate through the model and retreive row data for the rows with the box checed. I've currently got a method that is attempting to store the data as a 2d array. When I print the array, all values are null. I've stepped through the code and can't find where I've gone wrong. Here's my SSCCE...
    That's not a valid sscce. That's a code snippet that we cannot compile nor run. I believe that you'll have a better chance of getting help if you create and post an sscce-compliant example.

  3. #3
    Redefine12 is offline Member
    Join Date
    Feb 2012
    Posts
    66
    Rep Power
    0

    Default Re: Conditionally retrieve rowdata into 2d array

    Forgive me, I've since read up on the actual meaning of SSCCE. Beginner mistake to post a method vice compilable code. I've resolved the loop issue with a little help. Regarding the TableModelListener, you are correct, and I was using as you indicated, I just worded my question poorly. Another beginner mistake. As far as the loop goes, to reiterate, the objective was to pick up row data from a jtable only where the checkbox at column 0 is checked. In essence, a print selection box. While not an SSCCE, below snippet encapsulates the logic I was going for as a discrete method. I hope this may be of help to someone else.:

    Java Code:
    	 public void  returnReportData(){
    		 
    		 TableModel tm2 = table.getModel();
    //Get count of rows with Print Box checked (counter = z) to initialize the reportData Object[][] 
    		 int z = 0; 		 
    		 for(int x = 0; x < tm2.getRowCount(); x++){
    			 if(((Boolean)tm2.getValueAt(x, 0)) == true){
    				  z++;				  				  
    			 }
    		 }
    		 		 			 
    		 int nRow = tm2.getRowCount(), nCol = tm2.getColumnCount();
    		 Object[][] reportData = new Object[z][54];  
    		 int tRow = reportData.length, tCol = reportData[0].length;
    				 
    		 int index = 0;
    		 for (int i = 0 ; i < nRow ; i++){
    		     if(((Boolean)tm2.getValueAt(i, 0)) == true){
    		         for (int j = 1 ; j < nCol ; j++){   
    		              reportData[index][j] = tm2.getValueAt(i,j);  
    		         }
    		         ++index;
    		     }
    		 }
    		 
    		//Print array in rectangular form
    		 int ROWS = reportData.length;
    		 int COLS = reportData[0].length;
    
    		 for (int L = 0; L < ROWS; L++) {
    			 for (int M = 0; M < COLS; M++) {
    				System.out.print(" " + reportData[L][M]);
    		 }
    		 System.out.println("");
    		 }
    		 	 
    	}
    Fubarable likes this.

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

    Default Re: Conditionally retrieve rowdata into 2d array

    Thanks for getting back to us about your problem and its solution!

  5. #5
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,202
    Rep Power
    19

    Default Re: Conditionally retrieve rowdata into 2d array

    It does rather look like you're reinventing a wheel there. Specifically, RowFilter used in conjunction with TableRowSorter.

    db
    Fubarable likes this.
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  6. #6
    Redefine12 is offline Member
    Join Date
    Feb 2012
    Posts
    66
    Rep Power
    0

    Default Re: Conditionally retrieve rowdata into 2d array

    Fair enough, DB. I'm only a few months into Java and am fully aware that my code isn't tip-top by any means. I've hacked away to get what I've got and am striving to help my struggling enterprise get a project completed, trying to pick up knowledge along the way. I'm willing to work hard and pay my dues to the Java gods and am truly appreciative of the help I'm getting here. "I yam what I yam" ; )

  7. #7
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,202
    Rep Power
    19

    Default Re: Conditionally retrieve rowdata into 2d array

    I wasn't trying to be critical of your efforts. I was at least a couple of years into Java (as a hobby) before I discovered that TableRowSorter also managed row filtering.

    Using the JDK classes may considerably simplify your own code, making it more maintainable.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

Similar Threads

  1. gathering jtable rowdata from checkbox selection
    By Redefine12 in forum AWT / Swing
    Replies: 2
    Last Post: 04-21-2012, 01:59 AM
  2. Retrieve value from JTable
    By mcdhappy80 in forum AWT / Swing
    Replies: 5
    Last Post: 12-01-2010, 04:35 AM
  3. Retrieve array inside a list as a Map value
    By Ms.Ranjan in forum Advanced Java
    Replies: 2
    Last Post: 08-04-2010, 12:30 PM
  4. How do I retrieve an array from a different class?
    By Psyclone in forum AWT / Swing
    Replies: 11
    Last Post: 02-08-2010, 09:52 AM
  5. Retrieve Multidimensional Array ??
    By oneofthelions in forum New To Java
    Replies: 3
    Last Post: 12-12-2009, 07:24 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
  •