0

I am trying to select an item from the jList in one form (Home), and extract the data from the ArrayList and output the data to separate jTextFields in a different form (Details). Below is the method I'm trying to use to do this (not a lot there I know!).

public void passObjectData()
{
    int i = proObjList.getSelectedIndex();
}

I know once the method is complete, I can just call it on the form load method in the next form, but I'm stuck on how to get the method correct.

I don't know what other code, if any, will be needed for your help.

dic19
  • 17,821
  • 6
  • 40
  • 69
Nathan Janman
  • 25
  • 1
  • 7
  • 1
    Your question is not clear. Can you please explain more? – Maroun Nov 26 '14 at 12:03
  • Please put `swing`, `jlist` and `jtextfield` tags back. Those are correct. @MarounMaroun – dic19 Nov 26 '14 at 12:05
  • 1
    @dic19 I think the question is about passing values to methods in Java, which is irrelevant to Swing. If you think otherwise, please edit the question accordingly. – Maroun Nov 26 '14 at 12:06
  • Ok, I will try. I have a JavaSwing application, with many classes and many jFrame forms. I have hardcoded data into an ArrayList and have output a name to a jList. Now I want to get all of the data of one person that is stored in the ArrayList (name, address, tel num etc) and put this information into jTextFields. Easy if you are doing it in the same form! But I am not, the jTextFields are in a different form to the jList. So how can I use the .getSelectedIndex() function to select the item in the jList and display all related data to a different form with jTextFields? – Nathan Janman Nov 26 '14 at 12:11

1 Answers1

2

I have hardcoded data into an ArrayList and have output a name to a jList. Now I want to get all of the data of one person that is stored in the ArrayList (name, address, tel num etc) and put this information into jTextFields.

As I understand your question this ArrayList is the undelying data structure used to fill the ListModel and you want to get the selected index from the JList to retrieve the correct object stored in that array list. In this case you can:

  • Have a domain class called Person to hold the person's data (name, address, etc)
  • Add Person objects to the ListModel.
  • Provide an appropriate ListCellRenderer to display the person's name.
  • Use JList#getSelectedValue() to get the selected Person.
  • Pass this selected Person object to the text field's form and update those accordingly.
  • Optional: attach a ListSelectionListener to the JList in order to listen for user's selection changes and do the previous step automatically.

See the first 3 points of this approach exemplified here (note: the example is using JComboBox but the same applies to JList as well).

Suggested readings

Side note

Not sure if by forms you mean JFrames but just in case: please note that we should avoid using multiple JFrames. See this topic: The Use of Multiple JFrames, Good/Bad Practice?

Community
  • 1
  • 1
dic19
  • 17,821
  • 6
  • 40
  • 69