• Post Reply Bookmark Topic Watch Topic
  • New Topic

Loading Label from text file (awt)  RSS feed

 
Lyn Arce
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all, this is my first post here

I´m having problems with loading a Label from a text file.
This is some test code and to begin with I wanted to visualize the text
(later on I have to make it pick a random title + replace characters with "-")
but it doesn´t load...

Lyn

 
Alex Hurtt
Ranch Hand
Posts: 98
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm going to take a stab and guess that you are getting the output "blaFile" right?
My guess is that the JVM is not looking for your text file in the directory that you think it is looking.
Try writing out a simple file instead of reading one in and then search the file system for the file you just wrote. Where is it? Is it where you expected it to be? Is it in the same place as the file you were originally trying to read in?
 
Rob Spoor
Sheriff
Posts: 21135
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch, Lyn!

I'm sorry to add this kind of complexity at this early stage of your program, but you should still read concurrency in Swing. Don't let the title fool you, it applies to AWT as well.
Currently, your " this.tl.setText(str);" call is a potential danger due to the threading issue mentioned in the article I linked to.
 
Lyn Arce
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@Alex
The text file is located in the same directory, and it´s not empty (I put just some word in it to test but it´s actually a part of a bigger input AdminMode)
It compiles and runs but the window is just empty

My concern was/is that I wasn´t calling the text file in the right way, really linking the Label and the text file.
That is, these parts:

***
tl = new Label(str);

***
while ((cadena = br.readLine()) != null) {
this.tl.setText(str);


@Rob
Thanks for the tip, I will look into it.
 
Paul Clapham
Sheriff
Posts: 22828
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Lyn Arce wrote:The text file is located in the same directory, and it´s not empty.


The same directory as what? Anyway that isn't an answer to the question, which is whether the text file is located in the current working directory. Perhaps it isn't, and an exception is thrown by the file-reading code.
 
Campbell Ritchie
Marshal
Posts: 56536
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why are you using AWT rather than Swing, which was introduced over ten years ago?
Why have you got str as a field? You aren't using it as a field, but only as a local variable in the loadData method.
Why are you using a null layout?
Why are you reading all the lines in your text file; do you really need the last line? Or do you want the first line? In which case simply read the first line, and have a default available in case it is null:That line 3 will give you an error stream message showing you the text. Watch carefully that you get such output. Use System.err instead of System.out for your Exception printouts.
 
Lyn Arce
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I finally realized the problem was related to the file; stored in the wrong directory...
Pretty dull in the end, but at least the problem revealed some other issues with the code.

@Campell
It´s a textbook excercise, AWT is required.
As for the "null layout" the textbook says when you use setBounds() for manual layout, setLayout() should be set to null??
As for reading the lines, the program is supposed to be picking a random line (a title) from the text file,
and then replace only the characters (not whitespace) with "-", this last one being a bit of a headache me, but that´s another story...

So I guess AWT is not really the way to go, or what? Is it only worth it as an introduction for Swing?


Anyway, thanks a bunch everyone for your swift and insightful feedbacks,
Lyn
 
Paul Clapham
Sheriff
Posts: 22828
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Lyn Arce wrote:It´s a textbook excercise, AWT is required.
As for the "null layout" the textbook says when you use setBounds() for manual layout, setLayout() should be set to null??


Ouch. If you're using a textbook which teaches you AWT then it's surely an outdated textbook. As Campbell said, AWT has been obsolete since the last century. And a textbook which advocates using the null layout is also a candidate for the shredder in my opinion.

But most likely you have no choice in that matter. Just be warned.
 
Campbell Ritchie
Marshal
Posts: 56536
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Agree with Paul about the book.

It is correct that you can only get setBounds to work on a null layout, but neither setBounds nor a null layout is at all good with child Components.

You are not choosing a random line from your file; you are choosing the last line. For a random line there are various approaches; maybe putting all the lines into a List<String> and using a method from the Collections class to randomise the List, then choosing the first element would give a good result.
As for replacing all the letters with a hyphen, that can easily be done with a method of the String class. Note you will need to pass a regular expression as an argument to that method.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!