2011-09-11 10 views
8

enter image description hereenter image description hereJFormattedTextField emette

1) Come posso impostare il cursore a 0 possition senza utilizzare Caret o messa a fuoco avvolti in invokeLater() (confortly può essere risolto utilizzando @camickr Formatted Text Field Tips), c'è qualcuno che conosce un altro modo

enter image description hereenter image description here

2) How to reset Formatter volte (sollevando focus di TAB dalla tastiera), reset non funziona e su focusLost (campo vuoto) Formatter ritorni/caratteri reincarnati o String posteriore (ultima sapere prima di setText ("");),

Nota: conoscere il codice o il codice seguente is only this way, su come reimpostare Formatter da OTN, ma i loro terribili criteri di ricerca ...., solo codice (domanda o risposta di 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; 
    } 

    protected void maybeCommitEdit(Document document) { 
     try { 
      field.commitEdit(); 
     } catch (ParseException e) { 
      // uncomment to map empty string to null 
      if (field.getText().length() == 0) { 
       field.setValue(null); 
      } 
     } 
    } 

    /*public void commitEdit() throws ParseException { 
    if(allowsNull() && isBlank()) { 
    setValue(null); 
    } 
    else { 
    super.commitEdit(); 
    } 
    }*/ 
    private PropertyChangeListener getPropertyChangeListener() { 
     PropertyChangeListener propertyChangeListener = new PropertyChangeListener() { 

      @Override 
      public void propertyChange(PropertyChangeEvent evt) { 
       if ("value".equals(evt.getPropertyName())) { 
        matchValueChanged(evt.getNewValue()); 
       } 
      } 
     }; 
     return propertyChangeListener; 
    } 

    protected void matchValueChanged(Object value) { 
     System.out.println("got new value: " + value); 
    } 

    private DocumentListener getDocumentListener() { 
     DocumentListener documentListener = new DocumentListener() { 

      @Override 
      public void removeUpdate(DocumentEvent e) { 
       maybeCommitEdit(e.getDocument()); 
      } 

      @Override 
      public void insertUpdate(DocumentEvent e) { 
       maybeCommitEdit(e.getDocument()); 
      } 

      @Override 
      public void changedUpdate(DocumentEvent e) { 
      } 
     }; 
     return documentListener; 
    } 

    public static void main(String[] args) { 
     SwingUtilities.invokeLater(new Runnable() { 

      @Override 
      public void run() { 
       JFrame frame = new JFrame(""); 
       frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
       frame.add(new FormattedNull().createContent()); 
       frame.setLocationRelativeTo(null); 
       frame.pack(); 
       frame.setVisible(true); 
      } 
     }); 
    } 
} 

enter image description here

immagini allegate si basano sulla mia sscce

import java.awt.GridLayout; 
import java.awt.event.ActionEvent; 
import javax.swing.*; 
import javax.swing.text.MaskFormatter; 

public class TestTest { 

    private JFormattedTextField myFormattedField1 = new JFormattedTextField(createFormatter("AAAAAAAAAAAA")); 
    private JFormattedTextField myFormattedField2 = new JFormattedTextField(createFormatter("AAAAAAAAAAAA")); 
    private JFormattedTextField myFormattedField3 = new JFormattedTextField(createFormatter("AAAAAAAAAAAA")); 
    private JFormattedTextField myFormattedField4 = new JFormattedTextField(createFormatter("AAAAAAAAAAAA")); 
    private JButton jb = new JButton("Reset to Default"); 
    private JFrame frame = new JFrame("Text Test"); 

    public TestTest() { 
     myFormattedField1.setText("ABCDEFGHIJKL"); 
     myFormattedField2.setText("ABCDEFGHIJKL"); 
     myFormattedField3.setText("ABCDEFGHIJKL"); 
     myFormattedField4.setText("ABCDEFGHIJKL"); 

     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     frame.setLocation(150, 150); 
     frame.setLayout(new GridLayout(5, 0)); 
     frame.add(jb); 
     frame.add(myFormattedField1); 
     frame.add(myFormattedField2); 
     frame.add(myFormattedField3); 
     frame.add(myFormattedField4); 
     jb.addActionListener(new java.awt.event.ActionListener() { 

      @Override 
      public void actionPerformed(ActionEvent e) { 
       myFormattedField1.setText(""); 
       myFormattedField2.setText(""); 
       myFormattedField3.setText(""); 
       myFormattedField4.setText(""); 
      } 
     }); 
     frame.pack(); 
     frame.setVisible(true); 
    } 

    public static void main(String[] args) { 
     TestTest textTest = new TestTest(); 
    } 

