List of usage examples for javax.swing JFormattedTextField commitEdit
public void commitEdit() throws ParseException
AbstractFormatter
and set as the current value. From source file:CombinationFormatter.java
public static void main(String argv[]) { // a demo main() to show how CombinationFormatter could be used int comb1[] = { 35, 11, 19 }; int comb2[] = { 10, 20, 30 }; final JFormattedTextField field1 = new JFormattedTextField(new CombinationFormatter()); field1.setValue(comb1);/*from w w w .j av a2 s. c o m*/ final JFormattedTextField field2 = new JFormattedTextField(new CombinationFormatter()); field2.setValue(comb2); JPanel pan = new JPanel(); pan.add(new JLabel("Change the combination from")); pan.add(field1); pan.add(new JLabel("to")); pan.add(field2); JButton b = new JButton("Submit"); b.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent ae) { try { field1.commitEdit(); // make sure current edit (if any) gets // committed field2.commitEdit(); } catch (java.text.ParseException pe) { } int oldc[] = (int[]) field1.getValue(); int newc[] = (int[]) field2.getValue(); // // code to validate oldc[] and change to newc[] goes here // } }); pan.add(b); JFrame f = new JFrame("CombinationFormatter Demo"); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); f.setContentPane(pan); f.setSize(360, 100); f.setVisible(true); }
From source file:MainClass.java
public static JPanel demo1() { JPanel pan = new JPanel(new BorderLayout()); pan.setBorder(new TitledBorder("change format midstream")); MaskFormatter lowercase = null; try {/*from w w w. jav a 2 s . c om*/ lowercase = new MaskFormatter("LLLL"); } catch (ParseException pe) { } final JFormattedTextField field = new JFormattedTextField(lowercase); field.setValue("lower case"); pan.add(field, BorderLayout.CENTER); final JButton change = new JButton("change format"); JPanel changePanel = new JPanel(); changePanel.add(change); pan.add(changePanel, BorderLayout.SOUTH); change.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent ae) { try { field.commitEdit(); MaskFormatter uppercase = new MaskFormatter("UUUU"); DefaultFormatterFactory factory = new DefaultFormatterFactory(uppercase); field.setFormatterFactory(factory); change.setEnabled(false); } catch (ParseException pe) { } } }); return pan; }
From source file:FactoryDemo.java
public static JPanel demo2() { // Demo 2: Change the format of a field when the user presses a button. // We can't call field.setFormatter() because that's a protected method. // (Plus it wouldn't work anyway. The old factory would replace our new // formatter with an "old" one next time the field gains or loses // focus.)/*from w w w . j a va2 s . c om*/ // The thing to do is send a new factory to field.setFormatterFactory(). JPanel pan = new JPanel(new BorderLayout()); pan.setBorder(new TitledBorder("Demo 2: change format midstream")); MaskFormatter lowercase = null; try { lowercase = new MaskFormatter("LLLL"); } catch (ParseException pe) { } final JFormattedTextField field = new JFormattedTextField(lowercase); field.setValue("Fore"); pan.add(field, BorderLayout.CENTER); final JButton change = new JButton("change format"); JPanel changePanel = new JPanel(); changePanel.add(change); pan.add(changePanel, BorderLayout.SOUTH); change.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent ae) { try { field.commitEdit(); // commit current edit (if any) MaskFormatter uppercase = new MaskFormatter("UUUU"); DefaultFormatterFactory factory = new DefaultFormatterFactory(uppercase); field.setFormatterFactory(factory); change.setEnabled(false); } catch (ParseException pe) { } } }); return pan; }
From source file:com.anrisoftware.prefdialog.miscswing.validatingfields.ValidatingFormattedTextField.java
/** * Verifies the field./* w w w . ja v a2s . co m*/ * * @param verifier * the {@link InputVerifier} that verifiers the input. * * @param input * the {@link JTextFormattedField}. * * @return {@code true} if the input is valid and the focus can be yield. */ protected boolean verifyField(InputVerifier verifier, JFormattedTextField input) { try { input.commitEdit(); return verifier.verify(input); } catch (ParseException e) { return false; } }
From source file:components.IntegerEditor.java
public boolean stopCellEditing() { JFormattedTextField ftf = (JFormattedTextField) getComponent(); if (ftf.isEditValid()) { try {//from w w w. ja v a 2s . c o m ftf.commitEdit(); } catch (java.text.ParseException exc) { } } else { //text is invalid if (!userSaysRevert()) { //user wants to edit return false; //don't let the editor go away } } return super.stopCellEditing(); }
From source file:com.haulmont.cuba.desktop.sys.vcl.DatePicker.DatePicker.java
@Override public void setEditor(final JFormattedTextField editor) { final int ENTER_CODE = 10; editor.addKeyListener(new KeyAdapter() { @Override/*from ww w . j a v a2 s.c om*/ public void keyTyped(KeyEvent e) { if (e.getKeyChar() == '\u007F' && editor.getCaretPosition() < format.length()) { editor.setCaretPosition(editor.getCaretPosition() + 1); } } @Override public void keyPressed(KeyEvent event) { if (ENTER_CODE == event.getKeyCode()) try { editor.commitEdit(); } catch (ParseException e) { // } } }); editor.addFocusListener(new FocusAdapter() { @Override public void focusLost(FocusEvent e) { editor.setCaretPosition(0); } }); super.setEditor(editor); if (format == null) { setFormats( Datatypes.getFormatStrings(AppBeans.get(UserSessionSource.class).getLocale()).getDateFormat()); } else setFormats(format); }
From source file:com.googlecode.vfsjfilechooser2.accessories.connection.ConnectionDialog.java
private void initListeners() { this.portTextField.addKeyListener(new KeyAdapter() { @Override//www.j a va 2s .co m public void keyTyped(KeyEvent e) { char c = e.getKeyChar(); if (!((Character.isDigit(c) || (c == KeyEvent.VK_BACK_SPACE) || (c == KeyEvent.VK_DELETE)))) { getToolkit().beep(); e.consume(); } else { setPortTextFieldDirty(true); } } }); this.portTextField.addFocusListener(new FocusAdapter() { @Override public void focusLost(FocusEvent e) { JFormattedTextField f = (JFormattedTextField) e.getSource(); String text = f.getText(); if (text.length() == 0) { f.setValue(null); } try { f.commitEdit(); } catch (ParseException exc) { } } }); this.cancelButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { if (currentWorker != null) { if (currentWorker.isAlive()) { currentWorker.interrupt(); setCursor(Cursor.getDefaultCursor()); } } setVisible(false); } }); this.connectButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { currentWorker = new Thread() { @Override public void run() { StringBuilder error = new StringBuilder(); FileObject fo = null; setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR)); try { String m_username = usernameTextField.getText(); String m_defaultRemotePath = defaultRemotePathTextField.getText(); char[] m_password = passwordTextField.getPassword(); String m_hostname = hostnameTextField.getText(); String m_protocol = protocolList.getSelectedItem().toString(); int m_port = -1; if (portTextField.isEditValid() && (portTextField.getValue() != null)) { String s = portTextField.getValue().toString(); m_port = Integer.valueOf(s); } Builder credentialsBuilder = Credentials.newBuilder(m_hostname) .defaultRemotePath(m_defaultRemotePath).username(m_username) .password(m_password).protocol(m_protocol).port(m_port); Credentials credentials = credentialsBuilder.build(); String uri = credentials.toFileObjectURL(); if (isInterrupted()) { setPortTextFieldDirty(false); return; } fo = VFSUtils.resolveFileObject(uri); if ((fo != null) && !fo.exists()) { fo = null; } } catch (Exception err) { error.append(err.getMessage()); setCursor(Cursor.getDefaultCursor()); } if ((error.length() > 0) || (fo == null)) { error.delete(0, error.length()); error.append("Failed to connect!"); error.append("\n"); error.append("Please check parameters and try again."); JOptionPane.showMessageDialog(ConnectionDialog.this, error, "Error", JOptionPane.ERROR_MESSAGE); setCursor(Cursor.getDefaultCursor()); return; } if (isInterrupted()) { return; } fileChooser.setCurrentDirectoryObject(fo); setCursor(Cursor.getDefaultCursor()); resetFields(); if (bookmarksDialog != null) { String bTitle = fo.getName().getBaseName(); if (bTitle.trim().equals("")) { bTitle = fo.getName().toString(); } String bURL = fo.getName().getURI(); bookmarksDialog.getBookmarks().add(new TitledURLEntry(bTitle, bURL)); bookmarksDialog.getBookmarks().save(); } setVisible(false); } }; currentWorker.setPriority(Thread.MIN_PRIORITY); currentWorker.start(); } }); // add the usual right click popup menu(copy, paste, etc.) PopupHandler.installDefaultMouseListener(hostnameTextField); PopupHandler.installDefaultMouseListener(portTextField); PopupHandler.installDefaultMouseListener(usernameTextField); PopupHandler.installDefaultMouseListener(passwordTextField); PopupHandler.installDefaultMouseListener(defaultRemotePathTextField); this.protocolList.addItemListener(new ItemListener() { public void itemStateChanged(ItemEvent e) { if (e.getStateChange() == ItemEvent.SELECTED) { selectPortNumber(); } } }); this.protocolList.setSelectedItem(Protocol.FTP); }
From source file:net.sf.vfsjfilechooser.accessories.connection.ConnectionDialog.java
private void initListeners() { this.portTextField.addKeyListener(new KeyAdapter() { @Override//from ww w. j a v a 2 s . c o m public void keyTyped(KeyEvent e) { char c = e.getKeyChar(); if (!((Character.isDigit(c) || (c == KeyEvent.VK_BACK_SPACE) || (c == KeyEvent.VK_DELETE)))) { getToolkit().beep(); e.consume(); } else { setPortTextFieldDirty(true); } } }); this.portTextField.addFocusListener(new FocusAdapter() { @Override public void focusLost(FocusEvent e) { JFormattedTextField f = (JFormattedTextField) e.getSource(); String text = f.getText(); if (text.length() == 0) { f.setValue(null); } try { f.commitEdit(); } catch (ParseException exc) { } } }); this.cancelButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { if (currentWorker != null) { if (currentWorker.isAlive()) { currentWorker.interrupt(); setCursor(Cursor.getDefaultCursor()); } } setVisible(false); } }); this.connectButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { currentWorker = new Thread() { @Override public void run() { StringBuilder error = new StringBuilder(); FileObject fo = null; setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR)); try { String m_username = usernameTextField.getText(); String m_defaultRemotePath = defaultRemotePathTextField.getText(); char[] m_password = passwordTextField.getPassword(); String m_hostname = hostnameTextField.getText(); String m_protocol = protocolList.getSelectedItem().toString(); int m_port = -1; if (portTextField.isEditValid() && (portTextField.getValue() != null)) { String s = portTextField.getValue().toString(); m_port = Integer.valueOf(s); } Builder credentialsBuilder = Credentials.newBuilder(m_hostname) .defaultRemotePath(m_defaultRemotePath).username(m_username) .password(m_password).protocol(m_protocol).port(m_port); Credentials credentials = credentialsBuilder.build(); String uri = credentials.toFileObjectURL(); if (isInterrupted()) { setPortTextFieldDirty(false); return; } fo = VFSUtils.resolveFileObject(uri); if ((fo != null) && !fo.exists()) { fo = null; } } catch (Exception err) { error.append(err.getMessage()); setCursor(Cursor.getDefaultCursor()); } if ((error.length() > 0) || (fo == null)) { error.delete(0, error.length()); error.append("Failed to connect!"); error.append("\n"); error.append("Please check parameters and try again."); JOptionPane.showMessageDialog(ConnectionDialog.this, error, "Error", JOptionPane.ERROR_MESSAGE); setCursor(Cursor.getDefaultCursor()); return; } if (isInterrupted()) { return; } fileChooser.setCurrentDirectory(fo); setCursor(Cursor.getDefaultCursor()); resetFields(); if (bookmarksDialog != null) { String bTitle = fo.getName().getBaseName(); if (bTitle.trim().equals("")) { bTitle = fo.getName().toString(); } String bURL = fo.getName().getURI(); bookmarksDialog.getBookmarks().add(new TitledURLEntry(bTitle, bURL)); bookmarksDialog.getBookmarks().save(); } setVisible(false); } }; currentWorker.setPriority(Thread.MIN_PRIORITY); currentWorker.start(); } }); // add the usual right click popup menu(copy, paste, etc.) PopupHandler.installDefaultMouseListener(hostnameTextField); PopupHandler.installDefaultMouseListener(portTextField); PopupHandler.installDefaultMouseListener(usernameTextField); PopupHandler.installDefaultMouseListener(passwordTextField); PopupHandler.installDefaultMouseListener(defaultRemotePathTextField); this.protocolList.addItemListener(new ItemListener() { public void itemStateChanged(ItemEvent e) { if (e.getStateChange() == ItemEvent.SELECTED) { selectPortNumber(); } } }); this.protocolList.setSelectedItem(Protocol.FTP); }