wood burning stoves 2.0*
The moose likes Swing / AWT / SWT and the fly likes trouble with my table Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "trouble with my table" Watch "trouble with my table" New topic
Author

trouble with my table

Jason McKinley
Greenhorn

Joined: Jun 27, 2013
Posts: 18
Hello, I am trying to get a table to pull data from a file and place it in the table. My trouble is that I am getting en error. I will put in the section of code I am having the error then the whole code.

Section with error:



And here is the whole code:


I have been trying to find out what is wrong but I'm clueless. Anyway thanks in advice for the help.
Ranganathan Kaliyur Mannar
Bartender

Joined: Oct 16, 2003
Posts: 1085
    
  10

In the code section with error, you have a closing brace in line no.9 which should be in line no.13.
Your call to InsertData() should be within another method body. You are doing it outside the method body.


Ranga.
SCJP 1.4, OCMJEA/SCEA 5.0.
hima reddy
Greenhorn

Joined: Jul 11, 2013
Posts: 9
    
    1
Try below

FileInputStream is;
File f = new File("C:/MyAddressBook.txt");
is = new FileInputStream(f);
InsertData(is);
}


static void InsertData(FileInputStream is) {
Jason McKinley
Greenhorn

Joined: Jun 27, 2013
Posts: 18
Sorry it took me a bit to respond back but that did the trick. Thanks for the help, I have one more problem I would like help with. I was trying to get the table to show up on the gui like this:



but when I do that it only gives me a small box where the table should show up:



I have looked at the code and read up on what I could be doing wrong but I'm at a lost.
Ranganathan Kaliyur Mannar
Bartender

Joined: Oct 16, 2003
Posts: 1085
    
  10

You have only shown part of the code. Why does gridy have a value of 8? Also, I have no idea if you added rows to the table.

I would suggest you to write a small runnable program that replicates the issue and post that. That is called SSCCE.
Jason McKinley
Greenhorn

Joined: Jun 27, 2013
Posts: 18
Ranganathan Kaliyur Mannar wrote:You have only shown part of the code. Why does gridy have a value of 8? Also, I have no idea if you added rows to the table.

I would suggest you to write a small runnable program that replicates the issue and post that. That is called SSCCE.


Sorry it took so long to write back, had a busy week. Anyway here is the rest of the code:




And the part I'm having trouble with again:

Ranganathan Kaliyur Mannar
Bartender

Joined: Oct 16, 2003
Posts: 1085
    
  10

You are creating the model, but, you are not setting it to the table.
Jason McKinley
Greenhorn

Joined: Jun 27, 2013
Posts: 18
ok, I added and to:



but it's telling me

on both the model and table words in both the lines on code I added.

How would I go about linking them to the MyAddressBook section?
Ranganathan Kaliyur Mannar
Bartender

Joined: Oct 16, 2003
Posts: 1085
    
  10

This is a simple compilation error which can be resolved if you understand scoping of variables. I would suggest you to study that first with a simple program before attempting to build a GUI.
Jason McKinley
Greenhorn

Joined: Jun 27, 2013
Posts: 18
Ok, so I read up on scope of variables, found out my problem, and did a little of a rewrite on the whole thing. I got it to where it's not giving me any errors but not when I go to run the the program I get this:



Here is the newly written code:


What did I do wrong to it and how do I get it fixed?
btw, thanks for your last post in how you worded it and not just giving me the answe. I was able to really learn instead of just copying what you put down.
Ranganathan Kaliyur Mannar
Bartender

Joined: Oct 16, 2003
Posts: 1085
    
  10

Ok good. Now, you are getting a runtime error and the exception stack trace neatly points you out to the line number where the issue is: Testaddress.java:204

So, look at line number 204 in your program and find out the variables that you are using for method invocation. Have they been initialized? (I mean 'created' by calling new <someclass>?)

Also, while reading about scoping of variables, I think you might have also come across variable hiding - which happens when you use the same name for variables of different scope. Find out if you are doing something like that...
Jason McKinley
Greenhorn

Joined: Jun 27, 2013
Posts: 18
Sorry it took so long to write back, it's been a busy week for me. I have done some reading on the subject but I am missing what I am suppose to do. Is there anyway you can explain it to me a little more? Also on the part about variable hiding I changed:



to



to fix the variable hiding problem.

Now my code looks like this:
Ranganathan Kaliyur Mannar
Bartender

Joined: Oct 16, 2003
Posts: 1085
    
  10

After the changes, did you run the program? What issue are you getting? You should be getting a NullPointerExcetion.

This is because:
You declared an instance variable named model and then at line no.176, you declared a local variable with the same name. So, whatever initialization that you did was for the local variable and not for the class variable. Your local variable now 'hides' the class variable. I think that was not your intention.
Jason McKinley
Greenhorn

Joined: Jun 27, 2013
Posts: 18
Ok, I got the hiding fixed by taking out JTable and DefaultTableModel in lines 172 and 176. I'm not having any problems with that now. I'm still bonked on:



however, I just don't know what to replace data with to make the table work right. I thought maybe putting is there since it has to do with bring in the information to put in the rows but I get a "no suitable method found for addRow(FileInputStream)" error and I just don't think that is the right thing to put in there. Anymore advice you can give me on fixing this problem?

Here's the new code:
Ranganathan Kaliyur Mannar
Bartender

Joined: Oct 16, 2003
Posts: 1085
    
  10

Are you getting a compilation error now? on which line? Because I only see a addRow() call with an Object[] being passed.
Jason McKinley
Greenhorn

Joined: Jun 27, 2013
Posts: 18
No, the program runs fine but it's not reading anything from my csv file and putting it in the table. all I get is this:

Darryl Burke
Bartender

Joined: May 03, 2008
Posts: 4642
    
    5

Not the subject of your problem, but your code could be a lot shorter if you didn't have so many lines setting fields of the GridBagLayout to the value they already are.


luck, db
There are no new questions, but there may be new answers.
Ranganathan Kaliyur Mannar
Bartender

Joined: Oct 16, 2003
Posts: 1085
    
  10

I think the table is getting populated, but you are not able to view it. Easier way to find this out would be remove all the other components (by commenting) and having only the table - you would know for sure if the data is loaded.

As for the layout, I think you should use the 'fill' constraint - I see that you have set the weighty, you should also set the fill to VERTICAL for table.
Jason McKinley
Greenhorn

Joined: Jun 27, 2013
Posts: 18
Ranganathan Kaliyur Mannar wrote:I think the table is getting populated, but you are not able to view it. Easier way to find this out would be remove all the other components (by commenting) and having only the table - you would know for sure if the data is loaded.

As for the layout, I think you should use the 'fill' constraint - I see that you have set the weighty, you should also set the fill to VERTICAL for table.


I did as you said and commented out everything but the table and when I did that the data started showing. So I decided to see if the programs height was high, it turns out that was the problem. I extended the total height of the program and everything is working perfectly. Thanks so much for all the help and having the patients to working with me on it.
Ranganathan Kaliyur Mannar
Bartender

Joined: Oct 16, 2003
Posts: 1085
    
  10

Great, you're welcome.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: trouble with my table