0

This is a program that implements a simple GUI.

I usually replace the show Method with setVisible(true) however, I'm not sure how to handle this problem when the show() method takes arguments, I've tried @SuppressWarnings("deprecated") but my compiler was unimpressed. Thanks in Advance

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

GUI object class

public class UI2 extends Frame
{

Declaring objects of Frame class

    public Button b;
    public TextField t;
    public FirstPanel p1;
    public SecondPanel p2;
    Choice c;

Constructor

    UI2()
    {
         setLayout(new GridLayout(2,1));

         p1 = new FirstPanel(this);
         add(p1);

         p2 = new SecondPanel(this); 
         add(p2);
         validate();
    }
}

FirstPanel class

class FirstPanel extends Panel implements ActionListener
{
    UI2 myUI;
    public PopupMenu pm1;
    public PopupMenu pm2;
public MenuItem pm1_1, pm1_2, pm2_1, pm2_2;

FirstPanel(UI2 myUI)
{
    this.myUI = myUI;
    createTheGUI();
}

Create the GUI Method

public void createTheGUI()
{
    pm1 = new PopupMenu("First Menu");
    pm2 = new PopupMenu("Second Menu");

    pm1_1 = new MenuItem("First Menu First Choice");
    pm1_2 = new MenuItem("First Menu Second Choice");
    pm2_1 = new MenuItem("Second Menu First Choice");
    pm2_2 = new MenuItem("Second Menu Second Choice");

    pm1_1.addActionListener(this);
    pm1_2.addActionListener(this);
    pm2_1.addActionListener(this);
    pm2_2.addActionListener(this);

    pm2_2.setEnabled(false);

    pm1.add(pm1_1);
    pm1.add(pm1_2);
    pm2.add(pm2_1);
    pm2.add(pm2_2);

    pm1.addActionListener(this);
    pm2.addActionListener(this);

    add(pm1);
    add(pm2);
}

This method will be invoked when a menu item is selected

public void actionPerformed(ActionEvent e)
{
    Object source = e.getSource();
    if(source instanceof MenuItem)
    {
        handleMenuItems(e, (MenuItem)source);
    }
}

public void handleMenuItems(ActionEvent e, MenuItem source)
{
    String selected = source.getLabel();

    System.out.println("String Selected is: " + selected);
}
}   

called once the myUI object is instantiated

void createTheGUI()
{

     b = new Button("Click to see a Popup Menu");
     add(b);
     b.addActionListener(this);
}


public void actionPerformed(ActionEvent e)
    {

    myUI.p1.pm1.show(myUI.p1, 0, 0);
    myUI.p1.pm2.show(myUI.p1, 180, 0);
}
Taylor Hx
  • 2,815
  • 23
  • 36
  • It's better to strip down the code sample to make it easier to read – fejese Jun 29 '14 at 23:11
  • 4
    THe `show` method of `PopupMenu` is not deprecated. Anyhow, I'd recommend you to use Swing ( `JPanel`, `JFrame`... ) instead of AWT (`Panel`, `Frame`...). See http://docs.oracle.com/javase/tutorial/uiswing/ – Marco13 Jun 29 '14 at 23:35
  • You're right, sorry I made a silly mistake in my main method, thanks for the reply – Gary Cassar Jun 30 '14 at 00:09
  • Why AWT rather than Swing? See my answer on [Swing extras over AWT](http://stackoverflow.com/a/6255978/418556) for many good reasons to abandon using AWT components. +1 to the comment of @Marco13. – Andrew Thompson Jun 30 '14 at 10:00
  • For better help sooner, post an [MCVE](http://stackoverflow.com/help/mcve) (Minimal Complete and Verifiable Example). – Andrew Thompson Jun 30 '14 at 10:01

0 Answers0