Opening a dialog box from a menuItem

Hi everyone,

I am creating a GUI and i want to be able to open a dialog box from a menuItem, so that when the user clicks on the menuItem the dialog appears.

I was wondering how i could do this using event handlers. Which ones would i need to use.

The menuItem is calledcustomerEdit and the dialog is callededitCustomer.

Im a complete newbie at this, so any help is greatly appreciated

Thanks in advance

Carl

[467 byte] By [Mr_Carla] at [2007-9-30 23:57:16]
# 1

Hi,

there is the method addActionListener at the MenuItem (s. http://java.sun.com/j2se/1.4.2/docs/api/java/awt/MenuItem.html) class. You have to add this to your menuitem (customerEdit.addActionListener(this)). this is a pointer on the frame class that is the base class of your application. Then you have to add the method actionPerformed to your frame-class. If somebody clicks on the item the actionPerformed is called.

Good luck!

Teasa at 2007-7-7 15:41:17 > top of Java-index,Archived Forums,Java 2 Software Development Kit (J2SE SDK)...
# 2

could you show me where to place it in my code?

this is part of the code for my menu bar

salesMenuBar.setBackground(new java.awt.Color(237, 234, 218));

customerMenu.setBackground(new java.awt.Color(236, 233, 216));

customerMenu.setMnemonic('C');

customerMenu.setText("Customer");

customerEdit.setBackground(new java.awt.Color(255, 255, 255));

customerEdit.setMnemonic('E');

customerEdit.setText("Edit/Remove...");

customerEdit.addActionListener(new java.awt.event.ActionListener() {

public void actionPerformed(java.awt.event.ActionEvent evt) {

customerEdit(evt);

}

});

customerMenu.add(customerEdit);

salesMenuBar.add(customerMenu);

managementMenu.setBackground(new java.awt.Color(236, 233, 216));

managementMenu.setMnemonic('M');

managementMenu.setText("Management");

managementLogin.setBackground(new java.awt.Color(255, 255, 255));

managementLogin.setMnemonic('L');

managementLogin.setText("Login...");

managementMenu.add(managementLogin);

salesMenuBar.add(managementMenu);

helpMenu1.setBackground(new java.awt.Color(236, 233, 216));

helpMenu1.setMnemonic('H');

helpMenu1.setText("Help");

helpContent1.setBackground(new java.awt.Color(255, 255, 255));

helpContent1.setMnemonic('n');

helpContent1.setText("Contents");

helpMenu1.add(helpContent1);

helpAbout.setBackground(new java.awt.Color(255, 255, 255));

helpAbout.setMnemonic('A');

helpAbout.setText("About");

helpMenu1.add(helpAbout);

salesMenuBar.add(helpMenu1);

setJMenuBar(salesMenuBar);

pack();

}

private void customerEdit(java.awt.event.ActionEvent evt) {

// Add your handling code here:

}

Thanks again

Carl

Mr_Carla at 2007-7-7 15:41:17 > top of Java-index,Archived Forums,Java 2 Software Development Kit (J2SE SDK)...
# 3

you did it already, fill only the part of

private void customerEdit(java.awt.event.ActionEvent evt) {

Dialog editDialog = new Dialog(this);

editDialog.show();

}

don't know if you have to write your own dialog...

Teasa at 2007-7-7 15:41:17 > top of Java-index,Archived Forums,Java 2 Software Development Kit (J2SE SDK)...