🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

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

Re: Jtextfield problem

#9105
From: Sven Goetgeluck
Date: Thu, 25 May 2000 00:00
369 lines
12776 bytes
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
listing 1
package untitled1;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import com.borland.jbcl.layout.*;
import java.beans.*;
import javax.swing.text.Document;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;



public class Frame1 extends JFrame {
  XYLayout xYLayout1 = new XYLayout();
  JTextField jTextField2 = new JTextField();
  NumericEditControl pm2 = new NumericEditControl();

  //Construct the frame
  public Frame1() {
    enableEvents(AWTEvent.WINDOW_EVENT_MASK);
    try  {
      jbInit();
    }
    catch(Exception e) {
      e.printStackTrace();
    }
  }

  //Component initialization
  private void jbInit() throws Exception  {
    this.getContentPane().setLayout(xYLayout1);
    this.setSize(new Dimension(400, 300));
    this.setTitle("Frame Title");
    this.getContentPane().add(jTextField2, new XYConstraints(175, 14,
103, -1));
    this.getContentPane().add(pm2, new XYConstraints(184, 131, 173, 31));
     }
  //Overridden so we can exit on System Close
  protected void processWindowEvent(WindowEvent e) {
    super.processWindowEvent(e);
    if(e.getID() == WindowEvent.WINDOW_CLOSING) {
      System.exit(0);
    }
  }
}

listing 2

      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;

public class JSpinner extends JPanel
 implements ActionListener
{
 protected BasicArrowButton north, south;
  private ActionListener actionListener = null;

  public JSpinner()
 {
  setLayout(new GridLayout(2, 1));
  setPreferredSize(new Dimension(16, 16));

  north = new BasicArrowButton(BasicArrowButton.NORTH);
    north.addActionListener(this);
  add(north);

  south = new BasicArrowButton(BasicArrowButton.SOUTH);
    south.addActionListener(this);
  add(south);
    }
  public void addActionListenr(ActionListener l)
  {
   actionListener = l;
  }
  public void removeActionListenr(ActionListener l)
  {
  actionListener = null;
  }
 public void actionPerformed(ActionEvent event)


  if (event.getSource() == north)
  {
   System.out.println("north");
  }
  if (event.getSource() == south)
  {
   System.out.println("south");
  }
 }
listing3
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.*;


/** A simple kind of numeric spinner control.  It could
 *  be enhanced by replacing the text on the buttons with
 *  anti-aliased images, and by restricting the legal characters
 *  to 0 through 9 by extending PlainDocument [override the "insertString()"
 *  method].
 *
 *  Also, listening to mouse down would allow for the possibility
 *  of holding the mouse down to repeatedly change the value.  As
 *  it is now, the user must click once for each change.
 *
    Copyright:    Copyright (c) 2000
    @author       Paul Furbacher
*/

public class NumericEditControl extends JPanel implements ChangeListener,
MouseListener, FocusListener,ActionListener, AdjustmentListener{
  JTextField tf = new JTextField();
  GridBagLayout gridBagLayout1 = new GridBagLayout();
  Border border1;
  Border border2;
   String text= new String() ;
  public static final String TEXT_PROPERTY = "text";
 protected BasicArrowButton north, south;
  GridLayout gridLayout1 = new GridLayout();
  String lastText;


  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.setBackground(new Color(242, 242, 242));
    tf.setBorder(border2);
    tf.setMinimumSize(new Dimension(40, 19));
    tf.setHorizontalAlignment(JTextField.CENTER);
    setLayout(new BorderLayout());
  add(BorderLayout.CENTER, tf);
    JSpinner spinner = new JSpinner();
  addMouseListener(this);
  addFocusListener(this);
  add(BorderLayout.EAST, spinner);
            lastText = "";
            Document document = tf.getDocument();
            document.addDocumentListener( new DocumentListener() {
                private void fireChange() {
                    String oldText = text;
                    text = tf.getText();
                    firePropertyChange( TEXT_PROPERTY, oldText, text );
                }
                public void changedUpdate( DocumentEvent event ) {
                    fireChange();
                }
                public void insertUpdate( DocumentEvent event ) {
                    fireChange();
                }
                public void removeUpdate( DocumentEvent event ) {
                    fireChange();
                }
            } );



  }

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

  public void mouseClicked(MouseEvent event)
 {
  requestFocus();
 }
  public void mousePressed(MouseEvent event) {
  System.out.println("mousepressed");}
  public void mouseReleased(MouseEvent event)
 System.out.println("mousepressed");}
  public void mouseEntered(MouseEvent event)
 System.out.println("mousepressed");}
  public void mouseExited(MouseEvent event)
 System.out.println("mousepressed");}

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

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

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

  public void adjustmentValueChanged(AdjustmentEvent e) {
    //TODO: implement this java.awt.event.AdjustmentListener method;
     System.out.println("adjustment");
  }

}



