JFormattedTextField accepting different number of digits
I need a JTextField limiting to 3 decimal places. After a search, i discovered JFormattedTextField, which looks gr开发者_StackOverfloweat. The code right now is:
try
{
double aux = 25.4;
MaskFormatter mascara = new MaskFormatter("####.###");
final JFormattedTextField areaTextoXMin = new JFormattedTextField(mascara);
areaTextoXMin.setValue(aux);
}
catch (Exception e)
{
e.printStackTrace();
}
But if aux is not 4 integer digits and 3 decimal digits, it doesn't works, and the JFormattedTextField appears empty. Code would work is aux were 1000.000 for example.
Other ideas?
Try to use NumberFormat
e.g. DecimalFormat
and call setMinimumIntegerDigits()
and setMaximumIntegerDigits()
import javax.swing.*;
import javax.swing.text.*;
import java.util.regex.*;
public class MyFilter extends DocumentFilter
{
public void insertString(DocumentFilter.FilterBypass fb, int offset, String text, AttributeSet attr) throws BadLocationException
{
StringBuilder sb = new StringBuilder();
sb.append(fb.getDocument().getText(0, fb.getDocument().getLength()));
sb.insert(offset, text);
if(!containsOnlyNumbers(sb.toString())) return;
fb.insertString(offset, text, attr);
}
public void replace(DocumentFilter.FilterBypass fb, int offset, int length, String text, AttributeSet attr) throws BadLocationException
{
StringBuilder sb = new StringBuilder();
sb.append(fb.getDocument().getText(0, fb.getDocument().getLength()));
sb.replace(offset, offset + length, text);
if(!containsOnlyNumbers(sb.toString())) return;
fb.replace(offset, length, text, attr);
}
public boolean containsOnlyNumbers(String text)
{
Pattern pattern = Pattern.compile("\\d*(\\.\\d{0,3})?");
Matcher matcher = pattern.matcher(text);
boolean isMatch = matcher.matches();
return isMatch;
}
}
and then you can use it like:
JTextField txt = new TextField();
DocumentFilter onlyNumberFilter = new MyFilter();
((AbstractDocument)txt.getDocument()).setDocumentFilter(onlyNumberFilter);
maybe is there very simple solution for that, lets decimal places to infinity, restict that in view for example
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.math.RoundingMode;
import java.text.NumberFormat;
import java.util.Locale;
import javax.swing.*;
public class CurrencyFormaterTest {
private JLabel jl;
private JFormattedTextField jft, jft1, jft2, jft3, jft4;
private JButton jb = new JButton("but");
public CurrencyFormaterTest() {
JFrame frame = new JFrame("Text Test");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jl = new JLabel("Try with formatted text field ");
Locale local1 = new Locale("en", "US");
int minFra = 0;
int maxFra = 3;
jft = setFormat(jft, local1, minFra, maxFra);
//jft.setColumns(20);
jft.setMaximumSize(new Dimension(100, 20));
jft.setMinimumSize(new Dimension(100, 20));
jft.setPreferredSize(new Dimension(100, 20));
local1 = new Locale("gb", "GB");
minFra = 2;
maxFra = 2;
jft1 = setFormat(jft1, local1, minFra, maxFra);
//jft1.setColumns(20);
jft1.setMaximumSize(new Dimension(100, 20));
jft1.setMinimumSize(new Dimension(100, 20));
jft1.setPreferredSize(new Dimension(100, 20));
local1 = new Locale("de", "DE");
minFra = 0;
maxFra = 3;
jft2 = setFormat(jft2, local1, minFra, maxFra);
//jft2.setColumns(20);
jft2.setMaximumSize(new Dimension(100, 20));
jft2.setMinimumSize(new Dimension(100, 20));
jft2.setPreferredSize(new Dimension(100, 20));
local1 = new Locale("jp", "JP");
minFra = 0;
maxFra = 0;
jft3 = setFormat(jft3, local1, minFra, maxFra);
//jft3.setColumns(20);
jft3.setMaximumSize(new Dimension(100, 20));
jft3.setMinimumSize(new Dimension(100, 20));
jft3.setPreferredSize(new Dimension(100, 20));
local1 = new Locale("hk", "HK");
minFra = 0;
maxFra = 3;
jft4 = setFormat(jft4, local1, minFra, maxFra);
//jft4.setColumns(20);
jft4.setMaximumSize(new Dimension(100, 20));
jft4.setMinimumSize(new Dimension(100, 20));
jft4.setPreferredSize(new Dimension(100, 20));
frame.add(jb);
//
frame.add(jl);
frame.add(jft, BorderLayout.NORTH);
frame.add(jft1, BorderLayout.SOUTH);
frame.add(jft2, BorderLayout.WEST);
frame.add(jft3, BorderLayout.EAST);
frame.add(jft4, BorderLayout.CENTER);
jb.addActionListener(new java.awt.event.ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
JOptionPane.showMessageDialog(jb, "nel text Box : " + jft.getText());
}
});
frame.pack();
frame.setVisible(true);
}
public static void main(String[] args) {
CurrencyFormaterTest textTest = new CurrencyFormaterTest();
}
private JFormattedTextField setFormat(JFormattedTextField jft, Locale local1, int minFra, int maxFra) {
NumberFormat numberFormat;
Locale local = local1;
int setMin = minFra;
int setMax = maxFra;
numberFormat = NumberFormat.getCurrencyInstance(local);
numberFormat.setMinimumFractionDigits(setMin);
numberFormat.setMaximumFractionDigits(setMax);
numberFormat.setRoundingMode(RoundingMode.HALF_UP);
jft = new JFormattedTextField(numberFormat);
jft.setValue(new Double(342.796));
return jft;
}
}
精彩评论