Solved

Java Lists

Posted on 2004-04-09
15
177 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
  • 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
 
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

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 500 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
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 learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…

914 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now