Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / GUI / November 2003

Tip: Looking for answers? Try searching our database.

Application Design

Thread view: 
Rui Pacheco - 04 Nov 2003 09:55 GMT
Hi everyone

I just started working with Swing so please bear with me on these
somewhat dumb questions.
I have an JMenuBar with a JMenu wich in turn has a JMenuItem.
I also have an JToolbar with a JButton.
Both the JButton and the JMenuItem do the same thing: they create an
extra node on a JTree.
Now, the JToolbar and the JMenuBar are created using different
objects. Since they both do the same thing, how can I make them share
the same code? My idea here is trying to avoid writing the same code
twice, after all both components do the same thing.
bm - 04 Nov 2003 10:48 GMT
Would it work if both action listeners of JMenuItem and JButton
called the same function to add an extra node to JTree? Because
I don't think JMenuItem and JButton can share the same action
listener. Right?

> Hi everyone
>
[quoted text clipped - 8 lines]
> the same code? My idea here is trying to avoid writing the same code
> twice, after all both components do the same thing.
Antti S. Brax - 04 Nov 2003 10:55 GMT
behzad_mortazavi@hotmail.com wrote in comp.lang.java.gui:
> I don't think JMenuItem and JButton can share the same action
> listener. Right?

    Why do you think so? Have you tried?

    Hint: a JMenuItem and a JButton can share an action listener.

Signature

Antti S. Brax - asb(at)iki.fi   Rullalautailu pitää lapset poissa ladulta
  http://www.iki.fi/asb/          http://www.cs.helsinki.fi/u/abrax/hlb/

bm - 04 Nov 2003 11:40 GMT
You see, a professor in the education department told me
the purpose of discussion is to raise questions and show
alternatives to encourage thinking. She called it collaborative
learning. You see exploring the alternatives yourself is the key.
So I thought I should experiment. What do you think?
Did I do justice?

> behzad_mortazavi@hotmail.com wrote in comp.lang.java.gui:
> > I don't think JMenuItem and JButton can share the same action
[quoted text clipped - 7 lines]
>  Antti S. Brax - asb(at)iki.fi   Rullalautailu pit?? lapset poissa ladulta
>    http://www.iki.fi/asb/          http://www.cs.helsinki.fi/u/abrax/hlb/
Antti S. Brax - 04 Nov 2003 12:42 GMT
behzad_mortazavi@hotmail.com wrote in comp.lang.java.gui:
> You see, a professor in the education department told me
> the purpose of discussion is to raise questions and show
> alternatives to encourage thinking. She called it collaborative
> learning. You see exploring the alternatives yourself is the key.
> So I thought I should experiment. What do you think?
> Did I do justice?

    You failed. :-) Your article did not contain a question. It
    contained an assumption that was based on false knowledge.

    Also stop top posting. Put your own text under the quoted
    text like this. And remove irrelevant stuff.

>> behzad_mortazavi@hotmail.com wrote in comp.lang.java.gui:
>> > I don't think JMenuItem and JButton can share the same action
>> > listener. Right?

Signature

Antti S. Brax - asb(at)iki.fi   Rullalautailu pitää lapset poissa ladulta
  http://www.iki.fi/asb/          http://www.cs.helsinki.fi/u/abrax/hlb/

bm - 04 Nov 2003 13:04 GMT
> behzad_mortazavi@hotmail.com wrote in comp.lang.java.gui:
> > You see, a professor in the education department told me
[quoted text clipped - 6 lines]
> You failed. :-) Your article did not contain a question. It
> contained an assumption that was based on false knowledge.

Why would you think that? I tried to encourage the person to verify
the truth of the matter, by taking the opposite side. You see once
you give the correct response then there is no reason to think anymore.
You don't even provide the thinking process in addition to the correct
solution. Not that the user would pay attention anyways.
Who are you kidding, giving answers actually helps you most to solidify
your knowledge of the matter. It just makes the confused more lazzy
if anything. That's why some people don't even answer if you don't post
some code and show that you have put some effort into it.
Perhaps I took an extreme measure. What would you have done besides
giving the answer right away? Put yourself in a teacher's shoe.

> Also stop top posting. Put your own text under the quoted
> text like this. And remove irrelevant stuff.
[quoted text clipped - 6 lines]
>  Antti S. Brax - asb(at)iki.fi   Rullalautailu pit?? lapset poissa ladulta
>    http://www.iki.fi/asb/          http://www.cs.helsinki.fi/u/abrax/hlb/
Andrew Thompson - 04 Nov 2003 10:58 GMT
> I just started working with Swing ...
...
> Now, the JToolbar and the JMenuBar are created using different
> objects. Since they both do the same thing, how can I make them share
> the same code?

You need an AbstractAction, or rather, an implementation
of it.  I use them for both menus and buttons in the PhySci
software project.

The source is in the .jar file, you can find it at..
http://www.physci.org/PhySci.jar

The class that extends AbstractAction is..
physci.toolbox.ui.UserAction

HTH

--
Andrew Thompson
http://www.AThompson.info/
http://www.PhySci.org/
http://www.1point1C.org/
Thomas Fritsch - 04 Nov 2003 11:02 GMT
>Hi everyone
>
[quoted text clipped - 9 lines]
>twice, after all both components do the same thing.
>  

Hi Rui,

Create an Action (preferrably a subclass of AbstractAction).
   Action yourAction = new AbstractAction() {
        public void actionPerformed(ActionEvent e)  { /*do something*/ }
    }
Customize the Action with several properties
   yourAction.putValue(Action.NAME, ....);
   yourAction.putValue(Action.SMALL_ICON, ...);
   yourAction.putValue(Action.SHORT_DESCRIPTION, ...);
Set this action onto the menu's JMenuItem and the toolbar's JButton
   yourMenuItem.setAction(yourAction);
   yourToolButton.setAction(yourAction);
Swing will take the values provided by the putValue(...) calls as text,
icon, toolTipText
for the JMenuItem or JToolBar respectively.
Whenever the menu's JMenuItem and the toolbar's JButton is clicked,
the actionPerformed method of the Action will be called.

Regards
 Thomas
Stephan Friedrichs - 04 Nov 2003 11:04 GMT
> Hi everyone

Hello!

> [...]

> Now, the JToolbar and the JMenuBar are created using different
> objects. Since they both do the same thing, how can I make them share
> the same code? My idea here is trying to avoid writing the same code
> twice, after all both components do the same thing.

Take a look at the javax.swing.Action interface, especially the
AbstractAction implementation. Then you can create it like this

public class NodeAction extends AbstractAction {
    public NodeAction() {
        super("Add a node");
    }

    public void actionPerformed(ActionEvent ace) {
        // add your node
    }
}

and create your components as follows:

Action a = new NodeAction();
JButton b = new JButton(a);
menu.add(a);
toolbar.add(a);

Hope this helps - Stephan

Signature

http://home.t-online.de/home/sfriedrichs/

   JAddressBook: 1.1

Babu Kalakrishnan - 04 Nov 2003 11:23 GMT
> Hi everyone
>
[quoted text clipped - 8 lines]
> the same code? My idea here is trying to avoid writing the same code
> twice, after all both components do the same thing.

javax.swing.Action

BK


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.