NetBeans Forums

 FAQFAQ   SearchSearch   MemberlistMemberlist   RegisterRegister   ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 
  

JTable Problem

 
Post new topic   Reply to topic    NetBeans Forums -> NetBeans Platform Users
View previous topic :: View next topic  
Author Message
zelencho



Joined: 31 Mar 2010
Posts: 3

PostPosted: Wed Mar 31, 2010 4:33 pm    Post subject: JTable Problem Reply with 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
Back to top
sushant kumar



Joined: 27 Oct 2008
Posts: 16
Location: India

PostPosted: Wed Mar 31, 2010 7:49 pm    Post subject: [platform-dev] Re: JTable Problem Reply with quote

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

PostPosted: Thu Apr 01, 2010 2:03 am    Post subject: Reply with quote

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 Smile 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



JTable - error.png
 Description:
Here's a screan print of my JTable
 Filesize:  3.7 KB
 Viewed:  2498 Time(s)

JTable - error.png


Back to top
zelencho



Joined: 31 Mar 2010
Posts: 3

PostPosted: Thu Apr 01, 2010 2:25 am    Post subject: JTable working Reply with 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.

Thanks
Back to top
Tim Boudreau
Posted via mailing list.





PostPosted: Thu Apr 01, 2010 7:49 am    Post subject: [platform-dev] Re: JTable Problem Reply with quote

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
Display posts from previous:   
Post new topic   Reply to topic    NetBeans Forums -> NetBeans Platform Users All times are GMT
Page 1 of 1

 
Jump to:  
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


Powered by phpBB
By use of this website, you agree to the NetBeans Policies and Terms of Use. © 2012, Oracle Corporation and/or its affiliates. Sponsored by Oracle logo