Colocar o que foi digitado em um JTextField em uma variável qualquer

Java

30/09/2011

Então eu crie uma telinha simples no swing ... Que você digita sua idade na tela e ela envia pro banco... Mas eu quero pegar o que foi digitado no JTextField e colocar em uma variável qualquer.. E o nome do meu JTextField é t1. Pode me ajudar?
Phelipe Lopes

Phelipe Lopes

Curtidas 0

Respostas

Douglas Eric

Douglas Eric

30/09/2011

String qualquer = t1.getText();
GOSTEI 0
Maycon Back

Maycon Back

30/09/2011

[quote="sekkuar"]
String qualquer = t1.getText();
eu fiz log pra responder, Sekubar responde antes, ninja mode on.
GOSTEI 0
Maycon Back

Maycon Back

30/09/2011

Então o valor não foi pra variável, por que falta um botão de confirmação para confirmar o que foi feito, por que quando eu digito no JTF, preciso fechar a tela no "x" e isso cancela oq foi feito...
GOSTEI 0
Phelipe Lopes

Phelipe Lopes

30/09/2011

Oras. Swing é baseada em /eventos/, tudo depende de [i]quando[/i] você chamar o método. Se você chamar no construtor, o JTextField vai estar vazio, então ele vai retornar vazio, você precisa chamar esse método /depois/ que o usuário tiver digitado, mas para saber que o usuário terminou de digitar, você depende de um evento.
GOSTEI 0
Douglas Eric

Douglas Eric

30/09/2011

Como eu estou iniciando pode me explicar com códigos? Assim ficaria um pouco melhor
GOSTEI 0
Douglas Eric

Douglas Eric

30/09/2011

tutorial sobre eventos: http://download.oracle.com/javase/tutorial/uiswing/events/index.html
GOSTEI 0
Douglas Eric

Douglas Eric

30/09/2011

Acho que entendi, então quando for chamado o metodo construtor precisa ter um evento que leia o que está escrito no JTF?
GOSTEI 0
Phelipe Lopes

Phelipe Lopes

30/09/2011

Não. O Construtor vai criar um /Listener/, esse Listener vai ficar aguardando o tal evento acontecer, e então vai ler o que o usuário digitou.
GOSTEI 0
Douglas Eric

Douglas Eric

30/09/2011

Blz entendi vlw cara...
GOSTEI 0
Douglas Eric

Douglas Eric

30/09/2011

Então vi o tutorial e tentei criar minha tela mais dá erro:>

import java.awt.GridLayout;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.WindowAdapter;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;


public class Teste extends JFrame {
	JTextField t1;
	JLabel label1, label2;
	
	
	
	public static void main (String[] args){
	JFrame janela =  new Teste();
	TratEventosJan trat = new TratEventosJan();
    janela.addKeyListener(trat);
	janela.setVisible(true);
	}
	
	Teste(){
		setTitle("Site adulto");
		setSize(300,120);
		getContentPane().setLayout(new GridLayout(3,1));
		label2 = new JLabel("Por favor informenos a sua idade");
		label1 = new JLabel("Idade:");
		t1= new JTextField();
		getContentPane().add(label2); 
		getContentPane().add(label1); 	getContentPane().add(t1);
		
	}
		class TratEventosJan extends WindowAdapter{
			public void Pegaidade(KeyEvent evento){
				String qualquer = t1.getText();
				System.out.println(qualquer);
			}
		}
		
	}
Erro: Exception in thread "main" java.lang.Error: Unresolved compilation problem: The method addKeyListener(KeyListener) in the type Component is not applicable for the arguments (Teste.TratEventosJan) Acho que fiz tudo errado mas pelo menos tentei, me ajuda?
GOSTEI 0
Douglas Eric

Douglas Eric

30/09/2011

Você criou um WindowListener, tentando usar um KeyEvent... não rola... sugiro usar um ActionListener http://pt.wikibooks.org/wiki/Java/Swing/Eventos_e_Action_listeners adciona ele ao botão ou ao próprio JTextField, assim quando o usuário apertar enter, ele vai ler o texto
GOSTEI 0
Douglas Eric

Douglas Eric

30/09/2011

Boa idéia. Vou tentar aqui.
GOSTEI 0
Douglas Eric

Douglas Eric

30/09/2011

Consegui, muito Obrigado...
GOSTEI 0
POSTAR