List of usage examples for javax.swing.text JTextComponent getText
public String getText()
TextComponent
. From source file:AddingActionCommandActionListenerSample.java
public static void main(String args[]) { final JFrame frame = new JFrame("Default Example"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); JTextField textField = new JTextField(); frame.add(textField, BorderLayout.NORTH); frame.add(new JTextField(), BorderLayout.SOUTH); InputVerifier verifier = new InputVerifier() { public boolean verify(JComponent input) { final JTextComponent source = (JTextComponent) input; String text = source.getText(); if ((text.length() != 0) && !(text.equals("Exit"))) { JOptionPane.showMessageDialog(frame, "Can't leave.", "Error Dialog", JOptionPane.ERROR_MESSAGE); return false; } else { return true; }//from w ww . j a v a 2 s .co m } }; textField.setInputVerifier(verifier); frame.setSize(250, 150); frame.setVisible(true); }
From source file:MainClass.java
public static void main(String args[]) throws Exception { JFrame frame = new JFrame(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); JTextField nameTextField = new JTextField(); frame.add(nameTextField, BorderLayout.NORTH); frame.add(new JTextField(), BorderLayout.SOUTH); InputVerifier verifier = new InputVerifier() { public boolean verify(JComponent input) { final JTextComponent source = (JTextComponent) input; String text = source.getText(); if ((text.length() != 0) && !(text.equals("Exit"))) { JOptionPane.showMessageDialog(source, "Can't leave.", "Error Dialog", JOptionPane.ERROR_MESSAGE); return false; } else { return true; }/*from w w w . j a v a2s .c om*/ } }; nameTextField.setInputVerifier(verifier); frame.setSize(250, 100); frame.setVisible(true); }
From source file:Main.java
public static void main(String[] argv) throws Exception { JTextComponent tc = new JTextArea("Initial Text"); int docLength = tc.getDocument().getLength(); String text = tc.getText(); // Get the last 3 characters int offset = docLength - 3; int len = 3;/* w ww.j a va 2 s.c om*/ text = tc.getText(offset, len); }
From source file:Main.java
public static void main(String[] argv) throws Exception { JTextComponent tc = new JTextArea("Initial Text"); int docLength = tc.getDocument().getLength(); // Get all text String text = tc.getText(); // Get the first 3 characters int offset = 0; int len = 3;//from w w w.j a v a 2 s . c o m text = tc.getText(offset, len); }
From source file:JTextFieldSample.java
public static void main(String args[]) { String title = (args.length == 0 ? "TextField Listener Example" : args[0]); JFrame frame = new JFrame(title); Container content = frame.getContentPane(); JPanel namePanel = new JPanel(new BorderLayout()); JLabel nameLabel = new JLabel("Name: "); nameLabel.setDisplayedMnemonic(KeyEvent.VK_N); JTextField nameTextField = new JTextField(); nameLabel.setLabelFor(nameTextField); namePanel.add(nameLabel, BorderLayout.WEST); namePanel.add(nameTextField, BorderLayout.CENTER); content.add(namePanel, BorderLayout.NORTH); JPanel cityPanel = new JPanel(new BorderLayout()); JLabel cityLabel = new JLabel("City: "); cityLabel.setDisplayedMnemonic(KeyEvent.VK_C); JTextField cityTextField = new JTextField(); cityLabel.setLabelFor(cityTextField); cityPanel.add(cityLabel, BorderLayout.WEST); cityPanel.add(cityTextField, BorderLayout.CENTER); content.add(cityPanel, BorderLayout.SOUTH); ActionListener actionListener = new ActionListener() { public void actionPerformed(ActionEvent actionEvent) { System.out.println("Command: " + actionEvent.getActionCommand()); }//from w ww.j a v a2s . c o m }; nameTextField.setActionCommand("Yo"); nameTextField.addActionListener(actionListener); cityTextField.addActionListener(actionListener); KeyListener keyListener = new KeyListener() { public void keyPressed(KeyEvent keyEvent) { printIt("Pressed", keyEvent); } public void keyReleased(KeyEvent keyEvent) { printIt("Released", keyEvent); } public void keyTyped(KeyEvent keyEvent) { printIt("Typed", keyEvent); } private void printIt(String title, KeyEvent keyEvent) { int keyCode = keyEvent.getKeyCode(); String keyText = KeyEvent.getKeyText(keyCode); System.out.println(title + " : " + keyText + " / " + keyEvent.getKeyChar()); } }; nameTextField.addKeyListener(keyListener); cityTextField.addKeyListener(keyListener); InputVerifier verifier = new InputVerifier() { public boolean verify(JComponent input) { final JTextComponent source = (JTextComponent) input; String text = source.getText(); if ((text.length() != 0) && !(text.equals("Exit"))) { Runnable runnable = new Runnable() { public void run() { JOptionPane.showMessageDialog(source, "Can't leave.", "Error Dialog", JOptionPane.ERROR_MESSAGE); } }; SwingUtilities.invokeLater(runnable); return false; } else { return true; } } }; nameTextField.setInputVerifier(verifier); cityTextField.setInputVerifier(verifier); DocumentListener documentListener = new DocumentListener() { public void changedUpdate(DocumentEvent documentEvent) { printIt(documentEvent); } public void insertUpdate(DocumentEvent documentEvent) { printIt(documentEvent); } public void removeUpdate(DocumentEvent documentEvent) { printIt(documentEvent); } private void printIt(DocumentEvent documentEvent) { DocumentEvent.EventType type = documentEvent.getType(); String typeString = null; if (type.equals(DocumentEvent.EventType.CHANGE)) { typeString = "Change"; } else if (type.equals(DocumentEvent.EventType.INSERT)) { typeString = "Insert"; } else if (type.equals(DocumentEvent.EventType.REMOVE)) { typeString = "Remove"; } System.out.print("Type : " + typeString + " / "); Document source = documentEvent.getDocument(); int length = source.getLength(); try { System.out.println("Contents: " + source.getText(0, length)); } catch (BadLocationException badLocationException) { System.out.println("Contents: Unknown"); } } }; nameTextField.getDocument().addDocumentListener(documentListener); cityTextField.getDocument().addDocumentListener(documentListener); frame.setSize(250, 150); frame.setVisible(true); }
From source file:Main.java
public static double getDoubleTextField(double defaultValue, JTextComponent c) { try {//from ww w . ja va 2 s.co m return Double.parseDouble(c.getText()); } catch (RuntimeException re) { return defaultValue; } }
From source file:Main.java
/** * Pastes via the fake clipboard./*from www . j av a2 s. com*/ */ static void fakePaste(JTextComponent tc) { String text = tc.getText(); int caretPos = tc.getCaretPosition(); tc.setText(text.substring(0, caretPos) + clipboard + text.substring(caretPos)); tc.setCaretPosition(caretPos + clipboard.length()); }
From source file:Main.java
public static void appendText(JTextComponent jcomponent, String str) { StringBuilder stringBuilder = new StringBuilder(jcomponent.getText()).append("\n").append(str); jcomponent.setText(stringBuilder.toString()); }
From source file:Main.java
public static void refreshJTextComponent(JTextComponent textComponent) { // borrowed from metaphase editor int pos = textComponent.getCaretPosition(); textComponent.setText(textComponent.getText()); textComponent.validate();/*from ww w .j ava 2s .c o m*/ try { textComponent.setCaretPosition(pos); } catch (IllegalArgumentException e) { // swallow the exception // seems like a bug in the JTextPane component // only happens occasionally when pasting text at the end of a document System.err.println(e.getMessage()); } }
From source file:Main.java
public static KeyListener maxLength(JTextComponent textComponent, int length) { return new KeyAdapter() { @Override//w ww. j a v a2 s . c o m public void keyTyped(KeyEvent e) { if (textComponent.getText().length() >= length) { e.consume(); textComponent.setText(textComponent.getText().substring(0, length)); } } }; }