Cara eu testei aki mas nao funcionou, da uma olha do codigo de teste que eu fiz…
Tens mais alguma sugestão??
Obrigado…
package textFieldValue;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.text.NumberFormat;
import java.util.Locale;
import javax.swing.JFormattedTextField;
import javax.swing.text.DefaultFormatterFactory;
import javax.swing.text.JTextComponent;
import javax.swing.text.NumberFormatter;
public class NewJFrame extends javax.swing.JFrame{
/** Creates new form NewJFrame */
public NewJFrame() {
initComponents();
formatValue();
txfValue.addFocusListener(this.klUpCase);
}
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
btnOK = new javax.swing.JButton();
txfValue = new javax.swing.JFormattedTextField();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
btnOK.setText("OK");
btnOK.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
btnOKActionPerformed(evt);
}
});
txfValue.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
txfValueActionPerformed(evt);
}
});
txfValue.addFocusListener(new java.awt.event.FocusAdapter() {
public void focusGained(java.awt.event.FocusEvent evt) {
txfValueFocusGained(evt);
}
});
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(76, 76, 76)
.addComponent(btnOK, javax.swing.GroupLayout.PREFERRED_SIZE, 103, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGroup(layout.createSequentialGroup()
.addGap(25, 25, 25)
.addComponent(txfValue, javax.swing.GroupLayout.PREFERRED_SIZE, 145, javax.swing.GroupLayout.PREFERRED_SIZE)))
.addContainerGap(221, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(23, 23, 23)
.addComponent(txfValue, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(112, 112, 112)
.addComponent(btnOK)
.addContainerGap(122, Short.MAX_VALUE))
);
pack();
}// </editor-fold>
private FocusListener klUpCase = new FocusListener(){
public void focusGained(FocusEvent e){
System.out.print("asdsadsads");
JFormattedTextField source = (JFormattedTextField) e.getSource();
source.selectAll();
}
public void focusLost(FocusEvent e){ }
};
private void btnOKActionPerformed(java.awt.event.ActionEvent evt) {
}
private void formatValue() {
// Formato de visualizacao
NumberFormat dispFormat = NumberFormat.getCurrencyInstance();
NumberFormat num = NumberFormat.getNumberInstance(Locale.getDefault());
num.setGroupingUsed(false);
NumberFormatter dnFormat = new NumberFormatter(dispFormat);
NumberFormatter enFormat = new NumberFormatter(num);
DefaultFormatterFactory currFactory = new DefaultFormatterFactory(dnFormat, dnFormat, enFormat);
txfValue.setFormatterFactory(currFactory);
}
private void txfValueActionPerformed(java.awt.event.ActionEvent evt) {
}
private void txfValueFocusGained(java.awt.event.FocusEvent evt) {
// verificar se o componente é do tipo JTextComponent, se for
// fazer com que todo o texto do componente fique selecionado
if (evt.getComponent() instanceof JTextComponent) {
// se for o componente for do tipo JTextComponent, entao
// criar uma instancia do objeto para manipular
JTextComponent texto = (JTextComponent)evt.getComponent();
// setar a posição na string, onde começara a seleção,
// neste caso quero desde do inicio
texto.setSelectionStart(0);
// setar a posição final na string, neste caso até o tamanho do texto
texto.setSelectionEnd(texto.getText().length());
}
}
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new NewJFrame().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JButton btnOK;
private javax.swing.JFormattedTextField txfValue;
// End of variables declaration
}