We help IT Professionals succeed at work.

Java Lists

Kimberly1467
Kimberly1467 asked
on
Medium Priority
204 Views
Last Modified: 2010-03-31
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

Comment
Watch Question

Mayank SPrincipal Technologist
CERTIFIED EXPERT

Commented:
What seems to be the problem?
Mayank SPrincipal Technologist
CERTIFIED EXPERT

Commented:
You might try calling the update () method of pnl2 and see what happens.
Mayank SPrincipal Technologist
CERTIFIED EXPERT

Commented:
>> 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.
zzynxSr. Software engineer
CERTIFIED EXPERT

Commented:
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();
       }
}
zzynxSr. Software engineer
CERTIFIED EXPERT

Commented:
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
zzynxSr. Software engineer
CERTIFIED EXPERT

Commented:
Sorry, I meant of course:

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

instead of

          pnl2.lst1.add(...);
Mayank SPrincipal Technologist
CERTIFIED EXPERT

Commented:
Yes, you should use accessor/ mutator methods.

Author

Commented:
zzynx,

When I select "Pepsi" it does not appear in the 2nd panel.
Mayank SPrincipal Technologist
CERTIFIED EXPERT

Commented:
Maybe that you should post your full code (the ListPanel, ButtonPanel, etc).

Author

Commented:
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

Commented:
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
zzynxSr. Software engineer
CERTIFIED EXPERT

Commented:
>> When I select "Pepsi" it does not appear in the 2nd panel.
Even if you adapt your actionPerformed() function as indicated in my comment?
Sr. Software engineer
CERTIFIED EXPERT
Commented:
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION
zzynxSr. Software engineer
CERTIFIED EXPERT

Commented:
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.
zzynxSr. Software engineer
CERTIFIED EXPERT

Commented:
Thanks for accepting.
This keeps us answering your future questions too.
:°)
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a sample view!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.