title: Opening contents of a JList in new window
body: <p>I'm very new to Java.</p>
  
  <p>I've created two <code>JLists</code> in which you can add and remove 'shopping cart' items. </p>
  
  <p>Once the user has added all their items they can click a <code>submit</code> button to view their selected items in a new window. </p>
  
  <p>My first list is <code>itemList</code> (populated with items from array), the second list is <code>shoppinglist</code> which gets populated with whatever the user selects with a <code>JButton</code>. </p>
  
  <p>Additional arrays are created to handle the actions of the buttons moving the items to and from the <code>JLists</code>. I've tried a few things, but haven't been successful in showing the items that get selected and shown in <code>shopinglist</code> to appear in a new window once <code>submit</code> is hit. </p>
  
  <p>Any help is much appreciated.</p>
  
  <pre><code>//Create itemList
  itemList = new JList(shopping);
  contentPane.add(itemList);
  itemList.setVisibleRowCount(10);
      itemList.setFixedCellHeight(20);
      itemList.setFixedCellWidth(140);
      itemList.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
  
  
      //Add JScrollPane to maintain size
      JScrollPane list1 = new JScrollPane(itemList);
      //contentPane.add(list1);
  
      //Create shoppingList
      shoppingList = new JList(items);
      contentPane.add(shoppingList);
      shoppingList.setVisibleRowCount(10);
      shoppingList.setFixedCellHeight(20);
      shoppingList.setFixedCellWidth(140);
      shoppingList.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
  
      //Add JScrollPane to maintain size
      JScrollPane list2 = new JScrollPane(shoppingList);
  
      JPanel buttonPanel = new JPanel();
  
      //contentPane.add(list2);
  
      Buttonin = new JButton("&gt;&gt;");
      //Buttonin.setBounds(144, 46, 60, 23);
      Buttonin.addActionListener(this);
      buttonPanel.add(Buttonin);
  
      ButtonOut = new JButton("&lt;&lt;");
      //ButtonOut.setBounds(144, 80, 60, 23);
      ButtonOut.addActionListener(this);
      buttonPanel.add(ButtonOut);
  
      JPanel submitPanel = new JPanel();
  
      submitButton = new JButton("Submit");
      submitPanel.add(submitButton);
  
      submitButton.addActionListener(new ActionListener(){
          @Override
          public void actionPerformed(ActionEvent az) {
              JFrame frame = new JFrame ("Go Shopping!");
              frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
              frame.getContentPane().add(new MyPanel2());
              frame.pack();
              frame.setVisible(true);
  
          }
      });
  
  
      contentPane.add(list1);
      contentPane.add(buttonPanel);
      contentPane.add(list2);
      contentPane.setOpaque(true);
      contentPane.add(submitPanel);
      return contentPane;
  
  
  }
  
  
  
  
  public void actionPerformed(ActionEvent e) 
  {
      int i = 0;
  
      //When in buttonin is pressed index and value of selected item is output to array
  
      if (e.getSource() == Buttonin)
      {
  
          int[] fromindex = itemList.getSelectedIndices();
          Object[] from = itemList.getSelectedValues();
  
          //add items to the shoppingList
          for (i = 0; i &lt; from.length; i++)
          {
              items.addElement(from[i]);
  
          }
          //Must remove items that are selected from the itemList
          for (i = (fromindex.length-1); i &gt;= 0; i--)
          {
              shopping.remove(fromindex[i]);
          }
  
      }   
  
  
      //When out button is pressed index and value of selected item is output to new array
  
      else if (e.getSource() == ButtonOut)
      {
          Object[] to = shoppingList.getSelectedValues();
          int [] toindex = shoppingList.getSelectedIndices();
  
          //add items to previous list
             for(i = 0; i &lt; to.length; i++)
              {
                  shopping.addElement(to[i]);
              }
           //Must remove what's deselected
             for (i = (toindex.length-1); i &gt;= 0; i--)
              {
                  items.remove(toindex[i]);
              }
      }
  </code></pre>
  
  <p>Ok, bear with me (very very new to java) is this how I would set up the constructor to reference to ProfileFrame objects? And if so how do I change my main to reflect the new constructor?</p>
  
  <pre><code>public class GoShopping extends JPanel {
  
  private JList shopList;
  
  public GoShopping(ProfileFrame slist) {
  //construct components
  
  
  shopList = new JList(slist.getListModel());
  shopList.setBounds(6, 6, 123, 166);//don't worry I'm changing the layout
  add(shopList);
  }
  
  
  public static void main (String[] args) {
  JFrame frame = new JFrame ("MyPanel");
  frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
  
  //I need new GoShopping to reflect the new constructor, but not sure how to make it    work
  frame.getContentPane().add (new GoShopping());
  frame.pack();
  frame.setVisible (true);
  }
  }
  </code></pre>