Example usage for javax.swing JTextArea setSelectionStart

List of usage examples for javax.swing JTextArea setSelectionStart

Introduction

In this page you can find the example usage for javax.swing JTextArea setSelectionStart.

Prototype

@BeanProperty(bound = false, description = "starting location of the selection.")
public void setSelectionStart(int selectionStart) 

Source Link

Document

Sets the selection start to the specified position.

Usage

From source file:Main.java

public static void main(String[] args) {
    JFrame f = new JFrame();
    JTextArea ta = new JTextArea(5, 32);
    ta.setText("That's one small step for man...\nOne giant leap for mankind.");

    ta.setSelectionStart(10);

    ta.setSelectionEnd(20);//from   w  w  w. ja  v a 2s  .c o  m

    f.getContentPane().add(ta);
    f.setSize(100, 100);
    f.setVisible(true);

}

From source file:Main.java

/**
 * Select all text in a {@link JComponent} if it is a {@link JTextField} or {@link JTextArea}.
 *
 * @param comp the component//from w w w .  j a v a  2  s. c  o  m
 */
public static void selectAllText(JComponent comp) {
    if (comp instanceof JTextField) {
        JTextField tf = (JTextField) comp;
        tf.setSelectionStart(0);
        tf.setSelectionEnd(tf.getText().length());
    } else if (comp instanceof JTextArea) {
        JTextArea ta = (JTextArea) comp;
        ta.setSelectionStart(0);
        ta.setSelectionEnd(ta.getText().length());
    }
}

From source file:eu.delving.sip.Application.java

private static void memoryNotConfigured() {
    String os = System.getProperty("os.name");
    Runtime rt = Runtime.getRuntime();
    int totalMemory = (int) (rt.totalMemory() / 1024 / 1024);
    StringBuilder out = new StringBuilder();
    String JAR_NAME = "SIP-Creator-2014-XX-XX.jar";
    if (os.startsWith("Windows")) {
        out.append(":: SIP-Creator Startup Batch file for Windows (more memory than ").append(totalMemory)
                .append("Mb)\n");
        out.append("java -jar -Xms1024m -Xmx1024m ").append(JAR_NAME);
    } else if (os.startsWith("Mac")) {
        out.append("# SIP-Creator Startup Script for Mac OSX (more memory than ").append(totalMemory)
                .append("Mb)\n");
        out.append("java -jar -Xms1024m -Xmx1024m ").append(JAR_NAME);
    } else {//ww  w . java 2s .c  om
        System.out.println("Unrecognized OS: " + os);
    }
    String script = out.toString();
    final JDialog dialog = new JDialog(null, "Memory Not Configured Yet!",
            Dialog.ModalityType.APPLICATION_MODAL);
    JTextArea scriptArea = new JTextArea(3, 40);
    scriptArea.setText(script);
    scriptArea.setSelectionStart(0);
    scriptArea.setSelectionEnd(script.length());
    JPanel scriptPanel = new JPanel(new BorderLayout());
    scriptPanel.setBorder(BorderFactory.createTitledBorder("Script File"));
    scriptPanel.add(scriptArea, BorderLayout.CENTER);
    JPanel buttonPanel = new JPanel(new FlowLayout(FlowLayout.RIGHT));
    buttonPanel.setBorder(BorderFactory.createEmptyBorder(15, 15, 15, 15));
    JButton ok = new JButton("OK, Continue anyway");
    ok.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            dialog.setVisible(false);
            EventQueue.invokeLater(LAUNCH);
        }
    });
    buttonPanel.add(ok);
    JPanel centralPanel = new JPanel(new GridLayout(0, 1));
    centralPanel.setBorder(BorderFactory.createEmptyBorder(15, 25, 15, 25));
    centralPanel.add(
            new JLabel("<html><b>The SIP-Creator started directly can have too little default memory allocated."
                    + "<br>It should be started with the following script:</b>"));
    centralPanel.add(scriptPanel);
    centralPanel.add(new JLabel(
            "<html><b>Please copy the above text into a batch or script file and execute that instead.</b>"));
    dialog.getContentPane().add(centralPanel, BorderLayout.CENTER);
    dialog.getContentPane().add(buttonPanel, BorderLayout.SOUTH);
    dialog.pack();
    Dimension dimension = Toolkit.getDefaultToolkit().getScreenSize();
    int x = (int) ((dimension.getWidth() - dialog.getWidth()) / 2);
    int y = (int) ((dimension.getHeight() - dialog.getHeight()) / 2);
    dialog.setLocation(x, y);
    dialog.setVisible(true);
}