Example usage for javax.swing JTextField getSelectedText

List of usage examples for javax.swing JTextField getSelectedText

Introduction

In this page you can find the example usage for javax.swing JTextField getSelectedText.

Prototype

@BeanProperty(bound = false)
public String getSelectedText() 

Source Link

Document

Returns the selected text contained in this TextComponent.

Usage

From source file:Main.java

public static void main(String args[]) {
    final JTextField textField = new JTextField(15);
    JButton buttonCut = new JButton("Cut");
    JButton buttonPaste = new JButton("Paste");
    JButton buttonCopy = new JButton("Copy");

    JFrame jfrm = new JFrame("Cut, Copy, and Paste");
    jfrm.setLayout(new FlowLayout());
    jfrm.setSize(230, 150);//w w w.  jav a 2s .c  o m
    jfrm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    buttonCut.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent le) {
            textField.cut();
        }
    });

    buttonPaste.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent le) {
            textField.paste();
        }
    });

    buttonCopy.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent le) {
            textField.copy();
        }
    });

    textField.addCaretListener(new CaretListener() {
        public void caretUpdate(CaretEvent ce) {
            System.out.println("All text: " + textField.getText());
            if (textField.getSelectedText() != null)
                System.out.println("Selected text: " + textField.getSelectedText());
            else
                System.out.println("Selected text: ");
        }
    });

    jfrm.add(textField);
    jfrm.add(buttonCut);
    jfrm.add(buttonPaste);
    jfrm.add(buttonCopy);
    jfrm.setVisible(true);
}

From source file:TextCutPaste.java

/**
 * Bundle up the data for export.//from w ww.jav  a2  s.c  o m
 */
protected Transferable createTransferable(JComponent c) {
    JTextField source = (JTextField) c;
    int start = source.getSelectionStart();
    int end = source.getSelectionEnd();
    Document doc = source.getDocument();
    if (start == end) {
        return null;
    }
    try {
        p0 = doc.createPosition(start);
        p1 = doc.createPosition(end);
    } catch (BadLocationException e) {
        System.out.println("Can't create position - unable to remove text from source.");
    }
    String data = source.getSelectedText();
    return new StringSelection(data);
}

From source file:org.openconcerto.erp.core.common.ui.DeviseField.java

public static void addFilteringKeyListener(final JTextField textField) {

    textField.addKeyListener(new KeyAdapter() {
        public void keyTyped(java.awt.event.KeyEvent keyEvent) {

            final char keychar = keyEvent.getKeyChar();

            if (keychar == KeyEvent.VK_BACK_SPACE) {
                return;
            }/* w  ww  .  j a va2  s  . c  o  m*/

            // pas plus de 2 chiffres apres la virgule
            int pointPosition = textField.getText().indexOf('.');
            if (Character.isDigit(keychar)) {
                if (pointPosition > -1) {
                    // System.err.println("Text Selected :: " + textField.getSelectedText());
                    if (textField.getSelectedText() == null) {
                        if (textField.getCaretPosition() <= pointPosition) {
                            return;
                        } else {
                            if (textField.getText().substring(pointPosition).length() <= 2) {
                                return;
                            }
                        }
                    } else {
                        return;
                    }
                } else {
                    return;
                }
            }

            if (keychar == KeyEvent.VK_PERIOD && textField.getText().indexOf('.') < 0)
                return;
            // if (keychar == KeyEvent.VK_MINUS && (textField.getText().indexOf('-') < 0) &&
            // textField.getCaretPosition() == 0)
            // return;

            keyEvent.consume();
        }
    });
}