Solved

Java Lists

Posted on 2004-04-09
15
176 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:mayankeagle
Comment Utility
What seems to be the problem?
0
 
LVL 30

Expert Comment

by:mayankeagle
Comment Utility
You might try calling the update () method of pnl2 and see what happens.
0
 
LVL 30

Expert Comment

by:mayankeagle
Comment Utility
>> 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
Comment Utility
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
Comment Utility
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
Comment Utility
Sorry, I meant of course:

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

instead of

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

Expert Comment

by:mayankeagle
Comment Utility
Yes, you should use accessor/ mutator methods.
0
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 

Author Comment

by:Kimberly1467
Comment Utility
zzynx,

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

Expert Comment

by:mayankeagle
Comment Utility
Maybe that you should post your full code (the ListPanel, ButtonPanel, etc).
0
 

Author Comment

by:Kimberly1467
Comment Utility
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
Comment Utility
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
Comment Utility
>> 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
Comment Utility
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
Comment Utility
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
Comment Utility
Thanks for accepting.
This keeps us answering your future questions too.
:°)
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
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 arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

772 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

9 Experts available now in Live!

Get 1:1 Help Now