Article View: borland.public.jbuilder.java.swing
Article #9109Re: Jtextfield problem
From: Paul Furbacher [TeamB]
Date: Thu, 25 May 2000 00:00
Date: Thu, 25 May 2000 00:00
174 lines
7254 bytes
7254 bytes
"John P. McGrath" 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.
Same here, locked away in my attic "office" at home.
>[...]
> 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. ...
Agreed. What I did in roughing out my version of this was
to just copy from an existing Swing class which uses actionPerformed
(JButton, perhaps) the
> 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";
My suggestion was "positive" and "negative" -- half-dozen vs. six.
> 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?
I agree with this.
[...]
> ... 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.
> [...]
> Is this what you would do with it, Paul?
Actually, from a code re-use standpoint, you get more re-use
from your approach, ending up with three re-usable objects
instead of the two I was suggesting. Throw in a NumericDocument
which your NumericField would need, and you've got four re-usable
classes. Object population explosion.
Now, back to ...
> public class PlusMinusTextField
extends JPanel
implements
ChangeListener,
MouseListener,
FocusListener,
ActionListener
//
// You don't need to implement any of these interfaces
// as far as I can see. I did make the suggestion that
// you implement the "Adjustable" interface using the
// Wizard. I still think that's a good idea because
// you could use at least the "setValue()" and "getValue()"
// methods, not to mention the possibility of developing
// a bounded model for this component. And what if you
// want to go hog wild and implement accelerated incrementing/
// decrementing when the user holds the mouse down on a button
// for a specified length of time? The "blockIncrement"
// methods of the Adjustable interface fit the bill nicely.
{
> JTextField tf = new JTextField();
> GridBagLayout gridBagLayout1 = new GridBagLayout();
> Border border1;
> Border border2;
> private String text;
> protected BasicArrowButton north, south;
> GridLayout gridLayout1 = new GridLayout();
// Put the JSpinner declaration here. It's a
// component you could have placed visually.
JSpinner spinner = new JSpinner();
> public PlusMinusTextField() {...}
>
> private void jbInit() throws Exception {
> [..]
// the following listeners aren't need, unless you
// can make a good case for them.
> addMouseListener(this);
> addFocusListener(this);
> [...]
**** But you do have to add this component as a listener
**** to its spinner sub-component. That's how you know that
**** you are supposed to increment or decrement. This code
**** is very similar to the actionPerformed code you wrote
**** in the Spinner object. Use that as a guide. The difference
**** here is that you will fire an adjustmentValueChanged event
**** to any adjustment listeners, after you increment/decrement.
> }
// It's doubtful that you need this document-related stuff.
// "getValue()" should be all you need for this component.
>x Document getDocument()
>x {
>x return tf.getDocument();
>x }
>x String getString()
>x {
>x return tf.getText();
>x }
// These no longer makes any sense here.
>x void north_actionPerformed(ActionEvent e) {}
>x public void stateChanged(ChangeEvent event) {
>x repaint();
>x }
>x
>x public void mouseClicked(MouseEvent event) {
>x requestFocus();
>x }
>x
>x public void mousePressed(MouseEvent event) {}
>x public void mouseReleased(MouseEvent event) {}
>x public void mouseEntered(MouseEvent event) {}
>x public void mouseExited(MouseEvent event) {}
>x
>x public void focusGained(FocusEvent event) { repaint(); } // ???
>
> public void focusLost(FocusEvent event) { repaint(); } // ???
>
> public boolean isFocusTraversable() { return true; }
** Here's where you have to fire an event to any listener, such
** as that textfield in you Frame1 object. If you follow the
** suggestion of implementing the Adjustable interface, simply
** drill-down into the code for JScrollbar and copy those methods
** which are involved implementing the event messaging scheme:
** fireAdjustmentValueChanged() and whatnot. However, instead
** of this "actionPerformed()" method, you want to do the
** actionPerformed handling as you did in the Spinner.
> public void actionPerformed(ActionEvent event) {
> }
** Finally, you need to trap "actionPerformed" events from the
** numeric textfield sub-component. When the user presses "Enter",
** you need to fire a value changed event to the listener(s).
>
> }
That's about it, for now.
Paul Furbacher (TeamB)
Technical Details
Message-ID:
<392D3672.C45A6D1F@home.com>Path:
arc1-ca.pugleaf.net!unknown!not-for-mailReferences:
<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> <MPG.1396a443467bdd02989954@forums.borland.com>Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 111d45,e3d8a7969574fa02 X-Google-Attributes: gid111d45,public From: "Paul Furbacher [TeamB]" <pnf@home.com> Subject: Re: Jtextfield problem Date: 2000/05/25 Message-ID: <392D3672.C45A6D1F@home.com> X-Deja-AN: 627186853 Content-Transfer-Encoding: 7bit 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> <MPG.1396a443467bdd02989954@forums.borland.com> X-Accept-Language: en,pdf Content-Type: text/plain; charset=us-ascii Organization: Reverse Alchemy, Inc. Mime-Version: 1.0 Reply-To: pnf@home.com Newsgroups: borland.public.jbuilder.java.swing