• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Help with displaying data in JTextfield

 
Talal Mohammed
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • 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
 
Ralph Cook
Ranch Hand
Posts: 479
  • Mark post as helpful
  • send pies
  • 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.
 
Randall Twede
Ranch Hand
Posts: 4371
3
Java
  • Mark post as helpful
  • send pies
  • 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
  • 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: 4371
3
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ok i show you my example i hope it helps
 
Rob Camick
Ranch Hand
Posts: 2565
9
  • Mark post as helpful
  • send pies
  • 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.
 
Campbell Ritchie
Sheriff
Posts: 48652
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sound like something which would fit better on our GUIs forum.
Moving.
 
Randall Twede
Ranch Hand
Posts: 4371
3
Java
  • Mark post as helpful
  • send pies
  • 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
  • Quote
  • Report post to moderator
Thank you all, I'll look into implementing a document listener.
<3
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic