Ok, so here I have a program that creates two boxes. The top box should start out with all 12 months of the year in it. What I'm trying to have happen is be able to move the months (through the two buttons) back and forth. Originally it just had an Array that would send the months from the top to the bottom but wouldn't send them back up. Then I got it to be able to create the selected months in the opposite box. I think ArrayLists is the way to go here because I can just remove and add the selected months from one box to another. My problem here is syntax, I'm unfamiliar with all to combine ArrayLists and JLabels, any ideas?

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

/**
   This class demonstrates the List Component in
   multiple interval selection mode.
*/

public class MultipleSelection extends JFrame
{
   private JPanel monthPanel;         // To hold components
   private JPanel selectedMonthPanel; // To hold components
   private JPanel buttonPanel;        // To hold the buttons

   private JList monthList;         // To hold months
   private JList selectedMonthList; // Selected months

   private JScrollPane scrollPane1; // Scroll pane - first list
   private JScrollPane scrollPane2; // Scroll pane - second list

   private JButton buttonDown;          // A button
   private JButton buttonUp;          // A button

   // The following array holds the values that
   // will be displayed in the monthList list component.
   //private String[] months = { "January", "February",
    //        "March", "April", "May", "June", "July",
     //       "August", "September", "October", "November",
      //      "December" };

   private ArrayList<String> topBox = new ArrayList<String>();
   private ArrayList<String> botBox = new ArrayList<String>();



   /**
      Constructor
   */

   public MultipleSelection()
   {
       topBox.add (0, "January");
       topBox.add (1, "February");
       topBox.add (2, "March");
       topBox.add (3, "April");
       topBox.add (4, "May");
       topBox.add (5, "June");
       topBox.add (6, "July");
       topBox.add (7, "August");
       topBox.add (8, "September");
       topBox.add (9, "October");
       topBox.add (10, "November");
       topBox.add (11, "December");

      // Set the title.
      setTitle("List Demo");

      // Specify an action for the close button.
      setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

      // Add a BorderLayout manager.
      setLayout(new BorderLayout());

      // Build the panels.
      buildMonthPanel();
      buildSelectedMonthsPanel();
      buildButtonPanel();


      // Add the panels to the content pane.
      add(monthPanel, BorderLayout.NORTH);
      add(selectedMonthPanel,BorderLayout.SOUTH);
 //     add(buttonPanel, BorderLayout.SOUTH);
      add(buttonPanel, BorderLayout.CENTER);

      // Pack and display the window.
      pack();
      setVisible(true);
   }

   /**
      The buildMonthPanel method adds a list containing the
      names of the months to a panel.
   */

   private void buildMonthPanel()
   {
      // Create a panel to hold the list.
      monthPanel = new JPanel();

      // Create the list.
      monthList = new JList(months);

      // Set the selection mode to multiple
      // interval selection.
      monthList.setSelectionMode(
        ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);

      // Set the number of visible rows to 6.
      monthList.setVisibleRowCount(6);

      // Add the list to a scroll pane.
      scrollPane1 = new JScrollPane(monthList);

      // Add the scroll pane to the panel.
      monthPanel.add(scrollPane1);
   }

   /**
      The buildSelectedMonthsPanel method adds a list
      to a panel. This will hold the selected months.
   */

   private void buildSelectedMonthsPanel()
   {
      // Create a panel to hold the list.
      selectedMonthPanel = new JPanel();

      // Create the list.
      selectedMonthList = new JList();

      // Set the number of visible rows to 6.
      selectedMonthList.setVisibleRowCount(6);

      // Add the list to a scroll pane.
      scrollPane2 =
              new JScrollPane(selectedMonthList);

      // Add the scroll pane to the panel.
      selectedMonthPanel.add(scrollPane2);
   }

   /**
      The buildButtonPanel method adds a
      button to a panel.
   */

   private void buildButtonPanel()
   {
      // Create a panel to hold the list.
      buttonPanel = new JPanel();

      // Create the button.
      buttonDown = new JButton("move down");
      buttonUp = new JButton("move up");

      // Add an action listener to the button.
      buttonDown.addActionListener(new ButtonListener());
      buttonUp.addActionListener(new ButtonListenerUp());

      // Add the button to the panel.
      buttonPanel.add(buttonDown);
      buttonPanel.add(buttonUp);
   }

   /**
      Private inner class that handles the event when
      the user clicks the button.
   */

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

         // Store the selected items in selectedMonthList.
         selectedMonthList.setListData(selections);
      }
   }

   private class ButtonListenerUp implements ActionListener
   {
      public void actionPerformed(ActionEvent f)
      {
         // Get the selected values.
         Object[] selections =
                        selectedMonthList.getSelectedValues();


         // Store the selected items in selectedMonthList.

          monthList.setListData(selections);
      }
   }

   /**
   The main method creates an instance of the
   MultipleIntervalSelection class which causes it
   to display its window.
*/

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

Recommended Answers

All 2 Replies

my current idea is to edit line 94, replace -months- with -topBox- but the Eclipse doesn't like that

that's because they're not of the same type. just check the api for the JList and check what you can use there.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.