简体   繁体   中英

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 ?" However, it was posted 5 years ago, and my question is little bit different with that article.

The problem is that the getSelectedValues() method is deprecated now which is used in the private inner class 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.

I have two questions.

  1. What is 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 ?)?

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);
    }
}

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