PDA

View Full Version : [JAVA] JTextArea e Length


joeykiske
30-04-2010, 02:31
Salve!
sto provando a conoscere quanti caratteri sono stati scritti nella JTA comunicandoli direttamente su di una label.........

posto il codice del programma di prova..

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;

import javax.swing.*;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import javax.swing.text.Document;

public class Prova04
{
static int numlabel;
public static void main(String[] args)
{
JFrame w = new JFrame("Prova TXA");
final JTextArea t = new JTextArea(3,4);
w.setLayout(new BorderLayout());
w.add(t,BorderLayout.NORTH);
//DOCUMENTO PER PRELEVARE LA JTEXTAREA
Document prova = t.getDocument();
//LISTENER
prova.addDocumentListener(new DocumentListener()
{
public void changedUpdate(DocumentEvent e)
{ a = e.getDocument().getLength());
System.out.print(e.getDocument().getLength();
}

public void insertUpdate(DocumentEvent e)
{a = e.getDocument().getLength());
System.out.print(e.getDocument().getLength();
}

public void removeUpdate(DocumentEvent e)
{a = e.getDocument().getLength());
System.out.print(e.getDocument().getLength();
}
});

//TRASFORMARE NUMERO IN STRINGA POI IN LABEL
String numero = Integer.toString(numlabel);
JLabel asd = new JLabel(numero);
w.add(asd,BorderLayout.SOUTH);

w.pack();
w.setVisible(true);
}
}


.....Voi come avreste implementato il codice?!?!

è una mia curiosità,nulla di più :p :p

grazie anticipatamente!!

joeykiske
03-05-2010, 15:15
..anyone know alternative method?! :mbe: ..:D

banryu79
03-05-2010, 16:49
Beh, una volta che hai consultato il Document di un text component e hai recuperato il valore della sua proprietà "lenght" la storia finisce.
Non è che ci sia altro da sapere :D

Comunque:

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import javax.swing.text.Document;

/**
* Conta i caratteri digitati nella JTextArea mentre vengono inseriti.
*
* @author Francesco
*/
public class Example
{
public static void main(String... args) {
JFrame frame = new JFrame("Example");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

JLabel label1 = new JLabel("caratteri inseriti: ");
final JLabel label2 = new JLabel("0");
JPanel p = new JPanel();
p.add(label1, FlowLayout.LEFT);
p.add(label2);

JTextArea area = new JTextArea(8, 45);
area.setLineWrap(true);
area.setWrapStyleWord(true);

Document doc = area.getDocument();
doc.addDocumentListener(new DocumentListener() {
@Override public void insertUpdate(DocumentEvent e) {
updateLabel(e.getDocument().getLength());
}
@Override public void removeUpdate(DocumentEvent e) {
updateLabel(e.getDocument().getLength());
}
@Override public void changedUpdate(DocumentEvent e) {
updateLabel(e.getDocument().getLength());
}

private void updateLabel(int length) {
label2.setText(String.valueOf(lenght));
}
});

frame.add(p, BorderLayout.NORTH);
frame.add(area);
frame.pack();
frame.setVisible(true);
}
}

Praticamente identico al tuo.

joeykiske
03-05-2010, 19:41
okiii thanks mille! :D