JMenuItem ?
Hi
In mu JMenuItem there is an object named exit and button named exit...
JMenuItem exit =new JMenuItem("Exit");
i am trying to put an event as usual in exit..
her is my main function
publicstaticvoid main(String args[])
{
Pad pad =new Pad();
}
//event to exit
exitlistener ex =new exitlistener();
exit.addActionListener(ex);
//class exitlistener
class exitlistenerimplements ActionListener
{
publicvoid actionPerformed(ActionEvent evt)
{
Object obj = evt.getSource();
if(obj == exit)
{
System.exit(0);
}
}
}
Its a very long code to post here..
I think this code helps you to understand wat i mean...
error :
Pad.java:155: <identifier> expected
exit.addActionListener(ex);
^
Pad.java:155: <identifier> expected
exit.addActionListener(ex);
^
2 errors
would anybody tell me how can i overcome this error...
[1701 byte] By [
Reona] at [2007-11-27 2:12:05]

Uh, somehow I think you do have a scoping issue, but it's hard to tell from your stubs.
You could try the following instead:
JMenuItem exit = new JMenuItem(new AbstractAction("Exit")
{
public void actionPerformed( final ActionEvent e )
{
System.exit(0);
}
});
In Java, statements (like exit.addActionListener(ex);) are always placed
inside methods, constructors or initializer blocks:
public YourClass(your parameters) {
exit.addActionListener(ex);
//etc...
}
You can't just drop them into the class body the way you did.
Now it got three errors
Pad.java:61: ')' expected
JMenuItem exit = new JMenuItem(new AbstractAction("Exit"); //forum
^
Pad.java:155: <identifier> expected
exit.addActionListener(ex);
^
Pad.java:155: <identifier> expected
exit.addActionListener(ex);
^
3 errors
Reona at 2007-7-12 2:06:01 >

> Now it got three errors
>
> Pad.java:61: ')' expected
> JMenuItem exit = new JMenuItem(new
> AbstractAction("Exit"); //forum
Did you read the error? It tells you what the problem is. It expects an ')' on that line. Do you see where there's a missing ')'?
> Now it got three errors
>
> Pad.java:61: ')' expected
> JMenuItem exit = new JMenuItem(new
> AbstractAction("Exit"); //forum
>
>
> Pad.java:155: <identifier> expected
> exit.addActionListener(ex);
>^
> ier> expected
> exit.addActionListener(ex);
> ^
Err, have you ever done any coding before? Obviously, my code was meant instead of that ActionListener stuff ...
Sorry for that ')' ..itsmy mistake..but first time onwards i am carrying this errorPad.java:169: <identifier> expectedexit.addActionListener(ex);^Pad.java:169: <identifier> expectedexit.addActionListener(ex);
Reona at 2007-7-12 2:06:01 >

Is that line still outside of a method or constructor? If so, re-read reply 2.
Here is my complete code
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Pad
{
static JFrame frame;
static JMenuBar menuBar;
static JPanel panel;
static JOptionPane optionpane;
//Constructor of class Pad
public Pad()
{
frame = new JFrame();
frame.setTitle("Pad");
//menubar
menuBar = new JMenuBar();
//menu
JMenu file = new JMenu("File");
JMenuItem exit = new JMenuItem("Exit");
//event to exit
exit.addActionListener(ex);
file.add(exit);
//
frame.getContentPane().add(panel);
frame.setJMenuBar(menuBar);
frame.setDefaultCloseOperation(3);
frame.pack();
frame.setVisible(true);
frame.setSize(1024,734);
}
//main
public static void main(String args[])
{
Pad pad = new Pad();
}
exitlistener ex = new exitlistener();
//class exitlistener
class exitlistener implements ActionListener
{
public void actionPerformed( final ActionEvent evt )
{
Object obj = evt.getSource(); //here comes an error
if(obj == exit)
{
System.exit(0);
}
}
}
}
Reona at 2007-7-12 2:06:01 >

exit is defined as a local variable in Pad's ctor, you cannot reference it in your ActionListener implementation. Please read up on variable scope.
exitlistener ex = new exitlistener();That line is still outside of any method or constructor. You can't do that. You can't do that. It has to be inside of a method, constructor or static initializer.
exitlistener ex = new exitlistener();
> That line is still outside of any method or constructor. You can't do that. You can't do
> that. It has to be inside of a method, constructor or static initializer.
Wrong. Just. Plain. Wrong.
Declares package-private field ex of type exitlistener. Initialization happens whenever a ctor of Pad is called.
static JFrame frame;static JMenuBar menuBar;static JPanel panel;static JOptionPane optionpane;Code smell: static
If you dont mind , please talk like beginners can understand....
Reona at 2007-7-12 2:06:01 >

> exitlistener ex = new exitlistener();
> > That line is still outside of any method or
> constructor. You can't do that. You can't do
> > that. It has to be inside of a method,
> constructor or static initializer.
>
> Wrong. Just. Plain. Wrong.
> Declares package-private field ex of type
> exitlistener. Initialization happens whenever a ctor
> of Pad is called.
Oh ****, you're totally right. The lowercase class names and his previous code teamed up to confuse me. I guess that's what I get for being a ****. :-)
using static gui components will not work the way you expect them to work, so get rid of the static keyword when declaring gui components
> using static gui components will not work the way you> expect them to work, so get rid of the static keyword> when declaring gui componentsWhat he said :-)
Yes , i throw away that static keyword from main function but now error inif(obj == exit)
Reona at 2007-7-21 20:21:05 >

