aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes reading text file into GUI Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "reading text file into GUI" Watch "reading text file into GUI" New topic
Author

reading text file into GUI

Randy Adams
Greenhorn

Joined: Jul 01, 2006
Posts: 4
Hi all,
Newbie here...trying to learn it by using it.. My first post here at Java Ranch...looks like a great place!

I am using a JFrame with JPanel (with radiobuttons and jtextfields) and am wanting to get data from a text file (around 100 words) and input 4 words at a time into my gui.

I have been trying to do this by filling an string array with the data, (no problems here) then using jTextField to read the array. (having a few problems in this area though)

Am about 80% there...but just wondering is there a better way to approach this problem, that is, inputing words into gui, refreshing, input 4 new words, refreshing, etc.
Hope this is clear.
I'm not so familiar with jtext array, so dont really know if that is a possibility or if there are other better solutions.
Thanks much in advance for any help,
Randy
Taiwan
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
if it is not a requirement to read in the data in short lots,
a JTextArea, and it's read() method, might be a better way.

Randy Adams
Greenhorn

Joined: Jul 01, 2006
Posts: 4
thanks Michael for your quick response...
I should have explained more clearly...
The program is a test, so it should only show 4 words/phrases at a time.
Multiple choice, so each word has has a radio button beside it and bottons grouped so that can only choose one.
I have been trying to use jTextField x4, with 4 radiobuttons alongside them.
Randy
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
if each line's data is something like this
question,choice1,choice2,choice3,choice4,correct answer

and you know how many lines there are, an array of Question Objects would
do the job, otherwise an ArrayList().

Read the text file into the array in one go, roughly something like this



now you have your data in Question Objects, you can display it as
a JLabel for the question
a JRadioButton[] with the options/choices as the radioButton's text
a JButton for 'next' - this checks the index of the selected radioButton to
the 'answer' field of the Question, and if correct, correctAnswers++, before
displaying the next question/choices etc
Randy Adams
Greenhorn

Joined: Jul 01, 2006
Posts: 4
Hi Michael,
thanks much for your help.
Sounds like your approach is similar to mine in using an array to fill the text box.
I can show you what I have so far.
picture of my gui is here
http://www.pbase.com/adamstw/image/62785317

my code is here
(its about 300 lines-mostly gui, so didnt want to insert it here)
I'm a newbie to JAVA, so still very inefficient...sorry.
http://h1.ripway.com/adamsr/janet1.java


and the text file is here
http://h1.ripway.com/adamsr/disctest

I actually have an 3D array [] [] [], since I am using some other coding with each word.
I can fill the array, but right now having problems getting text into my jText (or jLabel). I am using code
jTextField3.setText(answers [0] [0] [0]);
but coming up empty.
Been playing around with this for a while, and thinking maybe there is a better way..hence my question.

If you have time, I continue to appreciate your help.
Randy
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
This reads in from "disctest.txt", and displays it in groups of 4 (per the pic)
I've added previous/next buttons to display the previous/next group.

I've used a number of shortcuts, simply because I don't follow what the end
result should be e.g the 'descriptions', and what is to happen when 'Done' is clicked.

Anyway, there might be a couple of things in it to help you to get your program working

(I've called everthing Disc.... - from the filename disctest)
Randy Adams
Greenhorn

Joined: Jul 01, 2006
Posts: 4
Fantastic Michael,
works like a charm...
thanks again for your time and help.
Randy
Roy Foo
Greenhorn

Joined: May 07, 2014
Posts: 1
@Randy Adams Can not download the disctest.txt file. Can you please upload somewhere.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39430
    
  28
Welcome to the Ranch

I think the answer is no, since RA has not been active for nearly 8 years.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: reading text file into GUI