Results 1 to 7 of 7
- 04-24-2012, 01:46 AM #1
Member
- Join Date
- Feb 2012
- Posts
- 66
- Rep Power
- 0
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(""); } }
-
Re: Conditionally retrieve rowdata into 2d array
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.
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.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...
- 04-24-2012, 05:51 PM #3
Member
- Join Date
- Feb 2012
- Posts
- 66
- Rep Power
- 0
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(""); } }
-
Re: Conditionally retrieve rowdata into 2d array
Thanks for getting back to us about your problem and its solution!
- 04-24-2012, 06:05 PM #5
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.
dbWhy do they call it rush hour when nothing moves? - Robin Williams
- 04-24-2012, 06:37 PM #6
Member
- Join Date
- Feb 2012
- Posts
- 66
- Rep Power
- 0
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" ; )
- 04-24-2012, 07:19 PM #7
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.
dbWhy do they call it rush hour when nothing moves? - Robin Williams
Similar Threads
-
gathering jtable rowdata from checkbox selection
By Redefine12 in forum AWT / SwingReplies: 2Last Post: 04-21-2012, 01:59 AM -
Retrieve value from JTable
By mcdhappy80 in forum AWT / SwingReplies: 5Last Post: 12-01-2010, 04:35 AM -
Retrieve array inside a list as a Map value
By Ms.Ranjan in forum Advanced JavaReplies: 2Last Post: 08-04-2010, 12:30 PM -
How do I retrieve an array from a different class?
By Psyclone in forum AWT / SwingReplies: 11Last Post: 02-08-2010, 09:52 AM -
Retrieve Multidimensional Array ??
By oneofthelions in forum New To JavaReplies: 3Last Post: 12-12-2009, 07:24 AM


2Likes
LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks