• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

reading text file into GUI

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 4632
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 4632
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Fantastic Michael,
works like a charm...
thanks again for your time and help.
Randy
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
@Randy Adams Can not download the disctest.txt file. Can you please upload somewhere.
 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch

I think the answer is no, since RA has not been active for nearly 8 years.
 
Once upon a time there were three bears. And they were visted by a golden haired tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic