NetBeans Forums
| View previous topic :: View next topic |
| Author |
Message |
zelencho
Joined: 31 Mar 2010 Posts: 3
|
Posted: Wed Mar 31, 2010 4:33 pm Post subject: JTable Problem |
|
|
Hi,
I hope someone can help me.
I'm writing a program in Java and need to use JTable to display results of a search. I know how to do it if the data was hard coded but cannot get it to work with variables. As far as my understanding goes (I'm quite new to programming) it should not make a difference.
Here's my code:
| Code: | public void populateTable(Object fName, Object fSize, Object fLocation, boolean bool){
String columnNames[] ={"File Name", "File Size", "File Location", "Select File"};//declare table headers
Object data[][] = {{fName,fSize,fLocation, new Boolean(false)}}; // declare the data that is to be writen to the table
model = new DefaultTableModel(data, columnNames);
tableDispDup.setModel(model);
} |
Then I have a loop in a different method that calls this method every time a search criteria is met. Every time the method is called the new data should be written to the JTable. At the moment is displaying only the last result, which makes me think that it overides the same row.
The second problem I have, is when I try to set the Column Select File as Boolean. I need check boxes there, which will allow the user to select individual rows.
Using NetBeans to do that works and the first three columns are set to Objects and the last one is Boolean, however my code seems to overide those settings.
I have been playing with this for a few days, unsuccessfully, so any help will be welcome.
Thanks in advance
|
|
| Back to top |
|
 |
sushant kumar
Joined: 27 Oct 2008 Posts: 16 Location: India
|
Posted: Wed Mar 31, 2010 7:49 pm Post subject: [platform-dev] Re: JTable Problem |
|
|
Hi.
JTable is very simple to use as i think.
here is a simple approach i think it might help
Note* : First model ur table using netbeans GUI editor. set its types eta all.
next, declare a variable DefaultTableModel model = (DefaultTableModel) jtable1.getModel();
now in populate method, do this
public void populate()
{
for(i=0;i<x;i++)
{
model.setvalueAt(i,0,"abc");
model.setvalueAt(i,1,"pqr");
model.setvalueAt(i,2,true);
}
}
It wont override ur code anywhere .
On Wed, Mar 31, 2010 at 10:04 PM, zelencho <address-removed ([email]address-removed[/email])> wrote:
| Quote: | Hi,
I hope someone can help me.
I'm writing a program in Java and need to use JTable to display results of a search. I know how to do it if the data was hard coded but cannot get it to work with variables. As far as my understanding goes (I'm quite new to programming) it should not make a difference.
Here's my code:
Code:
public void populateTable(Object fName, Object fSize, Object fLocation, boolean bool){
String columnNames[] ={"File Name", "File Size", "File Location", "Select File"};//declare table headers
Object data[][] = {{fName,fSize,fLocation, new Boolean(false)}}; // declare the data that is to be writen to the table
model = new DefaultTableModel(data, columnNames);
tableDispDup.setModel(model);
}
Then I have a loop in a different method that calls this method every time a search criteria is met. Every time the method is called the new data should be written to the JTable. At the moment is displaying only the last result, which makes me think that it overides the same row.
The second problem I have, is when I try to set the Column Select File as Boolean. I need check boxes there, which will allow the user to select individual rows.
Using NetBeans to do that works and the first three columns are set to Objects and the last one is Boolean, however my code seems to overide those settings.
I have been playing with this for a few days, unsuccessfully, so any help will be welcome.
Thanks in advance
|
--
Sun Campus Ambassador
SMVD University
(www.smvdu.ac.in)
** The Best way to Stop Thinking about a Problem is to Solve It **
|
|
| Back to top |
|
 |
zelencho
Joined: 31 Mar 2010 Posts: 3
|
Posted: Thu Apr 01, 2010 2:03 am Post subject: |
|
|
Hi,
thanks for your reply. I did try to implement what you suggested within my code, but it did not seem to work or I did not do it properly.
I managed to get input into my table with the correct number of rows being displyed and have check boxes too, however the input is an error.
[[Ljava.lang.Object;@12d96f2
I'm not to sure what I'm doing wrong. I tried to cast my variables from Objects to Strings but this did not solve the problem with the error.
This is my lates code:
| Code: | public void populateTable(String fName, Long fSize, String fLocation, boolean bool){
String columnNames[] ={"File Name", "File Size", "File Location", "Select File"};//declare table headers
Object data[][] = {{fName,fSize,fLocation, new Boolean(false).booleanValue()}}; // declare the data that is to be writen to the table
Vector v = new Vector();
v.add(data);
//model = new DefaultTableModel(data, columnNames);
DefaultTableModel model = (DefaultTableModel) tableDispDup.getModel();
((DefaultTableModel) tableDispDup.getModel()).addRow(v);
} |
You are right, JTables should not be complicated, but when you are stuck on it, it can get really frustrating.
Hope there is an 'easy' solution...
Thanks
| Description: |
| Here's a screan print of my JTable |
|
| Filesize: |
3.7 KB |
| Viewed: |
2498 Time(s) |

|
|
|
| Back to top |
|
 |
zelencho
Joined: 31 Mar 2010 Posts: 3
|
Posted: Thu Apr 01, 2010 2:25 am Post subject: JTable working |
|
|
I got it working...
I cannot believe how much time I spent on this.
All I had to do is to add this line:
| Code: | | ((DefaultTableModel) tableDispDup.getModel()).addRow(new Object[]{fName,fSize,fLocation,false}); |
Hope someone will find it useful.
Thanks
|
|
| Back to top |
|
 |
Tim Boudreau Posted via mailing list.
|
Posted: Thu Apr 01, 2010 7:49 am Post subject: [platform-dev] Re: JTable Problem |
|
|
On Mar 31, 2010, at 10:25 PM, zelencho wrote:
| Quote: | I got it working...
I cannot believe how much time I spent on this.
All I had to do is to add this line:
Code:
((DefaultTableModel) tableDispDup.getModel()).addRow(new Object[]{fName,fSize,fLocation,false});
Hope someone will find it useful.
|
I'm glad you solved your problem, but this is *really* not a list for general Swing/Java programming questions.
Such questions and answers will be useful to a lot more people (and probably get answered more quickly) in an appropriate forum. http://java.sun.com has quite a few:
http://forums.sun.com/index.jspa
-Tim
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum You can attach files in this forum You can download files in this forum
|
|