我想输入一个带有掩码数字的JFormattedTextfield,用于从右向左插入时间单位(min:sec和掩码##:##)。我用SetComponentOrientation来完成工作。唯一的情况是,当我连续键入3位数字(即1、2、3)时,JFormattedTextfield将显示12:3而不是1:23。这个问题已经在以下问题中讨论过了,但没有得到满意的回答:Swing - MaskFormatter - Enter Numbers from Right side of the textfield
我已经用我自己的PlainDocument子类尝试过了,但是并没有真正深入到使用insert方法的解决方案。有谁想过如何用MaskFormatter或其他方法解决这个问题吗?
如果能得到一些提示,听到其中一个人的意见,那就太好了。提前谢谢。
发布于 2017-10-19 14:49:58
下面是使用自定义DocumentFilter的自定义文本字段,该字段应该会有所帮助:
import java.awt.*;
import java.text.*;
import javax.swing.*;
import javax.swing.text.*;
public class ABMTextField extends JTextField
{
private DecimalFormat format;
private String decimal;
public ABMTextField(DecimalFormat format)
{
this.format = format;
decimal = Character.toString( format.getDecimalFormatSymbols().getDecimalSeparator() );
setColumns( format.toPattern().length() );
setHorizontalAlignment(JFormattedTextField.TRAILING);
setText( format.format(0.0) );
AbstractDocument doc = (AbstractDocument)getDocument();
doc.setDocumentFilter( new ABMFilter() );
}
@Override
public void setText(String text)
{
Number number = format.parse(text, new ParsePosition(0));
if (number != null)
super.setText( text );
}
public class ABMFilter extends DocumentFilter
{
public void insertString(FilterBypass fb, int offs, String str, AttributeSet a)
throws BadLocationException
{
replace(fb, offs, 0, str, a);
}
public void replace(FilterBypass fb, int offs, int length, String str, AttributeSet a)
throws BadLocationException
{
if (".0123456789".contains(str))
{
Document doc = fb.getDocument();
StringBuilder sb = new StringBuilder( doc.getText(0, doc.getLength()) );
int decimalOffset = sb.indexOf( decimal );
if (decimalOffset != -1)
{
sb.deleteCharAt(decimalOffset);
sb.insert(decimalOffset + 1, decimal);
}
sb.append(str);
try
{
String text = format.format( format.parse( sb.toString() ) );
super.replace(fb, 0, doc.getLength(), text, a);
}
catch(ParseException e) {}
}
else
Toolkit.getDefaultToolkit().beep();
}
public void remove(DocumentFilter.FilterBypass fb, int offset, int length)
throws BadLocationException
{
Document doc = fb.getDocument();
StringBuilder sb = new StringBuilder( doc.getText(0, doc.getLength()) );
int decimalOffset = sb.indexOf( decimal );
if (decimalOffset != -1)
{
sb.deleteCharAt(decimalOffset);
sb.insert(decimalOffset - 1, decimal);
}
sb.deleteCharAt( sb.length() - 1) ;
try
{
String text = format.format( format.parse( sb.toString() ) );
super.replace(fb, 0, doc.getLength(), text, null);
}
catch(ParseException e) {}
}
}
private static void createAndShowUI()
{
DecimalFormat format = new DecimalFormat("###,##0.00");
// DecimalFormat format = new DecimalFormat("0000000000");
ABMTextField abm = new ABMTextField( format );
JPanel panel = new JPanel();
panel.add( abm );
JFrame frame = new JFrame("ABMTextField");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add( panel );
frame.setSize(200, 200);
frame.setLocationByPlatform( true );
frame.setVisible( true );
}
public static void main(String[] args)
{
EventQueue.invokeLater(new Runnable()
{
public void run()
{
createAndShowUI();
}
});
}
}https://stackoverflow.com/questions/46831070
复制相似问题