List of usage examples for javax.swing JFormattedTextField getText
public String getText()
TextComponent
. From source file:Main.java
public static void main(String[] args) throws Exception { MaskFormatter formatter = new MaskFormatter("###-##-####"); JFormattedTextField tf = new JFormattedTextField(formatter); JPanel panel = new JPanel(new BorderLayout()); panel.add(tf, BorderLayout.NORTH); JButton clickBtn = new JButton("Click me!"); clickBtn.addActionListener(e -> { if (!tf.getText().matches(formatter.getMask())) { System.err.println("Your Input does not match the pattern!"); } else {/*www.j ava 2s . c o m*/ System.out.println(tf.getText()); } }); panel.add(clickBtn, BorderLayout.SOUTH); JFrame f = new JFrame(); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); f.getContentPane().add(panel, BorderLayout.CENTER); f.setSize(800, 600); f.setVisible(true); }
From source file:Main.java
public static void main(String[] args) throws Exception { final MaskFormatter formatter = new TimeFormatter(); formatter.setValueClass(java.util.Date.class); final JFormattedTextField tf2 = new JFormattedTextField(formatter); tf2.setValue(new Date()); final JLabel label = new JLabel(); JButton bt = new JButton("Show Value"); bt.addActionListener(e -> {/*ww w. j av a 2 s . c o m*/ System.out.println(" value 2 = " + tf2.getValue()); System.out.println(" value 2 = " + tf2.getText()); System.out.println("value class: " + formatter.getValueClass()); label.setText(tf2.getText()); }); JFrame f = new JFrame(); f.getContentPane().setLayout(new GridLayout()); f.getContentPane().add(tf2); f.getContentPane().add(label); f.getContentPane().add(bt); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); f.pack(); f.setVisible(true); }
From source file:Main.java
public static void main(String... args) { JFrame frame = new JFrame(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); JPanel contentPane = new JPanel(); JFormattedTextField ftf = new JFormattedTextField(NumberFormat.getNumberInstance()); ftf.setColumns(10);//from w ww . j a v a 2 s . co m ftf.setFocusLostBehavior(JFormattedTextField.PERSIST); ftf.setValue(100); lastValidValue = "100"; ftf.addCaretListener(e -> { 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.setVisible(true); }
From source file:br.com.itfox.utils.Utils.java
public static String formatCpfCnpj(String value) { String maskCpf = "###.###.###-##"; String maskCnpj = "##.###.###/####-##"; String maskCep = "##.###-###"; String mask = ""; if (value == null) { return ""; }//w ww . jav a2 s . c om if (value.length() == 14) { mask = maskCnpj; } else if (value.length() == 11) { mask = maskCpf; } else if (value.length() == 8) { mask = maskCep; } try { MaskFormatter formatter = new MaskFormatter(mask); JFormattedTextField textField = new JFormattedTextField(); formatter.install(textField); textField.setText(value); value = textField.getText(); } catch (Exception e) { e.printStackTrace(); } return value; }
From source file:br.com.itfox.utils.Utils.java
public static String formatTelephone(String value) { String maskTel = "####-####"; String maskCel = "#####-####"; String maskDDD = "(##) "; String mask = ""; if (value == null) { return ""; }/* ww w. j a v a 2 s. c o m*/ if (value.length() == 9) { mask = maskCel; } else if (value.length() == 8) { mask = maskTel; } else if (value.length() == 2) { mask = maskDDD; } try { MaskFormatter formatter = new MaskFormatter(mask); JFormattedTextField textField = new JFormattedTextField(); formatter.install(textField); textField.setText(value); value = textField.getText(); } catch (Exception e) { e.printStackTrace(); } return value; }
From source file:Main.java
private InputVerifier getInputVerifier() { InputVerifier verifier = new InputVerifier() { @Override/*from w w w. j a v a 2s. co m*/ public boolean verify(JComponent input) { JFormattedTextField field = (JFormattedTextField) input; String text = field.getText(); return isValidDate(text); } @Override public boolean shouldYieldFocus(JComponent input) { boolean valid = verify(input); if (!valid) { JOptionPane.showMessageDialog(null, "Please enter a valid date in format dd/mm/yyyy"); } return valid; } }; return verifier; }
From source file:com.alvermont.terraj.util.ui.FormattedTextFieldVerifier.java
/** * Called to verify the state of the component * * @param input The input component/*from w ww . j a va2s. c o m*/ * @return <pre>true</pre> if the input is in a valid state for the component */ public boolean verify(JComponent input) { if (input instanceof JFormattedTextField) { final JFormattedTextField ftf = (JFormattedTextField) input; final AbstractFormatter formatter = ftf.getFormatter(); if (formatter != null) { final String text = ftf.getText(); try { formatter.stringToValue(text); return true; } catch (ParseException pe) { // not a valid value return false; } } } return true; }
From source file:Main.java
private void initComponents() { JFrame frame = new JFrame("JFormattedTextField Example"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); MaskFormatter mask = null;// w ww . j a v a 2 s . c om try { mask = new MaskFormatter("##h##min##s");// the # is for numeric values mask.setPlaceholderCharacter('#'); } catch (ParseException e) { e.printStackTrace(); } final JFormattedTextField timeField = new JFormattedTextField(mask); // ActionListener for when enter is pressed timeField.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent ae) { Object source = ae.getSource(); if (source == timeField) { // parse to a valid time here System.out.println(timeField.getText()); } } }); frame.add(timeField); frame.pack(); frame.setVisible(true); }
From source file:com.igormaznitsa.zxpspritecorrector.files.FileNameDialog.java
private Character getCharacter(final JFormattedTextField field) { final String text = field.getText().trim(); return text.isEmpty() ? null : Character.valueOf(text.charAt(0)); }
From source file:com.googlecode.vfsjfilechooser2.accessories.connection.ConnectionDialog.java
private void initListeners() { this.portTextField.addKeyListener(new KeyAdapter() { @Override/* w w w . j a v a2 s. 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); }