🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: borland.public.jbuilder.java.swing
Article #9106

Re: Jtextfield problem

#9106
From: John P. McGrath
Date: Thu, 25 May 2000 00:00
125 lines
5538 bytes
On Thu, 25 May 2000 07:00:22 +0200, Sven Goetgeluck wrote:

I hope that Sven is not getting dizzy with comments coming from two 
different directions.  And Paul, if I say anything that does not fit with 
what you had in mind, please say so.  Even if it is something that you 
would do just a little differently, I would really like to hear it.  I 
have no experienced Java developers for co-workers, so I always wonder if 
there is a better way to do things.

> Hi Paul ,
> thanks for such an excelent explication,
> did al the work to told me but need to come back because
> im missing the link somewhere but cannot figure it out

I see two "missing links".

First, your spinner class is not firing any events.  Second, nothing is 
listening for events from the spinner.

The spinner already recognizes when the "north" or "south" button has 
been pressed, but it only prints a message when that happens.  It needs 
to fire an event.  So I would first write a separate method that fires 
the ActionEvent.

As it is written now, you can only have one listener, so this is pretty 
easy.  I would probably change that later to allow for multiple 
listeners.  But for the single listener, your method might look something 
like:

    private void fireActionPerformed( String actionCommand, int mods ) {
        if ( actionListener != null ) {
            ActionEvent event = new ActionEvent( this,
                ActionEvent.ACTION_PERFORMED, actionCommand, mods );
            actionListener.actionPerformed( event );
        }
    }

I would then add constants for the actionCommands that you are going to 
fire:

    public static final String UP_ACTION = "up";
    public static final String DOWN_ACTION = "down";

This method would then be called from your actionPerformed() method.  You 
could just add a fireActionPerformed() for each case, but a better way 
would be to use the same actionCommand for the buttons.  Then you would 
not have to treat the two buttons differently.

One thing that I would do a little differently from the existing code 
would be to use an inner class for the ActionListener that listens to the 
buttons.  I have often seen code written to implement the listener 
interface in the class itself, but I don't think it is the cleanest way 
to do it.  This is because I do not believe that the knowledge that the 
spinner listens to the two buttons should not be part of its public 
interface.

This change does add another object, so some may argue for the other 
approach based on efficiency, but this is a very minor issue.  Any 
comments, Paul?

public class JSpinner extends JPanel {
          :
          :
    public JSpinner()
    {
          :
          :
        north = new BasicArrowButton(BasicArrowButton.NORTH);
        north.addActionListener(buttonListener);
        north.setActionCommand(UP_ACTION);
        add(north);

        south = new BasicArrowButton(BasicArrowButton.SOUTH);
        south.addActionListener(buttonListener);
        south.setActionCommand(DOWN_ACTION);
        add(south);
    }
          :
          :
    private ActionListener buttonListener = new ActionListener() {
        public void actionPerformed(ActionEvent event) {
            fireActionPerformed( event.getActionCommand(),
                event.getModifiers() );
        }
    };


At this point, you have a nice, stand-alone spinner component that will 
fire an UP_ACTION or a DOWN_ACTION when the buttons are pushed.

Now you need to add a listener to hear these events, and increment or 
decrement the text field.  But before doing that, I would make one change 
to the class structure.  The current design has a compound component with 
a text field and a spinner in it.  If you are going to add functionality 
that will limit the text field to numeric values, I think it makes sense 
for the entry field to be written as a separate class.  That way, you can 
use the numeric entry field by itself, without an attached spinner.

To create the new class, I would start with:

public class NumericField
    extends JTextField
{
    public NumericField() {
    }

    public NumericField( int columns ) {
        super( columns );
    }
}

More code will be needed here.  You will certainly want to add getValue() 
and setValue() methods, and this is where you would add the increment() 
and decrement() methods as well.

Then your main component (NumericEditControl) class would add a listener 
to the spinner, and call the NumericField's increment() and decrement() 
methods when it gets the event from the spinner.

Is this what you would do with it, Paul?

-- 
John P. McGrath
<JPMcGrath@fast.net>
<jpm@nscs.fast.net>
Technical Details
Message-ID: <MPG.1396a443467bdd02989954@forums.borland.com>
Path: arc1-ca.pugleaf.net!unknown!not-for-mail
References: <3921af12@dnews> <39230C94.3B09A3A0@home.com> <39242302@dnews> <MPG.138e6cabc7cff616989936@forums.borland.com> <39250aa2@dnews> <MPG.139148a8c31034b098993c@forums.borland.com> <392A7C85.7D72C823@officeline.be> <MPG.139505deb9177169989947@forums.borland.com> <392BEFE0.6249F1E8@officeline.be> <392C4CAD.2C8B800C@home.com> <392c5d2d@dnews>

Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit
X-Google-Thread: 111d45,e3d8a7969574fa02
X-Google-Attributes: gid111d45,public
From: John P. McGrath <JPMcGrath@fast.net>
Subject: Re: Jtextfield problem
Date: 2000/05/25
Message-ID: <MPG.1396a443467bdd02989954@forums.borland.com>
X-Deja-AN: 627071374
References: <3921af12@dnews> <39230C94.3B09A3A0@home.com> <39242302@dnews> <MPG.138e6cabc7cff616989936@forums.borland.com> <39250aa2@dnews> <MPG.139148a8c31034b098993c@forums.borland.com> <392A7C85.7D72C823@officeline.be> <MPG.139505deb9177169989947@forums.borland.com> <392BEFE0.6249F1E8@officeline.be> <392C4CAD.2C8B800C@home.com> <392c5d2d@dnews>
Organization: Another Netscape Collabra Server User
Newsgroups: borland.public.jbuilder.java.swing