🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

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

Re: Jtextfield problem

#9107
From: sven goetgeluck
Date: Thu, 25 May 2000 00:00
314 lines
10421 bytes
John is this the spinner like you have it in mind ??

package spinner;
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);
 }
}

i just don(t know what is ment by mods in the function fireactionperformed

listing 2

package spinner;

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 PlusMinusTextField extends JPanel implements ChangeListener,
MouseListener, FocusListener,ActionListener{
  JTextField tf = new JTextField();
  GridBagLayout gridBagLayout1 = new GridBagLayout();
  Border border1;
  Border border2;
  private String text;
  protected BasicArrowButton north, south;
  GridLayout gridLayout1 = new GridLayout();



  public PlusMinusTextField() {
    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);
    JSpinner spinner = new JSpinner();
    addMouseListener(this);
    addFocusListener(this);
    add(BorderLayout.EAST, spinner);
  }

  Document getDocument()
  {
   return tf.getDocument();

  }
  String getString()
  {
   return tf.getText();
  }

  void north_actionPerformed(ActionEvent e) {

  }
  public void stateChanged(ChangeEvent event)
  {
   repaint();
  }

  public void mouseClicked(MouseEvent event)
  {
   requestFocus();
   }

  public void mousePressed(MouseEvent event) {}
  public void mouseReleased(MouseEvent event) {}
  public void mouseEntered(MouseEvent event) {}
  public void mouseExited(MouseEvent event) {}

  public void focusGained(FocusEvent event)
 {
  repaint();
 }

  public void focusLost(FocusEvent event)
 {
  repaint();
 }

  public boolean isFocusTraversable()
 {
  return true;
 }
  public void actionPerformed(ActionEvent event)
 {
  repaint();
 }

}



greetings Sven
John P. McGrath <JPMcGrath@fast.net> schreef in berichtnieuws
MPG.1396a443467bdd02989954@forums.borland.com...
> 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: <392d1d09@dnews>
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> <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: "sven goetgeluck" <sven.goetgeluck@pandora.be>
Subject: Re: Jtextfield problem
Date: 2000/05/25
Message-ID: <392d1d09@dnews>
X-Deja-AN: 627138124
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-Trace: 25 May 2000 05:31:05 -0800, 127.0.0.1
X-MSMail-Priority: Normal
X-Priority: 3
Newsgroups: borland.public.jbuilder.java.swing
X-MimeOLE: Produced By Microsoft MimeOLE V5.00.2615.200