简体   繁体   English

将数据从JList复制到(另一个)JList

[英]Copying data from JList to (another) JList

Briefly, the program displays the user's chosen options(months) to a screen. 简而言之,该程序将用户选择的选项(月份)显示在屏幕上。

I saw the question titled with "How to copy data from one JList to another JList ?" 我看到了标题为“如何将数据从一个JList复制到另一个JList ?”的问题。 However, it was posted 5 years ago, and my question is little bit different with that article. 但是,它是5年前发布的,我的问题与那篇文章有点不同。

The problem is that the getSelectedValues() method is deprecated now which is used in the private inner class ButtonListener . 问题是现在不赞成使用getSelectedValues()方法,该方法在私有内部类ButtonListener

I know that the method is replaced with getSelectedValuesList() , but it returns a List<E> now and the setListData method accepts array as its argument, so they don't work together. 我知道该方法setListData getSelectedValuesList()代替,但它现在返回List<E> ,而setListData方法接受array作为其参数,因此它们不能一起工作。

I have two questions. 我有两个问题。

  1. What is E ? 什么是E
  2. How can I copy multiple data from a JList to another JList (What is the most efficient way to do it? is it using DefaultListModel ?)? 如何将多个数据从一个JList复制到另一个JList (最有效的方法是什么?使用DefaultListModel吗?)?

Here is the code. 这是代码。

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


public class MultipleIntervalSelection extends JFrame
{
   private JPanel monthPanel;
   private JPanel selectedMonthPanel;
   private JPanel buttonPanel;

   private JList<String> monthList;
   private JList<String> selectedMonthList;

   private JScrollPane scrollPane1;
   private JScrollPane scrollPane2;

   private JButton button;

   private String[] months = {"January", "February", "March", "April", "May",
                              "June", "July", "August", "September", "October", 
                              "November", "December"};

   public MultipleIntervalSelection()
   {
      setTitle("Multi Selections");

      setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

      setLayout(new BorderLayout());

      buildMonthPanel();
      buildSelectedMonthsPanel();
      buildButtonPanel();

      add(monthPanel, BorderLayout.NORTH);
      add(selectedMonthPanel, BorderLayout.CENTER);
      add(buttonPanel, BorderLayout.SOUTH);            

      pack();
      setVisible(true);
   }

   private void buildMonthPanel()
   {
      monthPanel = new JPanel();

      monthList = new JList<>(months);

      monthList.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);

      monthList.setVisibleRowCount(6);

      scrollPane1 = new JScrollPane(monthList);

      monthPanel.add(scrollPane1);
   }                            

   private void buildSelectedMonthsPanel()
   {
      selectedMonthPanel = new JPanel();

      selectedMonthList = new JList<>();

      selectedMonthList.setVisibleRowCount(6);

      scrollPane2 = new JScrollPane(selectedMonthList);

      selectedMonthPanel.add(scrollPane2);
   }        

   private void buildButtonPanel()
   {
      buttonPanel = new JPanel();

      button = new JButton("Get Selections");

      button.addActionListener(new ButtonListener());

      buttonPanel.add(button);
   }

   private class ButtonListener implements ActionListener
   {
      public void actionPerformed(ActionEvent e)
      {
         Object[] selections = monthList.getSelectedValues();

         selectedMonthList.setListData(selections);
      }
   }

   public static void main(String[] args)
   {
      new MultipleIntervalSelection();
   }
}

不推荐使用的getSelectedValues()更改为getSelectedValuesList() ,因此必须将所选值保存到List

Simply get the list and transform it to an array, like this: 只需获取列表并将其转换为数组即可,如下所示:

private class ButtonListener implements ActionListener {

    public void actionPerformed(ActionEvent e) {
        List<String> list = monthList.getSelectedValuesList();
        int size = list.size();
        String[] values = new String[size];
        for (int ii=0; ii<size; ii++) {
            values[ii] = list.get(ii);
        }
        selectedMonthList.setListData(values);
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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