You Are Here:

Community: Developer Discussion Boards

#1 Old Event handling in JMenu in JPanel - 2003-07-23, 22:32

Join Date: Jul 2003
Posts: 2
Location: Finland
Send a message via AIM to mpyhajar Send a message via Yahoo to mpyhajar
mpyhajar
Offline
Registered User
Hi,

I'm writing a small register program GUI. I have a problem with ActionListener in JMenu. I wrote a JPanel which creates a JMenu and that works just fine. The problem is that id doesn't give any event signals. I have another class which impelemets ActionListener and it works fine with JButton (another panel for buttons), but not with JMenu.

Below is the JPanel for JMenu and Application which implements the ActionListener. Do you see anything wrong? I really can't imagine why I cannot get the action signals. JPanel is defined in JFrame as it should be. Have you got any ideas?

Regards,
Marko

class MenuPanel extends JPanel {

protected JMenuBar menuBar;
protected JMenu menu;
protected JMenuItem loadRegister;
protected JMenuItem saveRegister;
protected JMenuItem information;
protected JMenuItem quit;

public MenuPanel(Application application) {
menuBar = new JMenuBar();

menu = new JMenu("Tiedosto");

loadRegister = new JMenuItem("Lataa");
loadRegister.addActionListener(application);

saveRegister = new JMenuItem("Talleta");
saveRegister.addActionListener(application);

information = new JMenuItem("Tietoja");
information.addActionListener(application);

quit = new JMenuItem("Lopeta"); quit.addActionListener(application);

menu.add(loadRegister);
menu.add(saveRegister);
menu.addSeparator();
menu.add(information);
menu.add(quit);

menuBar.add(menu);

}
}

class Application implements ActionListener {

// Define CardPanel
private CardPanel cardPanel;

// Define linked list for cards
private CardList cardList = null;

// Constructor. Initializes cardPanel and cardList
public Application(CardPanel cardPanelRefrence) {
cardPanel = cardPanelRefrence;
cardList = new CardList();
}

public void actionPerformed(ActionEvent action) {
String actionPerformed = action.getActionCommand();

if (actionPerformed.equals("Lataa")) {
load();
}
if (actionPerformed.equals("Talleta")) {
save();
}
if (actionPerformed.equals("Tietoja")) {
info();
}
if (actionPerformed.equals("Lopeta")) {
System.exit(0);
}
}

// Continues with the methods...
Reply With Quote

#2 Old Solution. - 2003-07-24, 01:08

Join Date: Jul 2003
Posts: 2
Location: Finland
Send a message via AIM to mpyhajar Send a message via Yahoo to mpyhajar
mpyhajar
Offline
Registered User
Well, I found the answer. Heh, quite easy one but it took a while before I found it..

Just if you ever have similar problem. The problem was that in my JFrame I didn't create an object of class (Application) which implement s the ActionListener before using it as a reference in creating JPanel object. Quite confusing explanation, but here the code. This code was in my JFrame which was not included in this question.

private Application application;
private MenuPanel menuPanel;

application = new Application(cardPanel, rightInfoPanel);
menuPanel = new MenuPanel(application);

These two were introduced other way round, so that variable "application" was null. What a problem in fact.. :)

- Marko
Reply With Quote

#3 Old 2003-07-24, 09:58

Join Date: Mar 2003
Posts: 2,280
Location: Israel
shmoove
Offline
Forum Nokia Champion
How did you manage to reach this forum with that question is beyond me...
Reply With Quote
Reply « Previous Thread | Next Thread »
Display Modes
Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules

You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Forum Jump

Rate This

 
Bookmark this page: DeliciousDiggFacebookGoogleYahooStumbleUponRedditDiigoTechnocratiTwitter  Share this page Share this page Print this Page Print this page Invite a friend Invite a friend
京ICP备05048969号    Email Newsletters Press Terms & Conditions Privacy Policy Sitemap Contact Us © 2009 Nokia 
RDF Facets: qdcZidentifierQSxhttpE3aE2fE2fdiscussionE2eforumE2enokiaE2ecomE2fforumE2fshowthreadE2ephpE3ftE3d134434X qdcZtypeQUqfnZE45E78cludedFromGeneralE4cistingsQ qdcZtypeQUqfntypeZCommunityContentQ qdcZtypeQUqfntypeZE44iscussionQ qdcZtypeQUqfntypeZE44iscussionContentQ qdcZtypeQUqfntypeZE52esourceQ qdcZtypeQUqfntypeZWebpageQ qdcZtypeQUqmarsZManagedE52esourceQ qdcZtypeQUqwebZInformationE52esourceQ qdcZtypeQUqwebZPageQ qdcZtypeQUqwebZE52esourceQ qdcZtypeQUqrdfsZE52esourceQ qfnZtopicQUqfnTopicZentertainmentQ qfnZtopicQUqfnTopicZj2meQ qfnZtopicQUqfnTopicZjavaQ qfnZtopicQUqfnTopicZmediaQ qfnZtypeQUqfntypeZCommunityContentQ qfnZtypeQUqfntypeZE44iscussionQ qfnZtypeQUqfntypeZE44iscussionContentQ qfnZtypeQUqfntypeZE52esourceQ qfnZtypeQUqfntypeZWebpageQ qmarsZlanguageQUxhttpE3aE2fE2fswE2enokiaE2ecomE2flanguageE2d1E2fenX qrdfZtypeQUqfnZE45E78cludedFromGeneralE4cistingsQ qrdfZtypeQUqfntypeZCommunityContentQ qrdfZtypeQUqfntypeZE44iscussionQ qrdfZtypeQUqfntypeZE44iscussionContentQ qrdfZtypeQUqfntypeZE52esourceQ qrdfZtypeQUqfntypeZWebpageQ qrdfZtypeQUqmarsZManagedE52esourceQ qrdfZtypeQUqwebZInformationE52esourceQ qrdfZtypeQUqwebZPageQ qrdfZtypeQUqwebZE52esourceQ qrdfZtypeQUqrdfsZE52esourceQ