*
The moose likes Swing / AWT / SWT and the fly likes Create an Action Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Create an Action" Watch "Create an Action" New topic
Author

Create an Action

Jim Size
Greenhorn

Joined: Aug 10, 2010
Posts: 29
i got a problem, i have created my image processing application.
the user can acquire information about some colors of the image(Red-white-etc).
i give him the option to get the information for each color with for JMenuItems (red - white etc)

whiteEl3 is a JMenuItem.

so when the user click it he gets some information (i cut some code because i got no problem with the code).

the thing i don't know how to do is the following:

lets say that the user wants to get all the information, that he can from my JMenuItems (i got 20) but i dont want to make him click all that items.

i can make one more (lets name it "All") that when he clicks it, he gets all that info.
i thought that its not good to make a JMenuItem that is a copy of a mass code of the rest 20 items.
code for whiteEl3(example):

TIP: don't bother about processflag container whiteflag find etc.



so i believe that when the user clicks "All" in my code it should create some actions for each JMenuItems
like whiteEl3 action and then the program will do the same stuff like he has pressed whiteEl3 etc.

i think that if its possible the user will do only one click on JMenuItem "All" but in the code automatically will be performed some other actions for making the program "believe" that he clicked all the JMenuItems.

i have never done this before and its the final thing i need to give my application.
it would be helpful if you can link me some sites or give me some suggestions for that.
thanks

Ralph Cook
Ranch Hand

Joined: May 29, 2005
Posts: 479
Please consider the following statements:

The 'all' button causes every other related button press to execute.

The 'all' button returns the collected information the user would have gotten by pressing every other related button.

I believe you really want the second one, not the first. It is possible to have the program cause a "button press", but then it does the same things that would have been done if the user had pressed that button -- display popups, change focus, whatever. So in terms of your UI design, I think this is an important distinction.

The second statement seems to me to be a perfect place for good procedural programming. If your whiteEl3 button is pressed, then your action method calls another method (call it 'george') that returns the information indicated. If your 'all' button is pressed, then your action can call george, charley, nancy, mary, and whoever else it needs to to get all its information.

The named methods then contain the code to obtain the desired information in one place, and you do not need that code in any other places. They are also called without knowledge of what button was pressed (or that ANY button was pressed); when they are invoked, they return the indicated information and are done. Obtaining and returning this information is not necessarily related to the UI.

It is also possible that george takes a parameter to tell it what to return. If it were my code, making some assumptions about how things are organized, I would consider having george return one set of information based on one parameter, and for the 'all' button I would call george mutliple times with each parameter I wanted information about. I would NOT pass any representation of the button into george and have him figure it out from there, because that mixes my UI information (what buttons there are) with my non-UI programming (returning information about my image, or whatever).
Jim Size
Greenhorn

Joined: Aug 10, 2010
Posts: 29
thank you for your answer,

i got a JTextArea where all my JMenuItems write information from executed methods (those methods are executed if needed inside the "(if e.getSource==menuitemX {.. blah blah call method... window.append("found...");...} ).

i understand the advantage of your second suggestion but i can't understand how i am going to do it.

as i said i have never done it before and i don't know what should i look for.

can you please give me informations (sites, links, books, keywords, etc) for both statements ?

thank you again!

//edit// because i don't know this thing, i thought that a solution for faster executing some menuitem (pressed) to add keybinding (ctrl+1, etc) but i want to create a "All" menuItem so it can execute the menuitems i want.
Ralph Cook
Ranch Hand

Joined: May 29, 2005
Posts: 479
I do not know enough about what you want to be complete, but I've coded two things that illustrate what I'm talking about in the code below:



The idea is to put the logic about doing the actual work the user has requested in methods (and perhaps classes) that are separate from ones that do anything with the user interface. So if a routine figures out that a "white" option has been chosen (a UI thing), it invokes a method that does the work involved; the invoked method does not know or care that any particular menu item was chosen, just that it is supposed to go ahead and do a specific job. And when an option is chosen that is the equivalent of choosing multiple menu options, the resulting UI code can invoke all the work-doing methods in turn, so all the work gets done without copying that work code into a UI routine.

ANY time you find that copy-and-paste is tempting, look for a way to reorganize your code so that it isn't necessary. If the problem you are trying to solve has copies, it might just reflect the nature of the problem, but that is quite rare, actually.

rc
Jim Size
Greenhorn

Joined: Aug 10, 2010
Posts: 29
i know ralph that i need to avoid the copy and paste stuff but i think that you didn't understand my problem.

i need to write in the (if (e.getSource==allmenuitem){ "press" whitemenuitem; "press" redmenuitem; etc...})

already all my methods are separated from the UI stuff.
i know that when some same lines of code must be executed multiple times i should find a way to add em into a method so it will be less code.
but in my case i got 1.8k lines of code and i broke all my code into parts so it will be easier to access whats needed by pressing a menuitem and running the methods.

but each menuitem does a unique job like showing into the textarea for example all the same coloured areas.

when each menuitem is pressed lets say that it needs to execute 5 lines(run 3 methods and write into the textArea the stuff and update it) of code that are unique for each menuitem and they are inside the (if e.getSource==menuitemX...)
and i have 5 menu items

i need somehow tell my program to press that jmenuitem automatically through my code (many menuitems) because i don't wont to to add 5x5=25 lines of code into that "if" (if e.etSource==all ... ).
thanks again for answering so fast but how can i write in my program to press automatically the jmemuitem like the user has pressed it ? ? ??

Ralph Cook
Ranch Hand

Joined: May 29, 2005
Posts: 479
You're right. I don't understand.

Call the doClick() method on the JMenuItem.

rc
Jim Size
Greenhorn

Joined: Aug 10, 2010
Posts: 29
thanks mate for the help
the doClick() works fine! its exactly what i wanted
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Create an Action
 
Similar Threads
simple web browser in java
JMenuItem enable
keep the JMenu from closing the pop up of JMenuItems
JPopupMenu JMenuItem and JMenuItem : Listen to each other?
<ArrayList> help