2010-03-16 14 views
7

Estoy usando un objeto de JTextArea en mi aplicación que se ocupa de enviar sms.¿Cómo mantener el tamaño de JTextArea constante?

He usado un DocumentFilter para permitir que solo se escriban 160 caracteres en el área de texto pero ahora, quiero que el tamaño del área de texto sea constante. sigue aumentando si continúo escribiendo en la misma línea sin presionar la tecla "enter" o incluso cuando sigo presionando solo Ingrese la clave. Intenté usar una vez 'scrollbar' también, pero el problema sigue siendo el mismo. Sugerirme algo sobre esto. A continuación está mi código. Compruébelo por favor.

class Send_sms extends JPanel implements ActionListener,DocumentListener  
{  
    JButton send; 
    JTextArea smst; 
    JLabel title,limit; 
    JPanel mainp,titlep,sendp,wrap,titlewrap,blankp1,blankp2,sendwrap; 
    JScrollPane scroll; 
    Border br,blackbr; 
    Boolean flag = false; 
    PlainDocument plane; 
    public static final int LINES = 4; 
    public static final int CHAR_PER_LINE = 40;  
     //character limit 160 for a sms 

    public Send_sms() 
     { 
     br = BorderFactory.createLineBorder(Color.RED); 
     blackbr = BorderFactory.createEtchedBorder(EtchedBorder.RAISED,Color.DARK_GRAY,Color.GRAY); 
     setBorder(blackbr); 

       title = new JLabel("Enter the text you want to send!"); 
     title.setFont(new Font("",Font.BOLD,17)); 
     limit = new JLabel(""+charCount+" Characters"); 
     smst = new JTextArea(LINES,CHAR_PER_LINE); 
     smst.setSize(100,100); 
     plane = (PlainDocument)smst.getDocument(); 
     //adding DocumentSizeFilter 2 keep track of characters entered 
     plane.setDocumentFilter(new DocumentSizeFilter(charCount)); 
     plane.addDocumentListener(this); 
     send = new JButton("Send"); 
     send.setToolTipText("Click Here To Send SMS"); 
     send.addActionListener(this); 

     //scroll = new JScrollPane(smst); 
     //scroll.setPreferredSize(new Dimension(200,200)); 
     //scroll.setVerticalScrollBarPolicy(null); 
     //scroll.setHorizontalScrollBarPolicy(null); 
     smst.setBorder(br); 

     blankp1 = new JPanel(); 
     blankp2 = new JPanel(); 
     titlep = new JPanel(new FlowLayout(FlowLayout.CENTER)); 
     titlewrap = new JPanel(new GridLayout(2,1)); 
     mainp = new JPanel(new BorderLayout()); 
     sendwrap = new JPanel(new GridLayout(3,1)); 
     sendp = new JPanel(new FlowLayout(FlowLayout.CENTER)); 
     wrap = new JPanel(new BorderLayout()); 

     titlep.add(title); 
     titlewrap.add(titlep); 
     titlewrap.add(blankp1); 

     sendp.add(send); 
     sendwrap.add(limit); 
     sendwrap.add(blankp2); 
     sendwrap.add(sendp); 

     wrap.add(smst,BorderLayout.CENTER); 
     mainp.add(titlewrap,BorderLayout.NORTH); 
     mainp.add(wrap,BorderLayout.CENTER); 
     mainp.add(sendwrap,BorderLayout.SOUTH); 
     add(mainp); 


       } 

    public void actionPerformed(ActionEvent e) 
    { 
     Vector<Vector<String>> info = new Vector<Vector<String>>(); 
     Vector<String> numbers = new Vector<String>(); 
     if(e.getSource() == send) 
     { 
      //Call a function to send he message to all the clients using text 
      //charCount = 165; 
      String msg = smst.getText(); 
      if(msg.length() == 0) 
       JOptionPane.showMessageDialog(null,"Please Enter Message","Error",JOptionPane.ERROR_MESSAGE); 
      else 
      { 
      // System.out.println("Message:"+msg); 

       Viewdata frame = new Viewdata(msg); 

       limit.setText(""+charCount+" Characters"); 
       charCount = 160; 
       } 
     } 
    } 
    public void insertUpdate(DocumentEvent e) 
    { 
     System.out.println("The legth:(insert) "+e.getLength()); 
     for(int i = 0;i<e.getLength(); i++) 
     { 
      if(charCount >0) 
       charCount--; 
      else 
       break; 
     } 
     limit.setText(""+charCount+" Characters"); 

    } 
    public void removeUpdate(DocumentEvent e) 
    { 
     //System.out.println("The legth(remove): "+e.getLength()); 
     for(int i = 0;i<e.getLength(); i++) 
     { 

      charCount++; 

     } 
     limit.setText(""+charCount+" Characters");  
    } 
    public void changedUpdate(DocumentEvent e) 
    { 
     //System.out.println("The legth(change): "+e.getLength()); 

    } 

}//end Send_sms 

Respuesta

5

es necesario que especifique:

textArea.setColumns (160); 
textArea.setLineWrap (true); 
textArea.setWrapStyleWord (false); //default 

Pero el verdadero problema es que se permite introducir más de 160 caracteres. Necesita crear algún tipo de validador que saltee todos los caracteres imputados cuando ya hay 160 caracteres escritos.

10

sonido como que está creando el área de texto usando

JTextArea textArea = new JTextArea(); 

Al utilizar este formato el área de texto no tiene un tamaño preferido por lo que sigue creciendo. Si usa:

JTextArea textArea = new JTextArea(2, 30); 
JScrollPane scrollPane = new JScrollPane(textArea); 

A continuación, el área de texto tendrá un tamaño preferido de 2 filas y (aproximadamente) 30 columnas. Cuando escriba cuando exceda el ancho preferido, aparecerá la barra de desplazamiento horizontal. O si activa el ajuste, el texto se ajustará y aparecerá una barra de desplazamiento vertical.

-1

Inicializar el área de texto de un documento que se extiende PlainDocument y en el método insertString limitar los caracteres a 160

+0

-1, esto no tiene nada que ver con el control del tamaño del área de texto, sólo el número de caracteres que se ser agregado al documento. Además, el usuario ya ha escrito un DocumentFilter para hacer esto, que es el enfoque preferido para limitar el número de caracteres. – camickr

Cuestiones relacionadas