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

问题描述 投票:2回答:2

我目前创建了一个多区间选择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();
    }
}
java jbutton jlist
2个回答
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();
  }
}

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);
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.