Problemas requestFocus JButton

6 respostas
G

Amigos,

Estou com um problema no requestFocus(), não esta funcionando…

Na minha interface tem um JTextField e um JButton, quero que o foco fique no botao, pois configurei no JTextField uma mensagem q quando o usuario clicar apaga a mensagem…
Mas o foco ja começa nele… ai a mensagem nao aparece…

ja tentei…
myJBtton.requestFocus()… mas nao adiantou…

6 Respostas

ajfilho
Abraços!

Abraços!

G

Não funcionou também....

Segue código....

public MCalculatorUI() {
		super("MCalculator");
		getContentPane().setLayout(null);
		getContentPane().setBackground(Color.WHITE);

		Icon bug = new ImageIcon("imagem/wittelp.PNG");
		imagemJLabel = new JLabel(bug);
		getContentPane().add(imagemJLabel);
		imagemJLabel.setBounds(0, 5, 200, 41);

		qntJTextField = new JTextField();
		getContentPane().add(qntJTextField);
		qntJTextField.setText("Quantidade de Mensagens");
		qntJTextField.setBounds(30, 55, 154, 21);

		calcularJButton = new JButton();
		getContentPane().add(calcularJButton);

		calcularJButton.setText("Calcular");
		calcularJButton.setBounds(200, 55, 90, 21);
		calcularJButton.requestFocusInWindow();

		resultJLabel = new JLabel();
		getContentPane().add(resultJLabel);
		resultJLabel.setText(" Valor da Gravação");
		resultJLabel.setBounds(82, 90, 300, 14);

		ButtonHandler buttonHandler = new ButtonHandler();
		calcularJButton.addActionListener(buttonHandler);
		qntJTextField.addActionListener(buttonHandler);

		qntJTextField.addFocusListener(new textFieldHandler());

	}
ajfilho

Então é preciso dar uma lida na API para ver o que pode ter acontecido

Talvez seu componente não seja displayable… De acordo com um trecho da API do metodo requestFocusInWindow:

public boolean requestFocusInWindow()

Requests that this Component get the input focus, if this Component’s top-level ancestor is already the focused Window. This component must be displayable, focusable, visible and all of its ancestors (with the exception of the top-level Window) must be visible for the request to be granted. Every effort will be made to honor the request; however, in some cases it may be impossible to do so. Developers must never assume that this Component is the focus owner until this Component receives a FOCUS_GAINED event.

Estou tentando entender o que está acontecendo, mas dê uma pesquisada melhor nisso.

Abraços!

lina

Oi,

só mude a ordem da criação dos componentes…
crie o botão primeiro depois o textfield.

Estranho né?, mas funciona :wink:

Tchauzin

G

Caramba, não adiantou não...

Segue as classe de eventso..

class textFieldHandler implements FocusListener {

		public void focusGained(final FocusEvent event) {
			qntJTextField.setText("");
		}

		public void focusLost(final FocusEvent event) {
			// qntJTextField.setText("Quantidade de Mensagens");
		}

	}

	class ButtonHandler implements ActionListener {

		public void actionPerformed(final ActionEvent event) {
			int quantidade = 0;
			final Valores valor = new Valores();

			try {
				quantidade = Integer.parseInt(qntJTextField.getText());
				resultJLabel.setText(" R$ "
						+ valor.calcularGravacao(quantidade));
			}

			catch (final Exception e) {
				JOptionPane.showMessageDialog(null,
						"Digite o número de mensagens!", "Número Inválido",
						JOptionPane.ERROR_MESSAGE);
				resultJLabel.setText(" Valor da Gravação");
			}

		}
	}

vou ler a API!

G

Finalizando o tópico… (pq é horrivel quando eu procuro e nao tem solução!)…

Achei um post do emmanuel.silva onde teve esse problema e utilizou a solução abaixo:
http://www.guj.com.br/posts/list/58984.java#309968

Bug em Forum da Sun
http://forum.java.sun.com/thread.jspa?threadID=448376&messageID=2038261

addHierarchyListener(new HierarchyListener() { public void hierarchyChanged(HierarchyEvent e) { if ((e.getChangeFlags() & HierarchyEvent.SHOWING_CHANGED) != 0) { SwingUtilities.invokeLater(new Runnable() { public void run() { if (isVisible()) { calcularJButton.requestFocus(); } } }); } } });

Criado 11 de dezembro de 2008
Ultima resposta 15 de dez. de 2008
Respostas 6
Participantes 3