Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Java Lists

Posted on 2004-04-09
15
Medium Priority
?
188 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

0
Comment
Question by:Kimberly1467
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 7
  • 5
  • 2
  • +1
15 Comments
 
LVL 30

Expert Comment

by:Mayank S
ID: 10791338
What seems to be the problem?
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 10791354
You might try calling the update () method of pnl2 and see what happens.
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 10791361
>> 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.
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 37

Expert Comment

by:zzynx
ID: 10791386
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();
       }
}
0
 
LVL 37

Expert Comment

by:zzynx
ID: 10791399
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
0
 
LVL 37

Expert Comment

by:zzynx
ID: 10791412
Sorry, I meant of course:

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

instead of

          pnl2.lst1.add(...);
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 10791416
Yes, you should use accessor/ mutator methods.
0
 

Author Comment

by:Kimberly1467
ID: 10792393
zzynx,

When I select "Pepsi" it does not appear in the 2nd panel.
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 10793221
Maybe that you should post your full code (the ListPanel, ButtonPanel, etc).
0
 

Author Comment

by:Kimberly1467
ID: 10793297
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
0
 
LVL 15

Expert Comment

by:Javatm
ID: 10795324
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
0
 
LVL 37

Expert Comment

by:zzynx
ID: 10800104
>> When I select "Pepsi" it does not appear in the 2nd panel.
Even if you adapt your actionPerformed() function as indicated in my comment?
0
 
LVL 37

Accepted Solution

by:
zzynx earned 2000 total points
ID: 10800117
Oh, I think I see your problem.

It's not enough to have...
1) a button and
2) an actionPerformed() function

you have...
3) to link those two together via an Actionlistener.

So, for each button to work you have to call addActionListener( ... );

In your case:

(1) Forsee a function that returns your "Add" button:

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()

  public JButton getAddButton() {  return btn1; }

} // ButtonPanel

Then you can write:

(2)

public class ListFrame extends Frame implements ActionListener {

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


  public  ListFrame() {
    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);

    pnl3.getAddButton().addActionListener(this);          //  <<<<<<<<< Here's the magic !!!!!!!!!!!!!

  } // ListFrame()


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

Of course you have to do that for each of your buttons.
Success
0
 
LVL 37

Expert Comment

by:zzynx
ID: 10804519
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.
0
 
LVL 37

Expert Comment

by:zzynx
ID: 10807839
Thanks for accepting.
This keeps us answering your future questions too.
:°)
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
Suggested Courses

660 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question