Forums Register Login

JMenu help

+Pie Number of slices to send: Send
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.

+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
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.

+Pie Number of slices to send: Send

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?
+Pie Number of slices to send: Send
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?

+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
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?
+Pie Number of slices to send: Send
 

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.
+Pie Number of slices to send: Send
 

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.
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
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?
+Pie Number of slices to send: Send
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?

+Pie Number of slices to send: Send
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?
Hey! Wanna see my flashlight? It looks like this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com


reply
reply
This thread has been viewed 2322 times.
Similar Threads
First GUI program
help on reading textfield on 1 class from another class and using it to create a file
Event problems
Swing GUI Interface Question
Scroll panel problems
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 29, 2024 02:07:29.