简体   繁体   中英

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);
    }  

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
 }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM