The Artima Developer Community
Sponsored Link

Java Answers Forum
JTextfield

2 replies on 1 page. Most recent reply: Apr 18, 2002 4:01 PM by Thomas SMETS

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 2 replies on 1 page
Chris Francis

Posts: 2
Nickname: fran
Registered: Mar, 2002

JTextfield Posted: Apr 18, 2002 9:08 AM
Reply to this message Reply
Advertisement
Hi,

i have an applet with textfields and a submit button. When the button is pressed i want an error message to be displayed if there are to many or not enough characters in the textfield.

Can anyone help???


Hiran

Posts: 41
Nickname: jclu
Registered: Mar, 2002

Re: JTextfield Posted: Apr 18, 2002 11:08 AM
Reply to this message Reply
JTextField has a method called getDocument that gets the current document associated with the JTextField. Document has a method called getLength that gets the number of chars in the document. You could use those two methods to get the number of chars, check using an if statement if the length is enough and display a message if it isn't. In terms of displaying the message, I don't know if you can use a JDialog. If so, use a JDialog. Otherwise, I'm not sure what you can use. Hope this helps.
Hiran

Thomas SMETS

Posts: 307
Nickname: tsmets
Registered: Apr, 2002

Re: JTextfield Posted: Apr 18, 2002 4:01 PM
Reply to this message Reply
Chris,
I believe this helps ?
Beware that you need to slightly resize the window to see its content...
I still dunno why ...
package test.gui;
 
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
 
public class TextFieldLength  
{
  public JFrame jf = new JFrame("JFrame");
  public Frame f = new Frame ("Frame");
  
  public TextField tf;
  public JTextField jtf;
  
  public JButton jb = new JButton("Validate");
  
  TextFieldLength ()
  {
    jf.setVisible (true);
    jf.setSize (500, 200);
    jf.setLocation ( 0, 0);
    jtf = new JTextField (10);               
    jf.getContentPane ().add (jtf, BorderLayout.CENTER);
    
    jb.addActionListener (new CustomActionListener ());
    jf.getContentPane ().add (jb, BorderLayout.EAST);
    jf.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
    
    f.setVisible (true);
    f.setSize (300, 300);
    f.setLocation (500, 200);
    tf = new TextField (10);    
    f.add (tf, BorderLayout.CENTER);    
  }
 
/** 
  *
  */
  public static void main (String[] args)
  {
    new TextFieldLength();
  }
 
  class CustomActionListener
    implements ActionListener
  {
    public void actionPerformed (ActionEvent anActionEvent)
    {
      System.out.println ("Button pushed");
      System.out.println ("There is " + jtf.getDocument ().getLength () + " character");
    }
  }
}


I can only enough advise you to use inner class they are really smthg beautyfull to handle events

Thomas SMETS
SCJP, Brussels

Flat View: This topic has 2 replies on 1 page
Topic: creaing own awt table Previous Topic   Next Topic Topic: The Singleton Pattern

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use