Anyone got info on JMenuBar Listening?

Does anyone know of a way to listen for mouse input on the JMenuBar?

Ill be honest, im in a bit over my head... But basically I'm overwriting the JMenuBar paintComponent method, so i can put a little [x] button at the top right corner of screen. But now i need to be able to register when someone clicks on it....

It keeps throwin a NullPointerException if i do:My_Menu_Bar.addMouseListener(this);

and if i just doaddMouseListener(this), it will run the program, but it doesnt register the JMenuBar as a point from event.getX().

Any ideas?

[626 byte] By [SomeDudea] at [2007-11-26 18:47:46]
# 1

Also, I can post some code if you want... And just so everyone knows, I have done thorough research, and many attempts on editing my code using the Java API Doc as a reference.... There is zero information on this.

Although I have found out one thing... It seems there are 2 ways to write a Java Application,

1.) where the class extends JFrame

and

2.) where the class extends JPanel

If I wrote the app using #1, then I could do this:My_Menu_Bar.addMouseListener(new MouseAdapter() {

public void mouseClicked(MouseEvent evt) {

Menu_Bar_Functions(evt);

}

});

...

public void Menu_Bar_Functions(evt) {

System.out.println("Hey man, you clicked on the menu bar that's a pain in the arse when you write an Application using #2");

}

But, #2 doesnt let you do this. Returns yet again... Null Pointer

I am writing mine using #2 because the code is cleaner and easier to use in my opinion. It's just that the java parser is outputting a extremely vague error message, and im completely stuck.

Any help is greatly appreciated

SomeDudea at 2007-7-9 6:21:43 > top of Java-index,Java Essentials,New To Java...
# 2

>It seems there are 2 ways to write a Java Application,

>1.) where the class extends JFrame

>and

>2.) where the class extends JPanel

Sez who? I'm particular about this - I only derive from a GUI class if I need to override a method in that class, and I've never needed to override any methods in JFrame or JPanel, so I've never derived from either.

Anyhowdy, here's a tip about JMenuBar -- it's just another Container. Nothing special. Puts its pants on one leg at a time. Its layout manager seems to behave a bit like BoxLayout, so I wondered if a little dab of glue would do the trick:

import java.awt.*;

import java.net.*;

import javax.swing.*;

public class Example implements Runnable {

public void run() {

JMenuBar mb = new JMenuBar();

JMenu fileMenu = new JMenu("File");

fileMenu.add("New");

fileMenu.add("Open");

mb.add(fileMenu);

mb.add(new JMenu("Edit"));

mb.add(Box.createGlue());

try {

URL url = new URL("http://mud.mm-a7.yimg.com/image/3882365987");

JButton close = new JButton(new ImageIcon(url));

close.setMargin(new Insets(2,2,2,4));

close.setContentAreaFilled(false);

close.setBorderPainted(false);

mb.add(close);

} catch (MalformedURLException e) {

e.printStackTrace();

}

JFrame f = new JFrame("Example");

f.setJMenuBar(mb);

f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

f.getContentPane().add(new JScrollPane(new JTable(20,10)));

f.pack();

f.setLocationRelativeTo(null);

f.setVisible(true);

}

public static void main(String[]args) {

SwingUtilities.invokeLater(new Example());

}

}

DrLaszloJamfa at 2007-7-9 6:21:43 > top of Java-index,Java Essentials,New To Java...
# 3
Awesome thanks a ton bro, looks like I should rewrite my application from scratch ;)
SomeDudea at 2007-7-9 6:21:43 > top of Java-index,Java Essentials,New To Java...
# 4

Well the major problem here, is that I started coding my application... straight up JCreator. Then fellows of mine recommended JStudio 8, and it generates all of its code. So I was working from that, and it's better that I just write it from scratch with the above method.

Thanks again bro :)

SomeDudea at 2007-7-9 6:21:43 > top of Java-index,Java Essentials,New To Java...
# 5

Hey, im still having the same problem when i do this:...

public void actionPerformed(ActionEvent e) {

String cmd = e.getActionCommand();

if (cmd.equals("Exit")) quit();

}

...

protected void quit() {

frame.setVisible(false); // These 2 lines...

frame.dispose(); // Are producing the error.

System.exit(0);

}

It doesn't seem to like it when I call functions from the initial frame(which i made accessable to the entire class).

There must be a specific way in which I have to call these methods im assuming?

Right now all i get is:

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException

SomeDudea at 2007-7-9 6:21:43 > top of Java-index,Java Essentials,New To Java...
# 6

> Right now all i get is:

> Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException

is your program structure like this

public class Something

{

JFrame frame;

public Something()

{

JFrame frame = new JFrame();

...

}

actionPerformed()

quit()

}

if it is, you have 2 separate 'JFrame frame' - one is local to the constructor,

the other one is referenced from quit(), and is null

to fix, change

JFrame frame = new JFrame();

to

frame = new JFrame();

Michael_Dunna at 2007-7-9 6:21:43 > top of Java-index,Java Essentials,New To Java...
# 7
Fixed it. :)Message was edited by: SomeDude
SomeDudea at 2007-7-9 6:21:43 > top of Java-index,Java Essentials,New To Java...
# 8

> Well the major problem here, is that I started coding my application... straight

> up JCreator. Then fellows of mine recommended JStudio 8, and it generates all of its code.

I'm just a Swing dabbler, but ask the experienced Swing developers here what they think of the code that gets generated when building your GUI with a drag'n'drop tool...

DrLaszloJamfa at 2007-7-9 6:21:43 > top of Java-index,Java Essentials,New To Java...