Link to home
Start Free TrialLog in
Avatar of Kimberly1467
Kimberly1467

asked on

Java Lists

I am trying to write a program that has 1 frame and 3 panels. In the 1st panel is a list of beverages, the 3rd panel has add, remove, clear, and total buttons. I need help with having the selected item in the 1st panel to be added to the 2nd panel once the add button has been pressed. Here is what I have written thus far:

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


public class ListFrame extends Frame implements ActionListener {

  ListPanel pnl1 = new ListPanel();
  ListPanel pnl2 = new ListPanel();
  ButtonPanel pnl3 = new ButtonPanel();


  public  ListFrame() {
    List lst1 = new List(4, false);
    setLayout(new GridLayout(1,3));
    add(pnl1);
    pnl1.lst1.setForeground(Color.black);
    pnl1.lst1.setBackground(Color.green);
    pnl1.lst1.add("Pepsi");
    pnl1.lst1.add("Mountain Dew");
    pnl1.lst1.add("Lemonade");
    pnl1.lst1.add("Ice Tea");
       pnl1.lst1.addActionListener(this);
    add(pnl2);
    pnl2.lst1.setForeground(Color.black);
    pnl2.lst1.setBackground(Color.pink);
    add(pnl3);
    pnl3.setBackground(Color.orange);


  } // ListFrame()


public void actionPerformed(ActionEvent e)
 {
if(e.getSource() == pnl3.btn1)
{
pnl2.lst1.add(pnl1.lst1.getSelectedItem() );


}

 }// actionPerformed



} // ListFrame

Avatar of Mayank S
Mayank S
Flag of India image

What seems to be the problem?
You might try calling the update () method of pnl2 and see what happens.
>> List lst1 = new List(4, false);

By the way, what is the significance of this? You seem to have a lst1 member in ListPanel anyway.
Is it that you don't see it appear in the list?

try:

public void actionPerformed(ActionEvent e) {
       if (e.getSource() == pnl3.btn1) {
          pnl2.lst1.add(pnl1.lst1.getSelectedItem() );
          pnl2.lst1.revalidate();
          pnl2.lst1.repaint();
       }
}
You better forsee a getList() function on your ListPanel class returning the list that is in it.

Than you could write:

          pnl2.getList().add(...);

instead of

          pnl2.lst1.revalidate();

It's nicer OO
Sorry, I meant of course:

          pnl2.getList().add(...);

instead of

          pnl2.lst1.add(...);
Yes, you should use accessor/ mutator methods.
Avatar of Kimberly1467
Kimberly1467

ASKER

zzynx,

When I select "Pepsi" it does not appear in the 2nd panel.
Maybe that you should post your full code (the ListPanel, ButtonPanel, etc).
mayankeagle,

Here is the ButtonPanel:

import java.awt.*;
import java.awt.event.*;

public class ButtonPanel extends Panel {
 Button btn1 = new Button ("Add");
 Button btn2 = new Button ("Remove");
 Button btn3 = new Button ("Clear");
 Button btn4 = new Button ("Total");
 Label lbl1 =  new Label();


  public ButtonPanel() {
     setLayout(new GridLayout(5,1));
     add(btn1);
     add(btn2);
     add(btn3);
     add(btn4);
     add(new Label());

  } // ButtonPanel()
} // ButtonPanel
Hi there;

I created an example for you to get you started :

/**
 * Program Created By : Javatm
 * Date  : 4/10/2004
 * Time : 10 : 00 AM
 */
 
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class ListFrame extends JFrame {

    public  ListFrame() {

    super("Example . . .");

    JPanel p1 = new JPanel();
    JPanel p2 = new JPanel();
    JPanel p3 = new JPanel();
    JPanel p4 = new JPanel();
    JPanel p5 = new JPanel();

    String[] s1 = {"1. Coke", "2. Pepsi", "3. Sprite"};

    Font f1 = new Font("Arial", Font.PLAIN, 12);

    String i1 = " Click on an item 1st \n before you press\n move.";

    final JList l1 = new JList(s1);
    final JTextArea l2 = new JTextArea();
    final JTextArea l3 = new JTextArea();

    l3.setText(i1); l3.setEditable(false);
    l2.setEditable(false);

    JButton b1 = new JButton("   Move   ");
    JButton b2 = new JButton("   Clear   ");
    JButton b3 = new JButton("    Exit     ");

    p2.setLayout(new GridLayout(2,1));
    p4.setLayout(new GridLayout(1,1));
    p5.setLayout(new FlowLayout());

    p4.add(l3);
    p5.add(b1); p5.add(b2);
    p5.add(b3); p2.add(p4);
    p2.add(p5); l1.setFont(f1);

    l1.setBorder(BorderFactory.createTitledBorder("Section 1 :"));
    l2.setBorder(BorderFactory.createTitledBorder("Section 2 :"));
    l3.setBorder(BorderFactory.createTitledBorder("Section 3 :"));
    p3.setBorder(BorderFactory.createLineBorder(Color.gray));

    p1.setLayout(new GridLayout(1,3));
    p1.add(new JScrollPane(l1));
    p1.add(new JScrollPane(l2));
    p1.add(p2);

    this.getRootPane().setDefaultButton(b1);
    this.getContentPane().add(p1, BorderLayout.CENTER);
    this.getContentPane().add(p3, BorderLayout.SOUTH);

    Dimension sd = Toolkit.getDefaultToolkit().getScreenSize();
    setLocation(sd.width / 2 - 400 / 2,
    sd.height / 2 - 300 / 2);

    setResizable(false);
    setSize(400, 300);
    setVisible(true);

    b1.addActionListener(new ActionListener() {

    public void actionPerformed(ActionEvent h)
    {
    int x1 = l1.getSelectedIndex();

    if(x1 == 0) {
    l2.setText("1. Coke");    
    }
    else if(x1 == 1) {
    l2.append("\n2. Pepsi");    
    }
    else if(x1 == 2) {
    l2.append("\n3. Sprite");    
    }
    else {
    JOptionPane.showMessageDialog(null, "Sorry Option Not Allowed !", "Error . . .",    
    JOptionPane.ERROR_MESSAGE);    
    }
    }
    });

    b2.addActionListener(new ActionListener() {

    public void actionPerformed(ActionEvent h)
    {
    l2.setText("");
    }  
    });

    b3.addActionListener(new ActionListener() {

    public void actionPerformed(ActionEvent h)
    {
    System.exit(0);
    }  
    });
    }

    public static void main(String args[]) {
 
    ListFrame x = new ListFrame();
    x.setDefaultCloseOperation(EXIT_ON_CLOSE);

    }
}

Hope that helps . . .
Javatm
>> When I select "Pepsi" it does not appear in the 2nd panel.
Even if you adapt your actionPerformed() function as indicated in my comment?
ASKER CERTIFIED SOLUTION
Avatar of zzynx
zzynx
Flag of Belgium image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
By adding

                pnl3.getAddButton().addActionListener(this);          

you instruct that if the Add button of your ButtonPanel pnl3 [pnl3.getAddButton() ] is pressed
then the actionPerformed() function of your actionListener [ being your ListFrame (= this) ] should be triggered.
Thanks for accepting.
This keeps us answering your future questions too.
:°)