Example usage for javax.swing JTextPane requestFocus

List of usage examples for javax.swing JTextPane requestFocus

Introduction

In this page you can find the example usage for javax.swing JTextPane requestFocus.

Prototype

public void requestFocus() 

Source Link

Document

Requests that this Component gets the input focus.

Usage

From source file:Main.java

public static void main(String[] args) {
    final JTextPane tp = new JTextPane();
    JButton withFocus = new JButton("Select with focus");
    withFocus.addActionListener(e -> {
        tp.selectAll();/* w w w. java2 s .  c  om*/
        tp.requestFocus();
    });
    JButton withOutFocus = new JButton("Select without focus");
    withFocus.addActionListener(e -> tp.selectAll());

    JFrame frame = new JFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setLayout(new BorderLayout());
    frame.add(new JScrollPane(tp));
    JPanel panel = new JPanel();
    panel.add(withFocus);
    panel.add(withOutFocus);
    frame.add(panel, BorderLayout.SOUTH);
    frame.pack();
    frame.setVisible(true);
}