    protected MaskFormatter createFormatter(String s) { 
     MaskFormatter formatter = null; 
     try { 
      formatter = new MaskFormatter(s); 
     } catch (java.text.ParseException exc) { 
      System.err.println("formatter is bad: " + exc.getMessage()); 
     } 
     return formatter; 
    } 
} 
+1

1) Perché? 2) stringa vuota è una voce non valida, quindi il campo ritorna alla maschera stessa (imposta un placeHolderChar per vedere, gli spazi sono invisibili :-) – kleopatra

risposta

8
  1. In Mac OS, il comportamento predefinito del delegato UI, com.apple.laf.AquaTextFieldF, è simile a CaretPositionListener:

    • Tab o Maiusc - Tab: posto il cursore all'inizio del campo.

    • Fare clic su: posizionare brevemente il punto di inserimento all'inizio del campo e quindi spostarlo sul punto di clic.

    IMO, l'CaretPositionListener fa la seconda molto più agevolmente.

  2. Il comportamento predefinito di JFormattedTextField in caso di perdita di messa a fuoco è COMMIT_OR_REVERT. Nell'esempio seguente, Scheda sebbene il primo, campo non valido per vedere l'effetto. Il formattatore non può né commettere né ripristinare il valore non valido, quindi sostituisce gli spazi MASK.length(). Questo valore è valido, se un po 'oscuro.

Addendum: Ho aggiornato il codice qui sotto per consentire la modifica della setFocusLostBehavior().

Codice:

import java.awt.GridLayout; 
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 
import javax.swing.*; 
import javax.swing.text.MaskFormatter; 

/** @see http://stackoverflow.com/questions/7378821 */ 
public class TrashTest { 

    private static final String MASK = "########"; 
    private static final String DEFAULT = ""; 
    private static final String BOGUS = ""; 
    private JFormattedTextField jtf1 = createField(); 
    private JFormattedTextField jtf2 = createField(); 
    private JFormattedTextField jtf3 = createField(); 
    private JFormattedTextField jtf4 = createField(); 
    private JButton reset = new JButton("Reset to Default"); 
    private JComboBox combo = new JComboBox(); 
    private JFrame frame = new JFrame("Text Test"); 

    public TrashTest() { 
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     frame.setLocation(150, 150); 
     frame.setLayout(new GridLayout(0, 1)); 
     frame.add(reset); 
     frame.add(jtf1); 
     frame.add(jtf2); 
     frame.add(jtf3); 
     frame.add(jtf4); 
     frame.add(combo); 
     this.initFields(); 
     reset.addActionListener(new ActionListener() { 

      @Override 
      public void actionPerformed(ActionEvent e) { 
       initFields(); 
      } 
     }); 
     for (Edit e : Edit.values()) { 
      combo.addItem(e); 
     } 
     combo.setSelectedIndex(jtf1.getFocusLostBehavior()); 
     combo.addActionListener(new ActionListener() { 

      @Override 
      public void actionPerformed(ActionEvent e) { 
       Edit current = (Edit) combo.getSelectedItem(); 
       jtf1.setFocusLostBehavior(current.value); 
      } 
     }); 
     frame.pack(); 
     frame.setVisible(true); 
    } 

    private void initFields() { 
     jtf1.setText(BOGUS); 
     jtf2.setText(DEFAULT); 
     jtf3.setText(DEFAULT); 
     jtf4.setText(DEFAULT); 
    } 

    protected JFormattedTextField createField() { 
     MaskFormatter formatter = null; 
     try { 
      formatter = new MaskFormatter(MASK); 
     } catch (java.text.ParseException e) { 
      e.printStackTrace(System.out); 
     } 
     JFormattedTextField jtf = new JFormattedTextField(formatter); 
     return jtf; 
    } 

    enum Edit { 

     COMMIT(JFormattedTextField.COMMIT), 
     COMMIT_OR_REVERT(JFormattedTextField.COMMIT_OR_REVERT), 
     REVERT(JFormattedTextField.REVERT), 
     PERSIST(JFormattedTextField.PERSIST); 
     private int value; 

     private Edit(int n) { 
      this.value = n; 
     } 

     public static Edit getEnum(int n) { 
      for (Edit e : Edit.values()) { 
       if (e.value == n) { 
        return e; 
       } 
      } 
      return Edit.COMMIT_OR_REVERT; 
     } 
    } 

    public static void main(String[] args) { 
     TrashTest textTest = new TrashTest(); 
    } 
} 
+0

Questa risposta è piuttosto empirica; Sarei felice di ricevere eventuali correzioni o una risposta migliore. – trashgod

+0

grazie per la risposta, hackish :-) – mKorbel

+0

Per riferimento, ho aggiornato il codice per consentire la selezione dell'impostazione di messa a fuoco persa. – trashgod