2

After much toil I got my click thing working fine. Sadly, When I change the format of my JTextPane to "text/html" and add text to the JTextPane my button disappears. I am almost done with this harsh mistress. Could anyone help?

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 textPaneExample = new jlabeltest();
        textPaneExample.setSize(550, 300);
        textPaneExample.setVisible(true);
    }


    public jlabeltest() {

        JTextPane textPane = new JTextPane();
        textPane.setContentType("text/html");
        InlineB button = new InlineB("Button");     
        textPane.setText("<p color='#FF0000'>Cool!</p>");
        button.setAlignmentY(0.85f); 

        button.addMouseListener(new MouseAdapter()  {   

        public void mouseReleased(MouseEvent e) {

        if (e.isPopupTrigger()) {   

            JOptionPane.showMessageDialog(null,"Hello!");
            // Right Click   
        }   

        if (SwingUtilities.isLeftMouseButton(e)) {

            JOptionPane.showMessageDialog(null,"Click!");
            // Left Click
        }
    }   
    });  
        textPane.insertComponent(button); 
        this.add(textPane); 
    }
}
mKorbel
  • 109,525
  • 20
  • 134
  • 319
Confident
  • 321
  • 4
  • 17
  • should I just keep it in native mode and not use HTML or do I have a choice? – Confident Sep 29 '12 at 10:27
  • please to check post by [camickr](http://stackoverflow.com/users/131872/camickr) and [StanislavL](http://stackoverflow.com/users/301607/stanislavl) about `JTextPane` / `JEditorPane` – mKorbel Sep 29 '12 at 11:44
  • http://java-sl.com/custom_tag_html_kit.html that shows how to add a custom tag for button – StanislavL Sep 30 '12 at 08:00

2 Answers2

3

There seems to be some problem with this content type when adding components (see this post), but you could try something like the following:

    JTextPane textPane = new JTextPane();
    JButton button = new JButton("Button");     
    button.setAlignmentY(0.85f);

    HTMLEditorKit kit = new HTMLEditorKit();
    HTMLDocument doc = new HTMLDocument();
    textPane.setEditorKit(kit);
    textPane.setDocument(doc);

    try {
        kit.insertHTML(doc, doc.getLength(), "<p color='#FF0000'>Cool!", 0, 0, HTML.Tag.P);
        kit.insertHTML(doc, doc.getLength(), "<p></p>", 0, 0, null);
    } catch (BadLocationException ex) {
    } catch (IOException ex) {
    }
Community
  • 1
  • 1
Rempelos
  • 1,220
  • 10
  • 18
2

The problem is when you add text to the JEditorPane and then add a component it gets embedded within the HTML of the JEditorPane:

Here is an example:

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) {
        final 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
        final 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!");
                    String s = editorPane.getText();
                    System.out.println(s);
                }
            }
        });

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

class InlineB extends JButton {

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

When we click the button this is executed:

//added check for MouseEvent.BUTTON1 which is left click
                if (e.isPopupTrigger() || e.getButton() == MouseEvent.BUTTON1) {
                    JOptionPane.showMessageDialog(null, "Hello!");
                    String s = editorPane.getText();
                    System.out.println(s);
                }

the results of the println(s) is:

<html>
  <head>

  </head>
  <body>
    <p color="#FF0000">
      Cool!
      <p $ename="component">

    </p>
  </body>
</html>

As you can see the component is embedded in the HTML any further calls to setText() will erase this.

The only other viable solution (besides Rempelos +1 to him) is too:

Re-add the component to the JEditorPane after you call setText() like so:

        @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!");
                String s = editorPane.getText();
                System.out.println(s);

                editorPane.setText("<html><u>Hello</u></html>");
                //re add after call to setText
                editorPane.insertComponent(label);
            }
        }

Though a better way might be to extend the JEditorPane class and make the setText()method automatically add its component/button

David Kroukamp
  • 36,155
  • 13
  • 81
  • 138