问题
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?
回答1:
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.
回答2:
Use the position of the item that is selected in the JList to tell you what text to set in the JTextArea in the middle. Setup a listener on the JList and check for onChange events. Grab the index of the selected element. In an array somewhere, use that index to grab the relevant text that is mapped to that JList element that was selected. Then set the text of the JTextArea to what you grabbed from the array.
来源:https://stackoverflow.com/questions/11695454/select-item-in-list-show-text-in-text-area