aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes Help with displaying data in JTextfield Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Help with displaying data in JTextfield" Watch "Help with displaying data in JTextfield" New topic
Author

Help with displaying data in JTextfield

Talal Mohammed
Greenhorn

Joined: Nov 26, 2011
Posts: 13
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

Joined: May 29, 2005
Posts: 479
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

Joined: Oct 21, 2000
Posts: 4347
    
    2

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.


SCJP
Visit my download page
Talal Mohammed
Greenhorn

Joined: Nov 26, 2011
Posts: 13
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

Joined: Oct 21, 2000
Posts: 4347
    
    2

ok i show you my example i hope it helps
Rob Camick
Ranch Hand

Joined: Jun 13, 2009
Posts: 2227
    
    8
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

Joined: Oct 13, 2005
Posts: 40052
    
  28
Sound like something which would fit better on our GUIs forum.
Moving.
Randall Twede
Ranch Hand

Joined: Oct 21, 2000
Posts: 4347
    
    2

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

Joined: Nov 26, 2011
Posts: 13
Thank you all, I'll look into implementing a document listener.
<3
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Help with displaying data in JTextfield