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…
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
Tchauzin
G
guilherme.cq
Caramba, não adiantou não...
Segue as classe de eventso..
classtextFieldHandlerimplementsFocusListener{publicvoidfocusGained(finalFocusEventevent){qntJTextField.setText("");}publicvoidfocusLost(finalFocusEventevent){// qntJTextField.setText("Quantidade de Mensagens");}}classButtonHandlerimplementsActionListener{publicvoidactionPerformed(finalActionEventevent){intquantidade=0;finalValoresvalor=newValores();try{quantidade=Integer.parseInt(qntJTextField.getText());resultJLabel.setText(" R$ "+valor.calcularGravacao(quantidade));}catch(finalExceptione){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
guilherme.cq
Finalizando o tópico… (pq é horrivel quando eu procuro e nao tem solução!)…