• 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

Help with displaying data in JTextfield

 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello, I dont know if what im asking for is possible or not, however here is my question:
I have 4 JTextfields in a row looking like cells, first one is editable however the rest are not, what i want to do is, to 'listen' for user input where when the user inputs for example a student name, my program will search for the name in an array and output the rest of his information in the 3 remaining textfields that arent editable.
i'd also like to know what is the best data type to use, an array or 2d array or any other possible data types that i should know about.
Thanks in advance
 
Ranch Hand
Posts: 479
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well, a JTextField is normally editable; you can disable a JTextField, but you might consider making the "non-editable JTextFields" into JLabels instead. Unless you meant you're going to fill in values and let the user edit them later.

Certainly you can do a search on the value in one text field and fill in other components: the first thing that comes to mind is adding a listener for a lost focus event on the name field; when the user moves from that field to the next one, you can have a listener routine do the search and fill in the values. You could also listen for keypresses or keystrokes (two different things), though I recommend the first option unless you want to search every time the user presses a key.

If you don't know what an ActionListener is, you will need to study Swing and event-driven programming in general. This sounds like a reasonable early exercise in Swing, perhaps that was your intent.

You haven't given enough information to advise on "data structures"; which one(s) to use is going to depend on more than a collection of related text fields.
 
Ranch Hand
Posts: 4716
9
Scala Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
in a way it reminds me of a program i wrote where i created my own data structure that had 4 private Strings, a constructor that required 4 Strings and assigned them to the member variables, and 4 get methods to return the Strings.
 
Talal Mohammed
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you for your response, you solved my first problem, however in terms of data structures I mean, what is the best data structure that is suitable for this type of data, the JTextFields are arranged in cells, and each column has its own loop that arranges these cells, some of them are for strings, others for ints/doubles, all i want to do is to save the data in a structure such as an array (maybe each column having its own array or an array of objects?) but I don't know how to achieve that, I believe I'm supposed to try and create set and get methods but I don't understand how to create them, if you could show me an example or refer me to some guide I'd be very grateful

code for textfields:

for(int i = 0; i < studentName.length; i++) {
studentName[i] = new JTextField();
studentName[i].setBounds(28, 33 + (i *21), 254, 22);
add.. }
 
Randall Twede
Ranch Hand
Posts: 4716
9
Scala Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
ok i show you my example i hope it helps
 
Rancher
Posts: 3324
32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
See: How to Write a Document Listener. Whenever the listener fires you
get the text and do a search. If you find a unique entry you can display the results in the other text fields.

@Randall, your posted code is old AWT code. Swing concepts are different and should be promoted.
 
Marshal
Posts: 79179
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sound like something which would fit better on our GUIs forum.
Moving.
 
Randall Twede
Ranch Hand
Posts: 4716
9
Scala Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
it may be java1 but it still works. anyway i posted the wrong class file. i meant to post the Animal class
 
Talal Mohammed
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you all, I'll look into implementing a document listener.
<3
 
reply
    Bookmark Topic Watch Topic
  • New Topic