-1

I have a program utilizing JTabbedPane. On one pane I have a button that updates an arrayList of objects based on input from the same pane.

What I would like to happen is have the second pane update itself with the object information based on the arrayList in the first pane.

However, I am not sure how to pass the list between the panes. Is there some way to push the array to pane #2 when the update button on the first pane is pressed?

Here is the main file. Instantiating the two tabs

public class Assignment6 extends JApplet
 {

   private int APPLET_WIDTH = 650, APPLET_HEIGHT = 350;

   private JTabbedPane tPane;
   private StorePanel storePanel;
   private PurchasePanel purchasePanel;
   private ArrayList computerList;


   public void init()
    {

     computerList = new ArrayList();


     storePanel = new StorePanel(computerList, purchasePanel);
     purchasePanel = new PurchasePanel(computerList);




     tPane = new JTabbedPane();
     tPane.addTab("Store Inventory", storePanel);
     tPane.addTab("Customer Purchase", purchasePanel);

     getContentPane().add(tPane);
     setSize (APPLET_WIDTH, APPLET_HEIGHT); //set Applet size
    }
}

The first panel instantiates a button listener that applies all of the logic to the array list "compList"

private class ButtonListener implements ActionListener
   {
    public void actionPerformed(ActionEvent event)
     {
                //Add Computer to list
                Computer comp = new Computer();
                comp.setBrandName(brandField.getText());
                comp.setPrice(Double.parseDouble(priceField.getText()));
                comp.setMemory(Integer.parseInt(memoryField.getText()));
                comp.setCPU(typeField.getText(), Integer.parseInt(speedField.getText()));

                compList.add(comp);
                }
                stringField.setText(listString);


                alertLabel.setText("Computer Added");
            }
        }

Here is the other pane. The for loop at the end is what I need to push the arrayList to. After it receives the list, it populates a box with a checkbox for each object in the list

public PurchasePanel(ArrayList compList)
{
  west = new JPanel();
  east = new JPanel();
  totalField = new JTextField();

  this.compList = compList;
  setLayout(new GridLayout(0,2));
  add(west);
  add(east);
  east.setLayout(new BorderLayout());
  east.add(currentTotalLabel, BorderLayout.NORTH);
  east.add(totalField, BorderLayout.CENTER);
  west.setLayout( new BoxLayout(west, BoxLayout.Y_AXIS));

  for(Object c : compList){
      System.out.println("Made it");

      NumberFormat fmt = NumberFormat.getCurrencyInstance();

      String str = ("BrandName:" + (((Computer) c).getBrandName() +"CPU:" + (((Computer) c).getCPU() +"Memory:" + ((Computer) c).getMemory() + "M" +"Price:" + fmt.format(((Computer) c).getPrice()))));


      JCheckBox chk = new JCheckBox(str);
      west.add(chk);

    }
}

 }
jrounsav
  • 517
  • 2
  • 6
  • 20
  • There definitely is a way. Why don't you post your code so we can see exactly what you're working with? – ControlAltDel Oct 06 '14 at 19:15
  • Code posted. I left out all of the GUI setup @ControlAltDel – jrounsav Oct 06 '14 at 19:33
  • 1
    The best way to solve this problem is to use off-the-shelf Swing/etc components in 1 class that sets up and controls them all. That way, your logic about what needs to be done if your List changes can be handled at the top layer which has access to everything – ControlAltDel Oct 06 '14 at 19:42

1 Answers1

0

You can use the same listener used to update the first ArrayList, to update the second pane. Something like:

jButton1.addActionListener(new ActionListener() {
    @Override
    public void actionPerformed(ActionEvent e) { 
        // Update the first ArrayList
        // Update the second pane
    }
jmm
  • 1,044
  • 2
  • 12
  • 38