Example usage for javax.swing JTextArea getSelectedText

List of usage examples for javax.swing JTextArea getSelectedText

Introduction

In this page you can find the example usage for javax.swing JTextArea 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[] argv) throws Exception {
    JTextArea comp = new JTextArea();
    String actionName = "Lowercase";

    JFrame f = new JFrame();
    f.add(new JScrollPane(comp));
    f.setSize(300, 300);/* w w w .  j  a  va 2  s .c  o m*/
    f.setVisible(true);
    comp.getInputMap().put(KeyStroke.getKeyStroke("F2"), actionName);

    comp.getActionMap().put(actionName, new TextAction(actionName) {
        public void actionPerformed(ActionEvent evt) {
            JTextComponent comp = getTextComponent(evt);

            if (comp.getSelectionStart() == comp.getSelectionEnd()) {
                if (comp.getCaretPosition() < comp.getDocument().getLength()) {
                    try {
                        int pos = comp.getCaretPosition();
                        Document doc = comp.getDocument();
                        String str = doc.getText(pos, 1).toLowerCase();

                        doc.remove(pos, 1);
                        doc.insertString(pos, str, null);
                        comp.moveCaretPosition(pos + 1);
                    } catch (Exception e) {
                        System.out.println();
                    }
                }
            } else {
                int s = comp.getSelectionStart();
                int e = comp.getSelectionEnd();

                comp.replaceSelection(comp.getSelectedText().toLowerCase());
                comp.select(s, e);
            }
        }
    });
}

From source file:com._17od.upm.gui.AccountDialog.java

/**
 * This method takes in a JTextArea object and then copies the selected text
 * in that text area to the system clipboard.
 * /*  w  ww. jav a 2s . com*/
 * @param textArea
 */
public void copyTextArea(JTextArea textArea) {
    Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
    StringSelection selected = new StringSelection(textArea.getSelectedText());
    clipboard.setContents(selected, selected);
}

From source file:com.att.aro.ui.view.menu.tools.RegexWizard.java

@Override
public void focusLost(FocusEvent e) {
    JTextArea field = (JTextArea) e.getSource();
    if (field.getName().equals(requestField.getName())) {
        setRequestHighlightedText(field.getSelectedText());
        updateFocus(true, false, false);
    } else if (field.getName().equals(headerField.getName())) {
        setHeaderHighlightedText(field.getSelectedText());
        updateFocus(false, false, true);
    } else if (field.getName().equals(responseField.getName())) {
        setResponseHighlightedText(field.getSelectedText());
        updateFocus(false, true, false);
    } else {// if(field.getName().equals(regexRequestField.getName()) || field.getName().equals(regexResponseField.getName()) ||
            // field.getName().equals(regexHeaderField.getName())){
        clearHighlightedTexts();//from  w  w w .java2  s.  c  om
        if (field.getName().equals(regexRequestField.getName())) {
            updateFocus(true, false, false);
        } else if (field.getName().equals(regexResponseField.getName())) {
            updateFocus(false, true, false);
        } else {
            updateFocus(false, false, true);

        }
    }
}

From source file:uk.nhs.cfh.dsp.srth.desktop.modules.resultexplanationpanel.QueryStatisticsCollectionPanel.java

/**
 * Creates the statements panel.//from  w  ww  .j a  v a 2 s  .  co  m
 * 
 * @param textArea the text area
 * @param title the title
 * 
 * @return the j panel
 */
private synchronized JPanel createStatementsPanel(final JTextArea textArea, String title) {

    JPanel panel = new JPanel(new BorderLayout());
    panel.setBorder(BorderFactory.createCompoundBorder(BorderFactory.createEmptyBorder(),
            BorderFactory.createTitledBorder(title)));

    // create buttons for copy and export
    JButton copyButton = new JButton(new AbstractAction("Copy") {

        public void actionPerformed(ActionEvent event) {
            StringSelection selection = new StringSelection(textArea.getSelectedText());
            // get contents of text area and copy to system clipboard
            Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
            clipboard.setContents(selection, QueryStatisticsCollectionPanel.this);
        }
    });

    JButton exportButton = new JButton(new AbstractAction("Export") {

        public void actionPerformed(ActionEvent event) {
            // open a file dialog and export the contents
            FileDialog fd = new FileDialog(applicationService.getFrameView().getActiveFrame(),
                    "Select file to export to", FileDialog.SAVE);
            fd.setVisible(true);

            String fileName = fd.getFile();
            String fileDirectory = fd.getDirectory();

            if (fileDirectory != null && fileName != null) {
                File file = new File(fileDirectory, fileName);
                try {
                    String contents = textArea.getText();
                    FileWriter fw = new FileWriter(file);
                    fw.flush();
                    fw.write(contents);
                    fw.close();
                    // inform user
                    //                  manager.getStatusMessageLabel().setText("Successfully saved contents to file "+fileName);
                    logger.info("Successfully saved contents to file " + fileName);

                } catch (IOException e) {
                    logger.warn(e.fillInStackTrace());
                    //                  manager.getStatusMessageLabel().setText("Errors saving contents to file "+fileName);
                }
            }
        }
    });
    // create buttons panel
    JPanel buttonsPanel = new JPanel();
    buttonsPanel.setLayout(new BoxLayout(buttonsPanel, BoxLayout.LINE_AXIS));
    buttonsPanel.add(Box.createHorizontalStrut(200));
    buttonsPanel.add(copyButton);
    buttonsPanel.add(exportButton);

    panel.add(buttonsPanel, BorderLayout.SOUTH);
    panel.add(new JScrollPane(textArea), BorderLayout.CENTER);

    return panel;
}