Win a copy of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js this week in the JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

JMenuItems  RSS feed

 
Joe Lemmer
Ranch Hand
Posts: 171
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi there,

I wonder whether anyone can point me in the right direction...

I need to find out how I can make all my JMenuItem's have a similar sort of action. I want to click on any one of them and get them to all pass their own name as a string and do other similar stuff detailed in a ActionListener class. For more details, please see below.

I'm just mucking about trying to learn the basics of swing and linking up to databases with JDBC. I can enter a name of a new column for my database table into a JTextField of my GUI, press a button and I connect up to the database and add a column to it. After that (part of the same actionPerformed method) a method called getColumnHeadings() is called which I shall show below:



SQLCommand is a class that I've made with a few useful methods to interact with my database. The getColumnNameArrayList() method queries the database and eventually returns an ArrayList<String> with the column headings in. As you can see this ArrayList is then used to create / refresh my JMenuItems in my 'fileMenu' JMenu.

This is all well and good, but when I click on one of the JMenuItems in my menu, how do I let my action listener know where in the ArrayList this MenuItems name string is held? Thats all I need I think, I need to click on the MenuItem and get it to pass its own name string to the ActionListener or something (so that I can set which database table column will be added to.

Thanks for reading all the above waffle! Hope you can help.

Cheers

Joe Lemmer
 
Rob Spoor
Sheriff
Posts: 21048
85
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
1) Each ActionEvent has a source you can retrieve using getSource().
2) Check out how action commands work for both buttons, menu items and ActionEvents
 
Joe Lemmer
Ranch Hand
Posts: 171
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Rob,

Cheers for your reply.

I am (or think I am) OK with connecting buttons up to a class that implements the ActionListener interface. My problem was how to get the name of the MenuItem. I've looked again at the API (arrrrgh!!) and as I understand it an ActionEvent object is created when a component that is hooked up to one listening thingy or another is clicked. This object is then passed to all that ActionListener objects that they are connected to.

Problem is all my MenuItems are created in the for loop of my getColumnHeadings() method, so can I still use a addActionListener method in that for loop?

Also, which object is actually using the getSource() method. Is it the ActionListener object? If so, this isn't an object that I can give a name is it? In that case do I just use getSource(); like that as a statement in the actionPerformed method?

Thanks

Joe
 
Rob Spoor
Sheriff
Posts: 21048
85
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Joe Lemmer:
Problem is all my MenuItems are created in the for loop of my getColumnHeadings() method, so can I still use a addActionListener method in that for loop?

Sure, why not?

Also, which object is actually using the getSource() method. Is it the ActionListener object? If so, this isn't an object that I can give a name is it? In that case do I just use getSource(); like that as a statement in the actionPerformed method?

It's in ActionEvent:

Most of the XXXEvent objects have a source.
 
Joe Lemmer
Ranch Hand
Posts: 171
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for answering Rob. I really appreciate it.

I was just about to send you another exciting installment of "I've still got a problem", but then saw my mistake. WooHoo!

So, it's all working perfectly and I'm definitely happier with this.

Before I reach unbearable levels of smugness though, I wonder if you could just clear a couple of issues up:

Instead of typing



I tried typing



because I figured that I KNEW it was going to be a JMenuItem so I could be more specific. Nope. I'd really like to know why this is the case.

Many thanks

Joe Lemmer

By the way, if I don't acknowledge a reply I'm not being rude, I've just gone to bed. Cheers.
 
Rob Spoor
Sheriff
Posts: 21048
85
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
getSource() always returns an Object reference. It may point to a JMenuItem, but you'll have to cast it first.

Now you're saying that you know that the source is a JMenuItem. Does that mean that the class that implements ActionListener is private to your class?
 
Joe Lemmer
Ranch Hand
Posts: 171
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Rob,

OK thats great thanks. getSource() always returns an object. I was thinking that maybe you had put Object there to just make it flexible for future use etc (polymorphism!?).

I'm not sure what you mean by private class. I have made a class that implements ActionListener and will only MenuItems would register with it.

Thanks for your replies.

;-)
 
Rob Spoor
Sheriff
Posts: 21048
85
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
With private class I mostly mean that your class itself doesn't implement ActionListener:

This will allow your frame to be used as ActionListener for any button, menu item or anything that can have ActionListeners. If that is the case the source may not be a JMenuItem.
 
Joe Lemmer
Ranch Hand
Posts: 171
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks again for your time Rob. You've been a great help.

:-)
 
Maneesh Godbole
Bartender
Posts: 11445
18
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In addition to Rob's pointers, if you extend AbstractAction, you do not need the getSource() at all. All the required parameters can be stored in your CustomAction class.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!