• Post Reply Bookmark Topic Watch Topic
  • New Topic

Using Swing to display an array from text file issues

 
Jason Flaherty
Greenhorn
Posts: 15
Chrome MyEclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,

I am fresh into Java here. I am trying to pull data from a text file, 2000 words or so, and have them display one at a time flashing on the screen for a set time then the next word etc... until EOF.
I am getting these errors:


Below is my (and other online) code...



Currently I am trying to just put the array list out to the consol, then I want to use a timer to change the _textField everyone X seconds.

Thanks for any advice.

 
Unnar Björnsson
Ranch Hand
Posts: 164
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You have to instantiate the arraylist 'words'
 
Jason Flaherty
Greenhorn
Posts: 15
Chrome MyEclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks, got that done.



I am lost at how to get the _textField.setText(w.name); working. I am not doing something correct getting the timer running.

I can get it to use a timer and print out to the console no problem:



What am I missing updating the _textField?

Thanks a ton.
 
Darryl Burke
Bartender
Posts: 5155
11
Java Netbeans IDE Opera
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Moving to the GUIs forum, where we usually discuss such problems.
 
Darryl Burke
Bartender
Posts: 5155
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Each time through the Timer's action, you obtain a fresh Iterator. That isn't going to step through the List of Words, it's next() is just going to return the first Word each time.
 
Jason Flaherty
Greenhorn
Posts: 15
Chrome MyEclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Darryl Burke wrote:Each time through the Timer's action, you obtain a fresh Iterator. That isn't going to step through the List of Words, it's next() is just going to return the first Word each time.


Is that because is inside my ActionListener?
 
Rob Spoor
Sheriff
Posts: 20820
68
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It actually shouldn't. You create that Iterator once, when you create the ActionListener. The timer calls actionPerformed which does not create a new Iterator, it only calls the hasNext and next methods.

Could you change your code inside that if-block to this:
In other words, both set the text and print out the value. You need to store the result of w.next() because calling w.next() twice would mean you set the text of one word, then print another.
If you will now see the words being printed out but you don't see the text being set then there is a problem in setting the text. Other than a very short interval (half a second) I don't see what that could be yet.
 
Darryl Burke
Bartender
Posts: 5155
11
Java Netbeans IDE Opera
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rob Spoor wrote:It actually shouldn't. You create that Iterator once, when you create the ActionListener. The timer calls actionPerformed which does not create a new Iterator, it only calls the hasNext and next methods.


You're right, I misread the code. Not wanting to make excuses, but conventional code formatting with some vertical whitespace would have aided readability.


Sorry, Jason.

The problem appears to be in your main(...) method where you construct not one, but two instances of the class. One is made visible but the other is start()ed.

Also, all Swing code should be executed on the EDT. If you're not familiar with that, find the Oracle tutorial on Concurrency in Swing and go through it.

 
Jason Flaherty
Greenhorn
Posts: 15
Chrome MyEclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm sorry. I don't get where I have it starting 2x?

Thanks for any help.


NEVER MIND.... See next post.
 
Jason Flaherty
Greenhorn
Posts: 15
Chrome MyEclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Well I got it working. I am not sure how good this code is, but I put the read and print right in the constructor.

Now to make the words tween from tiny to big!
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!