LeonN
LeonN

Reputation: 37

How to know in which JTextPane the cursor is?

I have several JtextPanes and I want to create a method that set the value of a variable to the name of the TextPane which has been clicked because another methond is going to use it. There's also a button that adds a new jTextPane when the user clicks on it and I want those new TextPanes to inherit that method. Or if you know a simpler way to achieve that I'm open to read you. Let me know if you need more information or more code.

static JTextPane focus;

private void redColorActionPerformed(java.awt.event.ActionEvent evt) { //Ths is the method that works with the focus variable. It changes the color of the text in the clicked textpane.                                      
        TextEditor.cambiarColor(new java.awt.Color(255, 0, 0), focus);
    }  

Upvotes: 0

Views: 81

Answers (1)

Happy
Happy

Reputation: 825

It sound like you want to know the current Swing JTextPane component under the current mouse position? Then try to

  • Get current Mouse position
Point position = MouseInfo.getPointerInfo().getLocation();
 Component c = SwingUtilities.getDeepestComponentAt(
     the-root-component,
     position.getX(), 
     position.getY()
 );
 if (c instanceof JTextPane) {
    // do your logic
 }

Upvotes: 1

Related Questions