essa classe faz perfeitamente o que preciso é um jtextfield aceitar somente numeros e ,(virgula)
package caixa;
import javax.swing.*;
import javax.swing.event.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.text.*;
public class jTextOnlyNumber extends JFrame{
JTextField txtJT;
public jTextOnlyNumber(){
setTitle("TextField somente numeros");
Container contentPane = getContentPane();
contentPane.setLayout(null);
txtJT = new JTextField();
txtJT.setDocument(new NumberDocument());
txtJT.setBounds(16, 24, 185, 25);
contentPane.add(txtJT);
setSize(250, 100);
setVisible(true);
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
public class NumberDocument extends PlainDocument{
public NumberDocument(){
super();
}
public void insertString(int offset, String str, AttributeSet attr) throws BadLocationException {
//permite apenas uma virgula
if(str.equals(",")){
if(txtJT.getText().indexOf(",")>-1)
return;
else
super.insertString(offset, str, attr);
}
if (str.codePointAt(0) < 48 || str.codePointAt(0) > 57) return;
super.insertString(offset, str, attr);
}
}
public static void main(String args[]){
jTextOnlyNumber jt = new jTextOnlyNumber();
}
}
minha classe
public class teste extends javax.swing.JFrame {
/** Creates new form teste */
public teste() {
initComponents();
}
private void jSair2ActionPerformed(java.awt.event.ActionEvent evt) {
this.dispose();
}
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new teste().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JButton jSair2;
private javax.swing.JTextField valor;
// End of variables declaration
}
quero coloca ela no meu jtextfield VALOR como posso fazer isso desde ja agradeço