Java - updating textFields from JList?

You can add the listener and then just query the currently selected index.

You can add the listener and then just query the currently selected index. I did a sample for you, I hope you find it useful. This is the relevant section: private JComponent list() { final JList list = new JList( data); list.

AddListSelectionListener(new ListSelectionListener() { public void valueChanged(ListSelectionEvent e) { int I = list. GetSelectedIndex(); nameTextField. SetText( I >= 0?Data.

Get( I ) : "" ); } }); return new JScrollPane( list ); } Bear in mind that's not the only way to go, this is just an starting point for you. Here's the complete working sample: import java.util. Vector; import java.util.

Arrays; import java.awt. BorderLayout; import javax.swing. JList; import javax.swing.

JPanel; import javax.swing. JTextField; import javax.swing. JFrame; import javax.swing.

JScrollPane; import javax.swing. JLabel; import javax.swing. JComponent; import javax.swing.event.

ListSelectionListener; import javax.swing.event. ListSelectionEvent; public class JListSample { private Vector data = new Vector( Arrays. AsList( new String { "one", "two", "three" }) ); private JTextField nameTextField; public static void main( String args) { JListSample s = new JListSample(); s.run(); } public void run() { JFrame frame = new JFrame("Selection test"); frame.

Add( list(), BorderLayout. WEST ); frame. Add( editPanel() ); frame.pack(); frame.

SetVisible( true ); } private JComponent list() { final JList list = new JList( data); list. AddListSelectionListener(new ListSelectionListener() { public void valueChanged(ListSelectionEvent e) { int I = list. GetSelectedIndex(); nameTextField.

SetText( I >= 0? Data. Get( I ) : "" ); } }); return new JScrollPane( list ); } private JComponent editPanel() { JPanel panel = new JPanel(); panel.

Add( new JLabel("Name:") ); nameTextField = new JTextField(10); panel. Add( nameTextField ); return panel; } } This is what is displayed.

You just add the selection listener to the list, like that: jl. AddSelectionListener(new ListSelectionListener() { public void valueChanged(ListSelectionEvent e) { // evaluate e if necessary and call a method // in your class to write the text in the textfield int selectedRow = e.getFirstIndex(); // more complicate for multiselects updateTextFieldWithName(selectedRow); // to be implemented } }); Using an anonymous class like here is the quickest way. It's a bit hard to read but a typical pattern.(just read you preferred an inner class, but I can't code that here on the fly with no IDE at hand...).

Yes you will want to use a ListSelectionListener for this, you will also probably want to set the list to single selection(ListSelectionModel. SINGLE_SELECTION). This will allow the user to only select one item in the list.

You can then add you listSelectionListener, and in the valueChanged of the event do something like the following(not exact syntax). ValueChanged(ListSelectionEvent e){ int idx = e.getFirstIndex(); int idx2 = e.getLastIndex(); //idx and idx2 should be the same if you set SingleSel if(idx==idx2){ //here you can get the person detail however you have them stored. You can get them from the model like so, Object personObj = MYLIST.getModel().

GetElementAt(int index); } }.

I think I understand that a JList must use ListSelectionListener but I cannot seem to implement this Well, then start by reading the JList API. You will find a link to the Swing tutorial on "How to Use Lists", which contains a working example. Also in the tutorial you will find a section on "How to Write a List Selection Listener" which contains a second example.

Start with the tutorial for your basic questions.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions