File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Swing / AWT / SWT and the fly likes JMenu help Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "JMenu help" Watch "JMenu help" New topic
Author

JMenu help

Manny Ruiz
Ranch Hand

Joined: Dec 01, 2010
Posts: 40
Hello all. I am currently working on creating a program to calculate a mortgage payment plan where the user enters the amount of the loan, and then uses the menu to choose the interest rate and term. For my last project I created a simple adding GUI which really helped me understand what was going on without having to worry about getting the number correct. I will take the same approach with this project. I created a GUI that will perform a simple calculation. The user will enter the first number, and choose the second number from an options menu. I have been able to create the menu, and the items, but i am getting stuck from there. I will continue to research the jmenu, but i would also appreciate any direction as well.

Manny Ruiz
Ranch Hand

Joined: Dec 01, 2010
Posts: 40
I should probably be a bit clearer on the problem i am facing. i am not sure what to do to make the program recognize the options in the menu as numbers. Do I have to use parse to change them to int or is there a way to define them as int when they are created.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18657
    
    8

Have you read the Oracle tutorial "How to Use Menus"? I recommend starting there. In fact I recommend taking the example code from that tutorial, messing around with it, and then modifying it until it's the application you wanted to write.
Manny Ruiz
Ranch Hand

Joined: Dec 01, 2010
Posts: 40
OK I read the tutorials on both the JMenu and the JRadioButtonMenuItem. but it only helped me in creating the menu. I still cant figure out how to make the buttons do anything. I looked at an example where the radio button selection makes a picture appear, but I cant see how to make this work for my needs. Here is my menu code so far. I have another question about action events. What is the difference between using what i used:" item2.addActionListener(listener);" and "item2.addActionListener(this);" I have seen both used, but im not sure when to use what.

Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18657
    
    8


I don't see where you are assigning anything to the variable "listener". Did you just omit that part of your code from the post? If not, what do you expect to happen when you select that menu item?
Manny Ruiz
Ranch Hand

Joined: Dec 01, 2010
Posts: 40
The menu code I posted was added to the original code I had posted earlier. But i think I need to ask some questions that are related more directly to my assignment. I need to create a mortgage calculator that will let the user enter a loan amount and then use the menu items to select the interest rate and term. the text area will be used later to show an amortization table. I created the array for the different loan options, but i cant figure out how to make the calculation method get the array info for the option selected. Right now i used the [0] array item as a place holder. Is it possible to make the calculator get the right array information based on which menu item is selected?

Greg Brannon
Bartender

Joined: Oct 24, 2010
Posts: 561
I assume you'd like to use the JRadioButtonMenuItem, to set the interest rate and term. You could use the JRadioButtonMenuItem selection to set an arrayIndex for both the interest[] and term[] arrays.

As Paul and perhaps others have pointed out, you need to assign an ActionListener to your JRadioButtonMenuItems, ritem, ritem2, and ritem3, and then take the appropriate action when the ActionListener executes. I suggest that the desired action will be to set the arrayIndex as I mentioned above. Or, you could set the interest rate and term directly, but either way is fine.

There are good examples on the web of how it should look.


Always learning Java, currently using Eclipse on Fedora.
Linux user#: 501795
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18657
    
    8

It's possible that Manny actually doesn't want the user to click the menu item. It sort of sounds like he wants to see which radio-button menu item was the selected one at the time some other button was clicked.
Greg Brannon
Bartender

Joined: Oct 24, 2010
Posts: 561
Paul, thank you for your careful reading of Manny's postings and insghtful interpretation.

Manny, I don't think you can do what Paul has suggested DIRECTLY. You can look at the methods for the JRadioButtonMenuItem in the API and see if any look applicable, but the few available don't seem to cover that. But you can do it INDIRECTLY using the indexPointer idea I suggested earlier.

As you mentioned, you couldn't decide how to vary the index of your interest[] array, so you set it on the [0] item so that you could continue working your program. Instead of setting it permananently to [0] in your code, you could set it to a variable, e.g. [indexPointer]. indexPointer would initially be set to a (legal) default value, and the corresponding JRadioButtonMenuItem would be set selected when created. Then, when another JRadioButtonMenuItem is selected, the ActionListener would fire, and the listener logic would change the arrayIndex to the index corresponding to the radio button chosen by the user. You wouldn't be reading which button was selected directly, but you'd know which one was selected by the value of indexPointer.

Make sense?
Darryl Burke
Bartender

Joined: May 03, 2008
Posts: 4642
    
    5

Is it possible to make the calculator get the right array information based on which menu item is selected?

Assuming the RadioButtonMenuItems are in an array which is an instance field, iterate over that array in a for loop (not the enhanced for loop) and test for isSelected() -- when it's true, use the loop counter as the index to the interest (or term) array.


luck, db
There are no new questions, but there may be new answers.
Greg Brannon
Bartender

Joined: Oct 24, 2010
Posts: 561
Assuming the RadioButtonMenuItems are in an array which is an instance field . . .


He doesn't have the menu items set up that way now, but he certainly could. Cool idea. Thanks for sharing.
Manny Ruiz
Ranch Hand

Joined: Dec 01, 2010
Posts: 40
Thanks for all the suggestions so far. i havent had any time the ladt couple of days to work on this, but I gotta get crackin. I will add the listener to the radio menu items. From there i have a few ideas I will be trowing around.
Manny Ruiz
Ranch Hand

Joined: Dec 01, 2010
Posts: 40
I had such a hard time with arrays in my last class that I am not sure what to do with the indexpointer suggestion, although it sounds like a good one. So i was wondering would it be possible to make this work with if statments? for example



I know the best way to find out if this would work to is to plug it into my code, but while im doing that i hope someone can tell me if it is even possible?
Manny Ruiz
Ranch Hand

Joined: Dec 01, 2010
Posts: 40
ok i was messing around with the if statement, and it got to work...kind of. The payment is calculated correctly, but the only problem is that the payment is automatically calculated when the user clicks on the options. . Can i make it so the payment is not calculated until the user clicks the payment button?

Greg Brannon
Bartender

Joined: Oct 24, 2010
Posts: 561
Anytime you find yourself writing (or copying and pasting) the same code over and over again with minor changes, you should be thinking, "How do I just do that once?" or "How do I code that so that I can write it once but execute it multiple times?"

Here's how you might "just do it once" in your actionPerformed() method:



I haven't checked your code to see how it's working. Is it working like you want it to?
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: JMenu help