Search code examples
javaswinguser-interfacejlistjtextarea

Select item in list, show text in text area


I am working on a rough draft of a gui, looking at different options. I was hopping to use a JList to show text in a JTextArea based on what item is selected. You can see the JList on the left and the JTextArea in the center.

Or is there a better way to do this? I am already using tabs that will be used for a broad category. I saw that the CardLayout, but don't quite like the look. Any tips?

enter image description here


Solution

  • Well, you can use a ListSelectionListener to set the content accordingly..

     JList list = new JList(someArrayofData);
     list.addListSelectionListener(new ListSelectionListener(){
          public void valueChanged(ListSelectionEvent e){
                int selectedIndex = list.getSelectedIndex();
                //refresh the content based on the index
                setContent(selectedIndex);
          }
    });
    

    Its hard to say which gui layout will work the best for you without knowing what the application is for.