thanx Sven
Paul Furbacher [TeamB] <pnf@home.com> schreef in berichtnieuws
392C4CAD.2C8B800C@home.com...
> sven goetgeluck wrote:
> >
> > Hi Paul this is my code , ...
>
> Actually, John McGrath has been helping you out
> with the latest set of problems.  He may have a
> separate response, but I thought I'd jump back into
> this.
>
> First, I notice that your Spinner object has a dependency
> which it really should not have.  It requires a textField.
> When designing components, you should design them to be
> what is called "contractless".  That means that they should
> be able to stand on their own with no dependencies on other
> components.
>
> In this case, your spinner control should simply
> allow other components to register themselves as listeners
> to any change caused by mouse clicks on either of the buttons.
> It should know nothing about who may be listening except that
> they implement the appropriate listener interface
>
> So, to do this, you would add the following
> instance variable and methods to Spinner:
>
>   private ActionListener actionListener = null;
>
>   public void addActionListener(ActionListener l) {
>     actionListener = l;
>   }
>   public void removeActionListener(ActionListener l) {
>     actionListener = null;
>   }
>
> You would pass to the listener "positive" or "negative" commands.
> Actually, assign actionCommand strings to the two buttons and
> just pass that actionCommand string to the listener.  Note, we are
> assuming just a single listener.  This may not fit all cases, but
> it does fit this particular case.
>
> I don't see any need for a model in Spinner.  It is simply a control
> which emits notifications/events to listeners of how it has changed.
> It does not need to maintain any sense of "state".
>
> Lastly, with regard to Spinner, it should never receive keyboard focus
> (spinners just don't behave that way), you should not provide KeyEvent
> handling in the spinner.  Instead, that event handling should be done
> in the textfield because it naturally handles key events.  As a
> consequence of this, the "increment()" and "decrement()" don't make
> any sense here.
>
>
> When you have these changes made to Spinner, put it on a frame, and
> register the frame as a listener.  With each new component, you have
> to test it in isolation.  To test whether events are being broadcast
> properly, just use "System.out.println()" statements in the event
> handling method of the frame.  Write a "main()" method which
> instantiates the frame, pack and show it.  Click on the
> spinner and see whether you get what you expect.
>
> After you are sure the spinner works correctly (it just emits
> change events with values of "positive" or "negative") move
> on to the next set of design issues.
>
>
> Next, let's rename PlusMinusTextField to "NumericEditControl".  It
> was my mistake to name it PlusMinusTextField in the first place.
> It's a compound control, so it shouldn't be named after just
> one of its subcomponents.
>
> In the NumericEditControl component, add "this" as a listener
> to the spinner subcomponent instance and implement the
> ActionPerformed interface.
>
> I notice from your code in PlusMinusTextField that you have
> added a "jTextField" component, and more or less ignored the
> original "tf".  Either you want one or the other, not both.
>
> Also, you seem to be using the Designer and then rearranging
> things by hand.  Try to avoid this.  Just let JBuilder do its
> job, and you fill in the blanks where it asks you to.
>
> The "increment()" and "decrement()" methods would make sense
> in the NumericEditControl.  Call these when you handle the
> action event which comes from the spinner.
>
> The NumericEditControl needs an "add<type>Listener()" where
> you choose the type of the listener which other components
> should register as in order to get notifications that a change
> in value has occurred.  At present, you have no way to register
> listeners with this control.  No component should have to
> "reach into" the NumericEditControl just to receive change
> notifications.  Essentially, you are "reaching into" when
> you get the document and then add the textfield in Frame1
> as a listener.  This isn't the way it should be done.  Outsiders
> should not know the implementation details.
>
> For a quick solution, I'd make it an AdjustmentEvent.  Why?
>
> 1) The idea behind object-oriented programming is to increase
> re-use.  AdjustmentEvent is already written for some very
> similar cases.  It may, at first, seem like overkill.
>
> 2) If you implement the Adjustable interface in
> NumericEditControl (use the "Implement interface" Wizard)
> lots of the work is already done for you.  And, you could
> customize this control so that it would have limits, and
> you could do fancy things with how much each mouse click
> changes the value.
>
> Finally, when you get all that to work, come back and
> we'll talk about subclassing PlainDocument so that only
> numeric characters can be inserted into the textfield
> sub-component of NumericEditControl.
>
>
> If you get stuck, come back to the newsgroup.  I might
> have some revised code for you.  But for now, it's
> probably better if you struggle with the issues on
> your own.  Good luck.
>
>
> Paul Furbacher (TeamB)
Technical Details
Message-ID: <392c5d2d@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>

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: <392c5d2d@dnews>
X-Deja-AN: 626927494
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>
X-Trace: 24 May 2000 15:52:29 -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