Article View: borland.public.jbuilder.java.swing
Article #9114Re: Jtextfield problem
From: Sven Goetgeluck
Date: Fri, 26 May 2000 00:00
Date: Fri, 26 May 2000 00:00
349 lines
11810 bytes
11810 bytes
Code so far like paul and John see it
listing 1
--------
package untitled1;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.*;
import javax.swing.plaf.basic.BasicArrowButton;
import javax.swing.JTextField;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.*;
import javax.swing.plaf.basic.BasicArrowButton;
import javax.swing.JTextField;
public class JSpinner extends JPanel
/*implements ActionListener*/
{
protected BasicArrowButton north, south;
private ActionListener actionListener = null;
public static final String UP_ACTION = "up";
public static final String DOWN_ACTION = "down";
public JSpinner()
{
setLayout(new GridLayout(2, 1));
setPreferredSize(new Dimension(16, 16));
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() );
}
};
private void fireActionPerformed( String actionCommand, int mods ) {
if ( actionListener != null ) {
ActionEvent event = new ActionEvent( this,
ActionEvent.ACTION_PERFORMED, actionCommand, mods );
actionListener.actionPerformed( event );
}
}
public void addActionListenr(ActionListener l)
{
actionListener = l;
}
public void removeActionListenr(ActionListener l)
{
actionListener = null;
}
public void actionPerformed(ActionEvent event)
{
fireActionPerformed(UP_ACTION,1);
}
}
listing 2
-------
package untitled1;
import java.awt.*;
import javax.swing.*;
import com.borland.jbcl.layout.*;
import java.awt.event.*;
import javax.swing.border.*;
import javax.swing.text.Document;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import javax.swing.plaf.basic.BasicArrowButton;
import javax.swing.*;
import javax.swing.event.*;
public class NumericEditControl extends JPanel implements AdjustmentListener
{
JTextField tf = new JTextField();
GridBagLayout gridBagLayout1 = new GridBagLayout();
Border border1;
Border border2;
private String text;
protected BasicArrowButton north, south;
GridLayout gridLayout1 = new GridLayout();
JSpinner spinner = new JSpinner();
public NumericEditControl() {
try {
jbInit();
}
catch (Exception ex) {
ex.printStackTrace();
}
}
private void jbInit() throws Exception {
border1 =
BorderFactory.createBevelBorder(
BevelBorder.LOWERED,
Color.white,Color.white,
new Color(149, 142, 130),
new Color(104, 99, 91)
);
// Hide the border of the textfield itself.
border2 = BorderFactory.createEmptyBorder();
tf.setPreferredSize(new Dimension(40, 19));
tf.setBorder(border1);
tf.setMinimumSize(new Dimension(40, 19));
tf.setText("0");
setLayout(new BorderLayout());
add(BorderLayout.CENTER, tf);
add(BorderLayout.EAST, spinner);
}
String getValue()
{
return tf.getText();
}
public boolean isFocusTraversable()
{
return true;
}
public void actionPerformed(ActionEvent event)
{
}
public void adjustmentValueChanged(AdjustmentEvent e) {
//TODO: implement this java.awt.event.AdjustmentListener method;
}
}
>Where do i put the actionmistener of the spinner in the Nemericeditfield do
it listens to reaction of the spinner?
how to add a listener to the frame so i can update other components in the
frame , is this possible without the enter key????
hope you both will help me to the end with this because i have hard time
understanding this all but it is comming
greeting sven
Paul Furbacher [TeamB] <pnf@home.com> schreef in berichtnieuws
392D3672.C45A6D1F@home.com...
> "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:
<392d8347@dnews>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> <392D3672.C45A6D1F@home.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: "Sven Goetgeluck" <sven.goetgeluck@pandora.be> Subject: Re: Jtextfield problem Date: 2000/05/26 Message-ID: <392d8347@dnews> X-Deja-AN: 627303226 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> <392D3672.C45A6D1F@home.com> X-Trace: 25 May 2000 12:47:19 -0800, 213.224.45.64 X-MSMail-Priority: Normal X-Priority: 3 Newsgroups: borland.public.jbuilder.java.swing X-MimeOLE: Produced By Microsoft MimeOLE V5.00.2615.200