Adding mouselistener to JLabel / JButton inserted in JTextPane

I have a problem: when I try to add mouselistener to JLabel or JButton to JTextPane, I get the error "cannot be converted to Mouselistener by link conversion". I would rather have a component in JEditorPane. I also heard that you can use HyperlinkEvent.

Basically I want a component that can be right-clicked / left-clicked in JEditorPane (preffered) / JTextPane. Any help would be appreciated

Now it works (sortof), it only returns right clicks, and I don't need to draw the edges of the buttons. Can I underline the button text?

Below is a sample code ...

import java.awt.*;
import javax.swing.*;
import java.awt.Color;
import javax.swing.JTextPane;
import javax.swing.JButton;
import java.applet.*;
import java.awt.event.MouseAdapter; 
import java.awt.event.MouseEvent; 

public class jlabeltest extends Applet {

    public void init() {

        jlabeltest editorPaneExample = new jlabeltest();
        editorPaneExample.setSize(550, 300);
//      editorPaneExample.setText("tutorialData.com");
        editorPaneExample.setVisible(true);
    }


    public jlabeltest() {

        JTextPane editorPane = new JTextPane();
        editorPane.setSelectedTextColor(Color.red);
        editorPane.setText("<p color='#FF0000'>Cool!</p>");
        InlineB label = new InlineB("JLabel");  
        label.setAlignmentY(0.85f); 

        label.addMouseListener(new MouseAdapter()   {   

        public void mouseReleased(MouseEvent e)   
        {   
        if (e.isPopupTrigger())   
        {   
            JOptionPane.showMessageDialog(null,"Hello!");
            // do your work here   
        }   
    }   
});  
        editorPane.insertComponent(label); 
        this.add(editorPane); 
    }
}

InlineB.java

import javax.swing.JButton;

    public class InlineB extends JButton    {

        public InlineB( String caption )    {

            super( caption );
        }
    }
+5
source share
2 answers

, , .

JButton, HTML:

//added <u></u> to underlone button
InlineB label = new InlineB("<html><u>JLabel</u></html>");

, if MouseEvent.BUTTON1 SwingUtilities.isLeftMouseButton(MouseEvent me):

//added check for MouseEvent.BUTTON1 which is left click
if (e.isPopupTrigger() || e.getButton() == MouseEvent.BUTTON1) {
}

JButton, setBorder(null); InlineB, InlineB ( ):

   public InlineB(String caption) {
    super(caption);
    setBorder(null);//set border to nothing
}

, JTextPane, :

    //set content as html
    editorPane.setContentType("text/html");

, , :

enter image description here

import java.awt.Color;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.*;

public class Test {

    public static void main(String[] args) throws Exception {
        SwingUtilities.invokeLater(new Runnable() {

            @Override
            public void run() {
                new Test().createAndShowUI();
            }
        });
    }

    private void createAndShowUI() {
        JFrame frame = new JFrame();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        initComponents(frame);
        frame.pack();
        frame.setVisible(true);
    }

    private void initComponents(JFrame frame) {
        JTextPane editorPane = new JTextPane();
        editorPane.setSelectedTextColor(Color.red);

        //set content as html
        editorPane.setContentType("text/html");
        editorPane.setText("<p color='#FF0000'>Cool!</p>");

        //added <u></u> to underlone button
        InlineB label = new InlineB("<html><u>JLabel</u></html>");

        label.setAlignmentY(0.85f);

        label.addMouseListener(new MouseAdapter() {

            @Override
            public void mouseReleased(MouseEvent e) {
                //added check for MouseEvent.BUTTON1 which is left click
                if (e.isPopupTrigger() || e.getButton() == MouseEvent.BUTTON1) {
                    JOptionPane.showMessageDialog(null, "Hello!");
                    // do your work here   
                }
            }
        });

        editorPane.insertComponent(label);
        frame.getContentPane().add(editorPane);
    }
}

class InlineB extends JButton {

    public InlineB(String caption) {
        super(caption);
        setBorder(null);//set border to nothing
    }
}
+5

: mouselistener JLabel JButton JTextPane, " Mouselistener ".

, addMouseListener(), MouseListener. ? ( . ).
, (sortof). , ?

, , , , : https://meta.stackexchange.com/questions/48345/what-is-the-etiquette-for-changing-the-substance-of-a-question

JEditorPane.

, , . , JEditorPane .

, HyperlinkEvent.

HyperLinkEvent , , EXITED ACTIVATED. ?

, , / JEditorPane (preffered)/JTextPane.

/ . , , - ( ?) , . , , JEditorPane .

+2

All Articles