如何使用列表中的选定值?

时间:2019-02-14 16:27:10

标签: java jbutton jlist

我当前创建了一个多区间选择JList,并且我希望能够对选择做一些事情。目前,我的代码仅能创建一个JList,可以在其中“突出显示”所选对象。但是,我要做的是将选定的对象放入Array或ArrayList中,以便我可以对这些对象执行一些工作。我已经在线搜索并找到有关getSelectedValueList()的信息,但我真的不知道如何使用和访问它。

我的另一个问题是,我正在考虑创建一个名为“ Submit”的JButton,但我也想知道应该如何对带有JList的窗口进行处理。按下按钮后我应该退出吗?

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

    public class ClassSelection extends JFrame {
         private JList classList;
         private String[] classNames = {"Programming Foundations I", 
         "Programming Foundations II",
         "Digital Design", "Computer Organization", "Programming 
         Paradigms", 
         "Honors Programming Paradigms", "Cluster Computing", 
         "Software Engineering"};

     public ClassSelection() {
         super("Classes Taken:");

         Container container = getContentPane();
         container.setLayout(new FlowLayout());

         classList = new JList(classNames);
         classList.setVisibleRowCount(5);
         classList.setFixedCellHeight(45);
         classList.setFixedCellWidth(450);

classList.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTIO N);

         container.add(new JScrollPane(classList));
         setSize(600, 600);
         setVisible(true);

    }

    public static void main(String[] args) {
         ClassSelection application = new ClassSelection();
    }
}

2 个答案:

答案 0 :(得分:3)

请参阅下面的代码。它包含您的要求。

第二个问题的代码与您所说的一样。

  

“按下按钮后我应该退出吗?”

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JScrollPane;
import javax.swing.ListSelectionModel;
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.ArrayList;
import java.util.List;

public class ClassSelection extends JFrame {
    private JList classList;
    private String[] classNames = {"Programming Foundations I",
            "Programming Foundations II",
            "Digital Design", "Computer Organization", "Programming Paradigms",
            "Honors Programming Paradigms", "Cluster Computing",
            "Software Engineering"};

    public ClassSelection() {
        super("Classes Taken:");

        Container container = getContentPane();
        container.setLayout(new FlowLayout());

        classList = new JList(classNames);
        classList.setVisibleRowCount(5);
        classList.setFixedCellHeight(45);
        classList.setFixedCellWidth(450);
        classList.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
        JScrollPane jScrollPane = new JScrollPane(classList);

        // First question
        List<String> myList = new ArrayList<>();

        classList.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                super.mouseClicked(e);
                JList jList = (JList) e.getSource();
                String className = (String) jList.getSelectedValue();
                myList.add(className);
                System.out.println("clicked : " + className);

                // you can see the items being appended
                System.out.println("list : " + myList.toString());
            }
        });

        // Second question
        JButton btn = new JButton("Submit");
        btn.addActionListener(event -> {
            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            dispose();
        });

        container.add(jScrollPane);
        container.add(btn);
        setSize(600, 600);
        setVisible(true);
    }

    public static void main(String[] args) {
        ClassSelection application = new ClassSelection();
        application.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
}

答案 1 :(得分:0)

有两种方法可以做到这一点。我个人认为“方法1”就足够了。但是,如果您确实要在用户按下按钮之前收集选定的项目,请使用“方法2”。

方法1-在按钮的动作监听器中调用getSelectedValuesList()

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

public class ClassSelection extends JFrame {
  private JList classList;
  private String[] classNames = {"Programming Foundations I",
      "Programming Foundations II",
      "Digital Design", "Computer Organization",
      "Programming Paradigms",
      "Honors Programming Paradigms", "Cluster Computing",
      "Software Engineering"};

  public ClassSelection() {
    super("Classes Taken:");

    Container container = getContentPane();
    container.setLayout(new FlowLayout());

    classList = new JList(classNames);
    classList.setVisibleRowCount(5);
    classList.setFixedCellHeight(45);
    classList.setFixedCellWidth(450);

    classList.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);

    container.add(new JScrollPane(classList));

    JButton button = new JButton("Print selected items");
    button.addActionListener(new ActionListener() {
      @Override
      public void actionPerformed(ActionEvent e) {
        List selectedItems = classList.getSelectedValuesList();
        System.out.println(selectedItems);
      }
    });
    container.add(button, BorderLayout.SOUTH);

    setSize(600, 600);
    setVisible(true);

  }

  public static void main(String[] args) {
    ClassSelection application = new ClassSelection();
  }
}

方法2-使用ListSelectionListener

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import java.util.*;
import java.util.List;

public class ClassSelection2 extends JFrame {
  private JList classList;
  private List<String> selectedItemsList = new ArrayList<>();

  private String[] classNames = {"Programming Foundations I",
      "Programming Foundations II",
      "Digital Design", "Computer Organization",
      "Programming Paradigms",
      "Honors Programming Paradigms", "Cluster Computing",
      "Software Engineering"};

  public ClassSelection2() {
    super("Classes Taken:");

    Container container = getContentPane();
    container.setLayout(new FlowLayout());

    classList = new JList(classNames);
    classList.setVisibleRowCount(5);
    classList.setFixedCellHeight(45);
    classList.setFixedCellWidth(450);

    classList.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
    classList.addListSelectionListener(new ListSelectionListener() {
      @Override
      public void valueChanged(ListSelectionEvent e) {
        if (!e.getValueIsAdjusting()) {
          selectedItemsList.clear();
          selectedItemsList.addAll(classList.getSelectedValuesList());
        }
      }
    });

    container.add(new JScrollPane(classList));

    JButton button = new JButton("Print selected items");
    button.addActionListener(new ActionListener() {
      @Override
      public void actionPerformed(ActionEvent e) {
        System.out.println(selectedItemsList);
      }
    });
    container.add(button, BorderLayout.SOUTH);

    setSize(600, 600);
    setVisible(true);

  }

  public static void main(String[] args) {
    ClassSelection2 application = new ClassSelection2();
  }
}