I have a problem where when I try and add a mouselistener to a JLabel or JButton in a JTextPane I get the error "cannot be converted to Mouselistener by invocation conversion". I would prefer to have the component in a JEditorPane. I also heard a HyperlinkEvent could be used.
Basicly I want a component that can be right/left clicked in a JEditorPane(preffered)/JTextPane. Any help would be appreciated
Now it works (sortof) it only recives right clicks and I need to not draw button edges. Can I underline the button's text?
Example code follows...
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 );
}
}
JButton(String text) This constructor will create a button in the component with the specified text in the parameters.
The method setText() can be used to update the text of JLabel in Swing. In Java, we create a frame where the label will be changed if we press the button.
JButton is a push button used to perform an action. JLabel is a component used to dispay a short text string or an image, or both.
I'm not sure what you want the question is everywhere.
But look too underline text of a JButton
simply set the text of the button with HTML tags:
//added <u></u> to underlone button
InlineB label = new InlineB("<html><u>JLabel</u></html>");
as for the left click add a check to your if statement for the MouseEvent.BUTTON1
or SwingUtilities.isLeftMouseButton(MouseEvent me)
:
//added check for MouseEvent.BUTTON1 which is left click
if (e.isPopupTrigger() || e.getButton() == MouseEvent.BUTTON1) {
}
To not draw the borders of the JButton
simply call setBorder(null);
either in the InlineB
class or on the InlineB
instance (I did it within the class):
public InlineB(String caption) {
super(caption);
setBorder(null);//set border to nothing
}
also I see that you dont set the content type of the JTextPane
, which you should:
//set content as html
editorPane.setContentType("text/html");
I did a small example though I did not use an Applet but its very easy to port:
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
}
}
I have a problem where when I try and add a mouselistener to a JLabel or JButton in a JTextPane I get the error "cannot be converted to Mouselistener by invocation conversion".
The object you are passing to addMouseListener() implements the MouseListener interface. Right? (Just seen the code sample. Mouse adapter seems right).
You now say Now it works (sortof). Does it mean you have corrected that error?
BTW if that is resolved and you have subsequent problems, and they are reusable by the community, then i would advise to open a separate question: https://meta.stackexchange.com/questions/48345/what-is-the-etiquette-for-changing-the-substance-of-a-question
I would prefer to have the component in a JEditorPane.
I guess you mean the component you are listening. Anyway I'm not sure the JEditorPane is meant to be used as other components' container.
I also heard a HyperlinkEvent could be used.
HyperLinkEvent is meant for ENTERED, EXITED, and ACTIVATED event types. You are intending to handle Hyperlink events or mouse events?
Basicly I want a component that can be right/left clicked in a JEditorPane(preffered)/JTextPane. Any help would be appreciated
I would advise next time give the scope/context of the question first. I guess you mean you want something (can you be more specific?) on top of a text pane that can be clicked. Anyway I'm surprise you intend to use JEditorPane that way.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With