exit only exists in the scope of the constructor. Move the declaration of exit to the class level and you can access it from any method in the class.
I have one more doubt .. i tried this thing as same as in a JButton..it works there correctly...Is it sure shall i follow the same way in JMenuItem....
Reona at 2007-7-21 20:21:06 >

> I have one more doubt .. i tried this thing as same
> as in a JButton..
> it works there correctly...
> Is it sure shall i follow the same way in
> JMenuItem....
Are you asking if you can exit the program the same way when the menu item is clicked as when the button is clicked? If so, then yes, add the actionlistener to both components, and handle either case in the actionPerformed method.
But button works for me as in same pattern
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class exit1 extends JApplet
{
JPanel panel;
JButton exit;
public void init()
{
panel = new JPanel();
getContentPane().add(panel);
exit = new JButton("Exit");
panel.add(exit);
//exit button
exitlistener exobj = new exitlistener();
exit.addActionListener(exobj);
}
class exitlistener implements ActionListener
{
public void actionPerformed(ActionEvent evtexit)
{
Object objexit = evtexit.getSource();
if(objexit == exit)
{
System.exit(1);
}}}}
/*
<applet code = "exit1.class" width = 500 height = 300>
</applet>
*/
Reona at 2007-7-21 20:21:06 >

Did anybody please tell me how can i complete that event in JMenuItem.....
Reona at 2007-7-21 20:21:06 >

Add the ActionListener to the menu item and to the button. In the actionPerformed method, is the menu item OR the button were the source of the event, close the window. You can use the same instance of exitlistener for both components.
Did u mean like this..JMenuItem exit = new JMenuItem("Exit") implements ActionListener;
Reona at 2007-7-21 20:21:06 >

No.
Rewrite your exitlistener to not check the source object, i.e. remove that stupidObject obj = evt.getSource();
if(obj == exit)
stuff. Then create an instance of your exitlistener, i.e.exitlistener el = new exitlistener();
then add this instance to both, the button and the menu item, i.e.JButton jb = new JButton("Exit");
jb.addActionListener(el);
JMenuItem jmi = new JMenuItem("Exit");
jmi.addActionListener(el);
And while you're at it, rename your exitlistener toExitListener
By convention, you use capitalized camelcased names for classes in Java.
What i mean in that above Jbutton code is that i can handle events in buttons but not in JMenuItem...
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Pad
{
static JFrame frame;
static JMenuBar menuBar;
static JPanel panel;
//main
public static void main(String args[])
{
frame = new JFrame();
frame.setTitle("Pad");
//menubar
menuBar = new JMenuBar();
//menu
JMenu file = new JMenu("File");
menuBar.add(file);
JMenuItem exit = new JMenuItem("Exit");
file.add(exit);
panel = new JPanel();
//
frame.getContentPane().add(panel);
frame.setJMenuBar(menuBar);
frame.setDefaultCloseOperation(3);
frame.pack();
frame.setVisible(true);
frame.setSize(1024,734);
//exit button
ExitListener exobj = new ExitListener();
exit.addActionListener(exobj);
}
static class ExitListener implements ActionListener
{
public void actionPerformed(ActionEvent evtexit)
{
Object objexit = evtexit.getSource();
if(objexit == exit) //here is the problem show but no problem in JButton
{
System.exit(1);
}
}
}
}
Reona at 2007-7-21 20:21:06 >

I'm having a hard time following what you're asking. You keep talking about a Jbutton, but you've yet to show any code that even uses a JButton, let alone adds listeners to it. Have you actually tried implementing the suggestions you've been given? Do you have any code that uses a JMenuItem and a JButton at the same time? Thomas gave you the code to make this work in reply 26, have you tried it out yet?
Sorry for wasting your time..
But what actually i mean is that see 22nd post i mentioned there about how JButton works and handle events....
I put that same idea in JMenuItem as figured in 27th post ..
But wat actually you people think that i am putting JButton and JMenuitem in same code...Thats not wat i mean ..
I just figured JButton and events idea in JMenuItem .....
Thanx for listening me...
Reona at 2007-7-21 20:21:06 >

> Sorry for wasting your time..
> But what actually i mean is that see 22nd post i
> mentioned there about how JButton works and handle
> events....
> I put that same idea in JMenuItem as figured in 27th
> post ..
> But wat actually you people think that i am putting
> JButton and JMenuitem in same code...Thats not wat i
> mean ..
> I just figured JButton and events idea in JMenuItem
> .....
>
> Thanx for listening me...
Ok, I think I'm getting it now. You want to have a JMenuItem close the program, and you're trying to adapt some code you already have that uses a JButton for that same purpose. Your code in reply 27 looks like it should work, except you don't need to check which component triggered the event. Take these lines out:
Object objexit = evtexit.getSource();
if(objexit == exit) //here is the problem show but no problem in JButton
You only add the ExitListener to the components that you want to shut down the program, so there's no need to check which component it came from, just call System.exit().
I tried and tried and tried and atlast get mad....Now it works....Thanx Thanx Thanx hunter9000....But how did u noticed that there the problem is ....Anyway You have did a great task..Thanx for everyone who helps me....
Reona at 2007-7-21 20:21:11 >
