• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Liutauras Vilda
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Scott Selikoff
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
  • Frits Walraven
Bartenders:
  • Stephan van Hulst
  • Carey Brown

menu problem..please help ASAP..

 
Ranch Hand
Posts: 115
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi guys,i made the following program:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.io.IOException;

public class ColorMenu extends QuittableFrame {
public static void main(String[] args) {
new ColorMenu();
}
public static String msgs = "";
private String[] colorNames =
{ "Black", "White",
"Light Gray", "Medium Gray", "Dark Gray" };
private Color[] colorValues =
{ Color.black, Color.white,
Color.lightGray, Color.gray, Color.darkGray };

public ColorMenu() {
super("gaurav");

MenuBar mbar = new MenuBar();
//Menu colorMenu = new Menu("Colors");
//for(int i=0; i<2; i++)
//colorMenu.add(colorNames[i]);
//Menu grayMenu = new Menu("Gray");
//for(int i=2; i<colorNames.length; i++)>
//grayMenu.add(colorNames[i]);
//colorMenu.add(grayMenu);
//bar.add(colorMenu);
setMenuBar(mbar);
Menu io = new Menu("I/O");
MenuItem item1,item2;
io.add(item1 = new MenuItem("Open network applet from server"));
io.add(item2 = new MenuItem("-"));

Menu sub = new Menu("Load");
MenuItem item3,item4,item5,item6;
sub.add(item3 = new MenuItem("Node File"));
sub.add(item4 = new MenuItem("Link File"));
sub.add(item5 = new MenuItem("Incident File"));
sub.add(item6 = new MenuItem("TFRU File"));
io.add(sub);
mbar.add(io);

Menu ana = new Menu("Results");
MenuItem item7;
ana.add(item7 = new MenuItem("Analysis"));
mbar.add(ana);

Menu hel = new Menu("Help");
Menu man = new Menu("User Manual");
MenuItem item8,item9,item10;
man.add(item8 = new MenuItem("Word File"));
man.add(item9 = new MenuItem("Pdf File"));
man.add(item10 = new MenuItem("HTML File"));
hel.add(man);
mbar.add(hel);

MyMenuHandler handler = new MyMenuHandler();

item1.addActionListener(handler);
item2.addActionListener(handler);
item3.addActionListener(handler);
item4.addActionListener(handler);
item5.addActionListener(handler);
item6.addActionListener(handler);
item7.addActionListener(handler);
item8.addActionListener(handler);
item9.addActionListener(handler);
item10.addActionListener(handler);

MyWindowAdapter adapter = new MyWindowAdapter(this);
addWindowListener(adapter);

setBackground(Color.white);
resize(500, 500);
show();
}


public void paint(Graphics g) {
g.setColor(Color.black);
g.drawString(msgs,10,200);

}
}
class MyWindowAdapter extends WindowAdapter {
ColorMenu colorMenu;
public MyWindowAdapter(ColorMenu colorMenu) {
this.colorMenu = colorMenu;
}
public void windowClosing(WindowEvent we) {
colorMenu.setVisible(false);
}
}

class MyMenuHandler implements ActionListener,ItemListener {

ColorMenu colorMenu;
public void MyMenuhandler(ColorMenu colorMenu) {
this.colorMenu = colorMenu;
}

public void actionPerformed(ActionEvent ae) {
String msg = "You selected : ";
System.out.println(msg);
String arg = (String)ae.getActionCommand();
System.out.println(arg);
if(arg.equals("Open network applet from server"))
msg += "Open network applet from server";
else if(arg.equals("Node File"))
msg += "Node File";
else if(arg.equals("Link File"))
msg += "Link File";
else if(arg.equals("Incident File"))
msg += "Incident File";
else if(arg.equals("TFRU File"))
msg += "TFRU File";
else if(arg.equals("Node File"))
msg += "Node File";
else if(arg.equals("Results"))
msg += "Results";
else if(arg.equals("Analysis"))
msg += "Analysis";
else if(arg.equals("help"))
msg += "help";
else if(arg.equals("Word File"))
msg += "Word File";
else if(arg.equals("Pdf File"))
msg += "Pdf File";
else if(arg.equals("HTML File"))
msg += "HTML File";

ColorMenu.msgs = msg;
//System.out.println(ColorMenu.msgs);
colorMenu.repaint(); //line 139
}
public void itemStateChanged(ItemEvent ie) {
colorMenu.repaint();
}
}
and on executing it i get the following exception :
Exception occurred during event dispatching:
java.lang.NullPointerException
at MyMenuHandler.actionPerformed(ColorMenu.java:139)
at java.awt.MenuItem.processActionEvent(Unknown Source)
at java.awt.MenuItem.processEvent(Unknown Source)
at java.awt.MenuComponent.dispatchEventImpl(Unknown Source)
at java.awt.MenuComponent.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)
i camt understand why... ..please help..what i think is the class MyMenuHandler is not able to execute the command colorMenu.repaint(),but i dunno why because in the very next line it is executing the same command without any problem as u can see in the method itemStateChanged(ItemEvent ie)..
please hlp guys..i would really appreciate it..
regards
karan
 
Politics n. Poly "many" + ticks "blood sucking insects". Tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic