2012-04-22 51 views
1

对于一个任务,我需要做的JFormattedTextField有以下行为:按下时的JFormattedTextField,只确认进入

  • 如果值编辑,不等于最后的确认值的背景必须变成黄色。
  • 值验证可能发生在
  • 如果焦点没有任何损失应该发生(如果背景是黄色的,应该保持黄色,...)
  • 行动,应采取时按下回车键随时

我似乎无法找到听众完成此操作的正确组合。我尝试使用KeyAdapter,InputVerifierPropertyChangeListener但这给我非常难看的代码,只有80%的作品。

应该怎么做?

编辑:我写了一个小例子:

import java.awt.BorderLayout; 
import java.awt.Color; 
import java.awt.Container; 
import java.awt.event.ActionEvent; 
import java.awt.event.KeyAdapter; 
import java.awt.event.KeyEvent; 
import java.text.ParseException; 

import javax.swing.AbstractAction; 
import javax.swing.InputVerifier; 
import javax.swing.JButton; 
import javax.swing.JComponent; 
import javax.swing.JFormattedTextField; 
import javax.swing.JFrame; 
import javax.swing.JLabel; 
import javax.swing.JPanel; 

public class Test extends JPanel { 

    private JFormattedTextField field; 
    private JLabel label; 
    private JButton btn; 

    public Test() { 
     super(new BorderLayout()); 

     label = new JLabel("Enter a float value:"); 
     btn = new JButton(new AbstractAction("Print to stdout"){ 

      @Override 
      public void actionPerformed(ActionEvent e) { 
       System.out.println(field.getValue()); 
      } 

     }); 
     field = new JFormattedTextField(new Float(9.81)); 

     field.addKeyListener(new KeyAdapter(){ 

      @Override 
      public void keyPressed(KeyEvent e){ 
       field.setBackground(Color.YELLOW); 
      } 

      @Override 
      public void keyTyped(KeyEvent e){ 
       if(e.getKeyCode() == KeyEvent.VK_ENTER){ 
        try{ 
         field.commitEdit(); 
         field.setBackground(Color.WHITE); 
        }catch(ParseException e1){ 
         field.setBackground(Color.RED); 
        } 
       } 
      } 
     }); 

     field.setInputVerifier(new InputVerifier(){ 

      @Override 
      public boolean verify(JComponent comp) { 
       try{ 
        field.commitEdit(); 
        field.setBackground(Color.YELLOW); 
        return true; 
       }catch(ParseException e){ 
        field.setBackground(Color.RED); 
        return false; 
       } 
      } 

     }); 

     add(label, BorderLayout.NORTH); 
     add(field, BorderLayout.CENTER); 
     add(btn, BorderLayout.SOUTH); 
    } 


    public static void main(String[] args) { 
     JFrame window = new JFrame("InputVerifier test program"); 
     Container cp = window.getContentPane(); 
     cp.add(new Test()); 
     window.pack(); 
     window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     window.setVisible(true); 
    } 
} 

这几乎没有我想要的一切。但问题是ENTER键从未被捕获。我认为它在到达我的KeyListener之前就已经被使用了,但是我怎么能阻止这个呢?即使这是可以预防的,我仍然觉得应该有一个更清洁的为什么要完成上面的代码。

+0

您的意思是['InputVerifier'(HTTP://文档.oracle.com/JavaSE的/教程/ uiswing /其它/ focus.html#inputVerification)?请修改您的问题以包含一个[sscce](http://sscce.org/),其中显示了迄今为止您尝试的内容。 – trashgod 2012-04-22 09:55:20

+0

您是否可以进一步解释此行“如果编辑了值并且不等于最后验证的值”,这是否意味着“JFormattedTextField”只能取得一个合法值。除此之外,即使有效,也必须引发颜色变化。 – 2012-04-22 10:42:45

+0

我的意思是以下内容。假设该字段包含'9.81'。当用户键入'9.811'后跟退格键(因此该字段现在包含'9.81')时,该字段不应改变颜色。 – 2012-04-22 10:45:31

回答

4

试试你的手在此代码示例,告诉我这是所期望的行为,或者你希望别的东西,除了这一点:

import java.awt.*; 
import java.awt.event.*; 
import java.text.NumberFormat; 
import javax.swing.*; 
import javax.swing.event.CaretEvent; 
import javax.swing.event.CaretListener; 

public class JFormattedExample 
{ 
    private String lastValidValue; 

    private void createAndDisplayGUI() 
    { 
     JFrame frame = new JFrame("JFormattedTextField Example"); 
     frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); 

     JPanel contentPane = new JPanel(); 
     contentPane.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5)); 

     final JFormattedTextField ftf = new JFormattedTextField(
          NumberFormat.getNumberInstance()); 
     ftf.setColumns(10); 
     ftf.setFocusLostBehavior(JFormattedTextField.PERSIST); 
     ftf.setValue(100); 
     lastValidValue = "100"; 
     ftf.addCaretListener(new CaretListener() 
     { 
      public void caretUpdate(CaretEvent ce) 
      { 
       System.out.println("Last Valid Value : " + lastValidValue); 
       if (ftf.isEditValid()) 
       { 
        String latestValue = ftf.getText(); 
        System.out.println("Latest Value : " + latestValue); 
        if (!(latestValue.equals(lastValidValue))) 
         ftf.setBackground(Color.YELLOW.darker()); 
        else 
        { 
         lastValidValue = ftf.getText(); 
         ftf.setBackground(Color.WHITE); 
        } 
       } 
       else 
       { 
        System.out.println("Invalid Edit Entered."); 
       } 
      } 
     }); 

     contentPane.add(ftf); 
     frame.setContentPane(contentPane); 
     frame.pack(); 
     frame.setLocationByPlatform(true); 
     frame.setVisible(true); 
    } 

    public static void main(String... args) 
    { 
     SwingUtilities.invokeLater(new Runnable() 
     { 
      public void run() 
      { 
       new JFormattedExample().createAndDisplayGUI(); 
      } 
     }); 
    } 
} 
相关问题