在这个问题中有没有办法只接受JTextField中的数值?其中一个答案表明JFormattedTextField存在问题.
我还没有使用它,但有人可以扩展(或不同意)这个课程的问题吗?
基本上Swing JComponents能够以这种形式显示分数2 2/3
.如何以最好的形式绘制分数,例如2⅔?
.
编辑
.
因为看到我只有一个办法JTable
里面JSpinner
有一个TableColumn
和TableRow
(可能普通的模拟JtextField
太),其中TableRenderer
可能会有一些的JTextComponent
格式化使用HTML和TableCellEdit
事件TableEditor
,以SWITH到平原JFormattedTextField
,
还有另一种方式,是否有可能与平原J(Formatted)TextField
???
我的程序中有一个jFormattedTextField,当jFormattedTextField值有效更改时,我需要更新jLabel的文本.
实际上jFormattedTextField获取一个数字,jLabel在这个数字和另一个数字之间显示差异.
我目前通过听jFormatted文本的"FocusLost"事件来做到这一点.
我怎样才能做到这一点?
我的JFormattedTextField
物体上有两个物体JFrame
.我希望通过这些JFormattedTextField
对象的值得到基本的数学(加法).当焦点丢失第一个或第二个文本字段时,我希望它发生.但是当" focusLost()
",事件没有得到最后一个值时,它会得到之前的值.
例如; tf1
为tf2
0,最初为0.我写了2 tf1
,当when focusLost()
(tf1+tf2
)变为0时,当我改变其中任何一个时,结果变为2(前一个值)
如何获取focusLost上的最后一个值?
这是我的代码:
JFormattedTextField tf1,tf2;
NumberFormat format=NumberFormat.getNumberInstance();
tf1=new JFormattedTextField(format);
tf1.addFocusListener(this);
tf2=new JFormattedTextField(format);
tf2.addFocusListener(this);
Run Code Online (Sandbox Code Playgroud)
并且focusLost()
:
public void focusLost(FocusEvent e) {
if(tf1.getValue() == null) tf1.setValue(0);
if(tf2.getValue() == null) tf2.setValue(0);
//because if I dont set, it throws nullPointerException for tf.getValue()
BigDecimal no1 = new BigDecimal(tf1.getValue().toString());
BigDecimal no2 = new BigDecimal(tf2.getValue().toString());
System.out.println("total: " + (no1.add(no2)));
}
Run Code Online (Sandbox Code Playgroud)
1)如何将Cursor设置为0,而不使用Caret或Focus包装到invokeLater()(使用@camickr 格式文本字段提示可以解决),是否有人知道另一种方式
2)How to reset Formatter
有时(通过TAB从键盘提高Focus),重置不起作用,并且在focusLost(空字段)格式化程序返回/重新生成的字符或字符串返回(最后在setText("")之前知道;),
注意:知道代码或下面的代码is only this way
,关于如何从OTN重置Formatter,但是他们可怕的搜索rulles ....,只有代码(问题或答案由Jeanette ???)
import java.awt.event.*;
import java.beans.*;
import java.text.ParseException;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.text.Document;
public class FormattedNull {
private JFormattedTextField field;
private JButton focusButton;
private JComponent createContent() {
JComponent content = new JPanel();
field = new JFormattedTextField(new Integer(55));
field.setColumns(20);
field.addPropertyChangeListener(getPropertyChangeListener());
field.getDocument().addDocumentListener(getDocumentListener());
content.add(field);
focusButton = new JButton("just something focusable");
focusButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
field.setValue(0);
field.requestFocusInWindow();
}
});
content.add(focusButton);
return content;
} …
Run Code Online (Sandbox Code Playgroud) 我有JFormattedTextField
一个NumberFormat
与Locale.US.因此小数点分隔符是点,分组分隔符是逗号.
现在,我在此文本字段中键入字符串"1,23",并将焦点移动到另一个组件.我希望字符串消失(就像我输入"a"而不是"1,23"时那样),因为在使用Locale.US时它显然不是数字的有效表示.但相反,文本字段中的文本更改为"123".
这是因为NumberFormat
解析时使用的并不严格,只是忽略逗号.
问题:如何在这种情况下NumberFormat
抛出一个ParseException
,以便在将焦点移动到另一个组件后文本字段为空?
测试代码:
JDialog dialog = new JDialog();
JPanel panel = new JPanel(new BorderLayout());
dialog.getContentPane().add(panel);
NumberFormat nf = NumberFormat.getInstance(Locale.US);
JFormattedTextField textField = new JFormattedTextField(nf);
textField.setText("1,23");
panel.add(textField, BorderLayout.CENTER);
panel.add(new JButton("focus"), BorderLayout.EAST);
dialog.pack();
dialog.setVisible(true);
Run Code Online (Sandbox Code Playgroud)
将焦点从文本字段移动到按钮,文本将更改为"123".
我在我的程序中使用了一些JFormattedTextFields.由于某些原因,当单击文本字段后文本字段获得焦点时,插入符号位置始终跳转到左侧(位置0).我希望插入符号最终位于用户点击的位置.因此,如果我在两个数字之间点击,则插入符应该在这两个数字之间.
所以我实现了一个FocusListener,它将获得点击位置并在那里设置插入位置.
FocusListener focusListener = new FocusListener(){
public void focusGained(FocusEvent evt) {
JFormettedTextField jftf = (JFormattedTextField) evt.getSource();
//This is where the caret needs to be.
int dot = jftf.getCaret().getDot();
SwingUtilities.invokeLater( new Runnable() {
public void run() {
'the textField that has focus'.setCaretPosition('Some how get the evt or dot');
}
});
}
public void focusLost (FocusEvent evt) {}
});
Run Code Online (Sandbox Code Playgroud)
我尝试了很多让他上班的事情.我尝试过使用final关键字,但是只能用于单个文本字段.
我在焦点监听器中使用了set/get方法来分配当前对象,但我不确定如何使其"安全"(例如,它们是否需要同步?).
也许有一些我想念的东西?
让JFormattedTextField与我的自定义格式一起使用后,我感到非常沮丧,我想知道是否有一个Formatter
或FormatterFactory
那个使用正则表达式?
我的想法是,如果有一个,那么我可以将它包装在一个静态类中并像这样调用它:
mFormattedTextField.setFormatterFactory(
SomeStaticClass.getRegexFormatFactory("^(\\d{1,}h)(\\s([0-5])?[0-9]m)?$"));
Run Code Online (Sandbox Code Playgroud)
有关更多背景信息,请参阅我之前的问题:
"我想使用JFormattedTextField来允许用户在表单中输入持续时间值.示例有效值为:2h 30m
72h 15m
6h
0h
"
我正在尝试创建一个只接受24小时时间的JFormattedTextField.
我非常接近解决方案,但有一个案例,其中以下代码示例不起作用.
如果输入时间"222"并从字段中更改焦点,则时间将更正为"2202".我希望它只接受一个完整的4位数24小时时间.除了我刚刚提到的那个代码之外,这个代码几乎在所有情况下都能正常工作.有什么建议?
public static void main(String[] args) throws ParseException {
DateFormat dateFormat = new SimpleDateFormat("HHmm");
dateFormat.setLenient(false);
DateFormatter dateFormatter = new DateFormatter(dateFormat);
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JFormattedTextField textField = new JFormattedTextField(dateFormatter);
frame.add(textField, BorderLayout.NORTH);
frame.add(new JTextField("This is here so you can change focus."), BorderLayout.SOUTH);
frame.setSize(250, 100);
frame.setVisible(true);
}
Run Code Online (Sandbox Code Playgroud) 我发现很难清楚JFormattedTextField
s 的价值.怎么做?
我试过了
txtJFormattedTextField.setText("");
Run Code Online (Sandbox Code Playgroud)
但是当焦点再次失去时,我清除的价值就会出现.我阅读了有关此问题的API.
我为日期字段创建JTFormattedTextFields的工厂如下 -
public static JFormattedTextField createDateField() {
MaskFormatter maskFormatter = null;
try {
maskFormatter = new MaskFormatter("##/##/####");
} catch (ParseException e) {
e.printStackTrace();
}
JFormattedTextField formattedTextField = new JFormattedTextField(
maskFormatter);
SwingHelper.installDateValidation((AbstractDocument) formattedTextField
.getDocument());
formattedTextField.setColumns(10);
return formattedTextField;
}
Run Code Online (Sandbox Code Playgroud)
试过了
try {
((JFormattedTextField) txtSigninDate).commitEdit();
} catch (ParseException e) {
e.printStackTrace();
}
Run Code Online (Sandbox Code Playgroud)
结果是一个例外 - 下面是异常跟踪.
java.text.ParseException: stringToValue passed invalid value
at javax.swing.text.MaskFormatter.stringToValue(MaskFormatter.java:456)
at javax.swing.text.MaskFormatter.stringToValue(MaskFormatter.java:371)
at javax.swing.JFormattedTextField.commitEdit(JFormattedTextField.java:530)
at app.view.action.Authentication_Action.clearSigninFields(Authentication_Action.java:207)
at app.view.action.authentication.AuthenticationCommand.decorateSignout(AuthenticationCommand.java:285)
at app.view.action.authentication.AuthenticationCommand.executeSignin(AuthenticationCommand.java:122)
at app.view.action.Authentication_Action$2.actionPerformed(Authentication_Action.java:292)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2018)
Run Code Online (Sandbox Code Playgroud)