• 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

How to fill text fields with selection from JList

 
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have an addressBook GUI where I have a JList that populates with the contacts names, and once I double click a contacts name I'm wanting to fill the textfields with the contacts corresponding data.

ex) ContactType: Family (enum), Name: Zoidberg, Address: 111 Space Drive, City: New York City, etc...

I've got it to where I select open from the JMenu, it populates the JList, but once I select a contacts name, all the textfields are populated, but only with the contacts name

ex) Name: Zoidberg, Address: Zoidberg, City: Zoidberg, etc....

What am i doing wrong here, and how can i fix it to where it fills out the correct data?

Here's my code so far:



I'm certain the logic is messed up near the end where i set all text fields to the index, because no matter what field i want to fill it's at it's going to set it to whatever index I select, but I don't know how to fix it, any help would be great, Thanks.
 
Marshal
Posts: 28175
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You'd have to store it somewhere, instead of just reading it from the Contacts.txt file and ignoring it. I would recommend putting each contact's information into a Contact object (which you would write a data-access class to represent) and also create a Map whose keys are names and whose values are Contact objects. Then double-clicking on a list entry would take the name, find the associated Contact object from the map, and get the data from that Contact object to put into the text fields.

By the way the usual way to select something from a JList is to just single-click on the JList entry. If you do that then you could use an ordinary ListSelectionListener instead of having to hack around with MouseListeners. But that's a topic for after you get your basic design fixed up.
 
Rancher
Posts: 3324
32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Crossposted: http://stackoverflow.com/questions/30005687/how-would-i-fill-my-textfields-after-i-select-an-item-from-jlist

The answer was given 4 hours ago.
 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Make each item in the list a an object that contains all the information for a contact, i.e. when you read in a string of info, parse it into different values, each of which is a member of the contact object. You can create each object by passing the data values to the object's constructor. You might also want to consider overriding the toString() method in the object to define what you see in the list. When you click a list item, you now have access to the object that represents the entire entity.
 
john lampard
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Make each item in the list a an object that contains all the information for a contact, i.e. when you read in a string of info, parse it into different values, each of which is a member of the contact object. You can create each object by passing the data values to the object's constructor. You might also want to consider overriding the toString() method in the object to define what you see in the list. When you click a list item, you now have access to the object that represents the entire entity.



Would I go about it something like this?

 
Steve Beckle
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, this is what I had in mind. Your list model will now contain these contact objects that contain all of the info for the contact. When you select the entry in the list, your list selection model will allow you to get the selected item, which, if you parameterize your list model properly, will automatically be a Contact object. You can always make a list/combo box hold any kind of object you want to, and as I mentioned before, if you override the toString() method for the Contact object, you can have your list display anything you want. For example,



I would also parameterize my list model:



That ensures type safety, (you can only add Contact objects to the list model) and when you work with a list selection object, you know you're dealing with a Contact object and don't have to do any casting.

 
john lampard
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is just frustrating the brain cells out of me, so i've modified my read method to look like this.



then added the open action listener in the JMenu, which is here.


contactList, is my JList, and getContactList method is in my AddressBook class which looks like

list here being an ArrayList <Contacts>.

Whenever I run the program, and try to open the file, I get a load of errors, here's a snippet of that:



line 708 is where I declare String [] info = line. split(",");
and line 367 is where I call the loadFile();

What am I doing wrong here, please help!!
 
Rob Camick
Rancher
Posts: 3324
32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why do you have a List of Contacts? The DefaultListModel holds all your Contacts.

If you want a specific contact when the user clicks on the list you get the contact from the ListModel or from the JList itself.

That is the selected item will be the Contact that was selected.
 
john lampard
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Rob Camick wrote:Why do you have a List of Contacts? The DefaultListModel holds all your Contacts.

If you want a specific contact when the user clicks on the list you get the contact from the ListModel or from the JList itself.

That is the selected item will be the Contact that was selected.



So, my contactsList would do the displaying, and if I select something I should call from the DefaultListModel?
 
Rob Camick
Rancher
Posts: 3324
32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well you can use the JList.getSelectedValue() method which will get the data from the DefaultListModel for you.
 
john lampard
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Rob Camick wrote:Well you can use the JList.getSelectedValue() method which will get the data from the DefaultListModel for you.



That's what i'm doing, but my problem is actually trying to populate the list with my contacts names.
 
Rob Camick
Rancher
Posts: 3324
32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Look at your code:



You assign null to the line variable.
 
john lampard
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Rob Camick wrote:Look at your code:



You assign null to the line variable.



If I don't assign it to anything, I'll get an error saying "line" may not have been initialized.
 
Paul Clapham
Marshal
Posts: 28175
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

john lampard wrote:If I don't assign it to anything, I'll get an error saying "line" may not have been initialized.



Yeah... but you should initialize it to something which isn't pointless. In particular if you're about to scan through it and split it based on commas, you should initialize it to something which is going to have some commas in it. Presumably you've already got a string like that somewhere?
 
john lampard
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No, this part of the code is the only part where I declare an actual string, all the others would be an array or arraylist of strings, I don't get it because my readStates method handles that problem fine.

 
Rob Camick
Rancher
Posts: 3324
32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

because my readStates method handles that problem fine.



Well, the readStates() method actually reads some text into the variable.

In your other code you set the variable to null and never add any text to the variable!!! Data doesn't just magically appear in the variable.
 
john lampard
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Rob Camick wrote:
Well, the readStates() method actually reads some text into the variable.

In your other code you set the variable to null and never add any text to the variable!!! Data doesn't just magically appear in the variable.



Gotcha change it to this:



Still nothing is coming up when i activate the openFile listener.
 
I miss the old days when I would think up a sinister scheme for world domination and you would show a little emotional support. So just look at this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic