简体   繁体   中英

Printing String of JList Selections with ListSelectionListener in Java

fairly new to java here. I'm writing a GUI program of which I want to be able to append the strings (rather than the index number) of selected list items to a text area using a JButton. I am unaware of which java method would allow me to do this. When I use the getSelectedIndex method, it only allows me to append the index number, rather than the string value of the list item to my text area. If it is still unclear what I am asking, here is my code:

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;

public class FantasyInterface extends JFrame implements ActionListener{

private JList list1;
private JList list2;
private JLabel runningbacks;
private JButton addPlayer1;
private JButton addPlayer2;
private JTextArea text;
String lineSeparator = System.getProperty("line.separator");
private String[] rbs = {"Matt Forte", "Arian Foster", "Maurice Jones-Drew", "Adrian Peterson", "Ray Rice"};
FantasyTeam team1 = new FantasyTeam(5);
FantasyTeam team2 = new FantasyTeam(5);

public FantasyInterface(){
    super("Fantasy Football Simulator");

    // Set up listsPanel
    JPanel listsPanel = new JPanel();

    runningbacks = new JLabel("Running Backs:");
    listsPanel.add(runningbacks);
    list1 = new JList(rbs);
    list1.setVisibleRowCount(5);
    list1.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
    listsPanel.add(list1);

    addPlayer1 = new JButton("Add To Team 1");
    listsPanel.add(addPlayer1);

    list2 = new JList(rbs);
    list2.setVisibleRowCount(5);
    list2.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
    listsPanel.add(list2);

    addPlayer2 = new JButton("Add To Team 2");
    listsPanel.add(addPlayer2);
    // Add formatted JPanels to Content Pane
    getContentPane().add(listsPanel, BorderLayout.NORTH);

    // Set up textPanel, where info will appear
    JPanel textPanel = new JPanel();
    text = new JTextArea(20, 20);
    textPanel.add(text);
    // Add formatted JPanels to Content Pane
    getContentPane().add(text, BorderLayout.SOUTH);

    ListSelectionListener listSelectionListener = new ListSelectionListener() {
          public void valueChanged(ListSelectionEvent listSelectionEvent) {
            //System.out.println("First index: " + listSelectionEvent.getFirstIndex());
            //System.out.println(", Last index: " + listSelectionEvent.getLastIndex());
            boolean adjust = listSelectionEvent.getValueIsAdjusting();
            //System.out.println(", Adjusting? " + adjust);
            if (!adjust) {
              JList list = (JList) listSelectionEvent.getSource();
              int selections[] = list.getSelectedIndices();
              Object selectionValues[] = list.getSelectedValues();
              for (int i = 0, n = selections.length; i < n; i++) {
                if (i == 0) {
                  System.out.println(" Selections: ");
                }
                System.out.println(selections[i] + "/" + selectionValues[i] + " ");
              }
            }
          }
        };
     list1.addListSelectionListener(listSelectionListener);
     addPlayer1.addActionListener(this);
}


public void actionPerformed(ActionEvent event){
    Object srcObj = event.getSource();

    if (srcObj == addPlayer1){
        text.append(lineSeparator + list1.getSelectedIndex());
    }
}

}

The last part,

if (srcObj == addPlayer1){
        text.append(lineSeparator + list1.getSelectedIndex());
    }

is where I am wondering if there is a method to get the selected index in string form. Thanks to anyone who helps!

Use:

if (!list1.isSelectionEmpty()) {
   text.append(lineSeparator + list1.getModel().getElementAt(list1.getSelectedIndex()));
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM