elhacker.net cabecera Bienvenido(a), Visitante. Por favor Ingresar o Registrarse
¿Perdiste tu email de activación?.

 

 


Tema destacado: Guía actualizada para evitar que un ransomware ataque tu empresa


+  Foro de elhacker.net
|-+  Programación
| |-+  Programación General
| | |-+  Java
| | | |-+  limitar textbox
0 Usuarios y 1 Visitante están viendo este tema.
Páginas: [1] Ir Abajo Respuesta Imprimir
Autor Tema: limitar textbox  (Leído 3,422 veces)
Roboto


Desconectado Desconectado

Mensajes: 581



Ver Perfil WWW
limitar textbox
« en: 22 Noviembre 2011, 12:42 pm »

Veran,uso netbeans,pork me parece mejor k eclipse,pero eso da iwal.
tengo los 2 programas.

mi problema esk usando formularios o sin usarlos,da iwal.
no encuentro la opcion de limitar un JTextField a un nº det. de caracteres.

lo unico k se me ocurrio fue,hacer un evento keyPress,que contara el nº de letras,y cuando llegaras al tope,no te deje meter mas.

el prblema esk cuando tratas de meter,se ve la letra y luego se borra (keda muy warro), saben alguna forma de como hacerlo?


En línea

adastra
Endless Learner
Ex-Staff
*
Desconectado Desconectado

Mensajes: 885


http://thehackerway.com/


Ver Perfil WWW
Re: limitar textbox
« Respuesta #1 en: 22 Noviembre 2011, 12:54 pm »

Necesitas usar un objeto JTextFieldLimit
por ejemplo:

Código:
tx = new JTextField(15);
tx.setDocument(new JTextFieldLimit(10));


En línea

adastra
Endless Learner
Ex-Staff
*
Desconectado Desconectado

Mensajes: 885


http://thehackerway.com/


Ver Perfil WWW
Re: limitar textbox
« Respuesta #2 en: 22 Noviembre 2011, 12:56 pm »

Perdón le di enviar antes de terminar...

el objeto anterior debes crearlo tu mismo, y el metodo setDocument establece esos limites, por ejemplo:
Código:
import java.awt.FlowLayout;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.text.AttributeSet;
import javax.swing.text.BadLocationException;
import javax.swing.text.PlainDocument;

class JTextFieldLimit extends PlainDocument {
  private int limit;
  JTextFieldLimit(int limit) {
    super();
    this.limit = limit;
  }

  JTextFieldLimit(int limit, boolean upper) {
    super();
    this.limit = limit;
  }

  public void insertString(int offset, String str, AttributeSet attr) throws BadLocationException {
    if (str == null)
      return;

    if ((getLength() + str.length()) <= limit) {
      super.insertString(offset, str, attr);
    }
  }
}

public class Main extends JFrame {
  JTextField textfield1;

  JLabel label1;

  public void init() {
    setLayout(new FlowLayout());
    label1 = new JLabel("max 10 chars");
    textfield1 = new JTextField(15);
    add(label1);
    add(textfield1);
    textfield1.setDocument(new JTextFieldLimit(10));
   
    setSize(300,300);
    setVisible(true);
  }
En línea

Roboto


Desconectado Desconectado

Mensajes: 581



Ver Perfil WWW
Re: limitar textbox
« Respuesta #3 en: 22 Noviembre 2011, 13:27 pm »

thx,voy a probarlo
En línea

Páginas: [1] Ir Arriba Respuesta Imprimir 

Ir a:  

WAP2 - Aviso Legal - Powered by SMF 1.1.21 | SMF © 2006-2008, Simple Machines