javahtmlswinghtml-tablejeditorpane

How to obtain the row from a mouse click on JEditorPane containg an HTML table


Is there a way to obtain the row index within an HTML table defined in a JEditorPane? I tried viewToModel method but this isn't what I was expecting.

pane.addMouseListener(new MouseListener(){

    @Override
    public void mouseClicked(MouseEvent e) {
        System.out.println( pane.viewToModel(e.getPoint()) );               
    }

                ...

Solution

  • Easy/lazy answer: of course you cannot. Even in a pure html page, you need some javascript to achive this. JEditorPane can't handle javascript (i think, maybe i'm wrong), so no chance.

    Now, you can use a different approach:

    <a href="#row001">Some field</a> 
    
    editor.addHyperlinkListener(new HyperlinkListener() {
                @Override
                public void hyperlinkUpdate(HyperlinkEvent e) {
                    if(e.getEventType() == HyperlinkEvent.EventType.ACTIVATED) {
                        try {
                            String rowcode = e.getDescription();
                    int row = Integer.parseInt(rowcode.replace("#row",""));
                        } catch (IOException ioe) {
                            ioe.printStackTrace();
                        }
                    }
                }
    
            });
    

    Now in row you should have the number of the row.

    Full example

    package test;
    
    import java.awt.BorderLayout;
    import java.awt.EventQueue;
    
    import javax.swing.JEditorPane;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import javax.swing.border.EmptyBorder;
    import javax.swing.event.HyperlinkEvent;
    import javax.swing.event.HyperlinkListener;
    
    public class App extends JFrame {
    
        private static final long serialVersionUID = 1L;
    
        private String HTML = "<html><table border=\"1\"><tr><td><a href=\"#row001\">First row</a></td></tr><tr><td><a href=\"#row002\">Second row</a></td></tr><tr><td><a href=\"#row003\">Third row</a></td></tr></table></html>";
    
        private JPanel contentPane;
        private JEditorPane editor;
    
        /**
         * Launch the application.
         */
        public static void main(String[] args) {
            EventQueue.invokeLater(new Runnable() {
                public void run() {
                    try {
                        App frame = new App();
                        frame.setVisible(true);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            });
        }
    
        /**
         * Create the frame.
         */
        public App() {
            setTitle("HTML TABLE CLICKABLE");
            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            setBounds(100, 100, 408, 235);
            contentPane = new JPanel();
            contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
            contentPane.setLayout(new BorderLayout(0, 0));
            setContentPane(contentPane);
    
            editor = new JEditorPane();
            editor.setEditable(false);
            editor.setContentType("text/html");
            contentPane.add(editor, BorderLayout.CENTER);
            editor.setText(HTML);
    
            editor.addHyperlinkListener(new HyperlinkListener() {
                @Override
                public void hyperlinkUpdate(HyperlinkEvent e) {
                    if(e.getEventType() == HyperlinkEvent.EventType.ACTIVATED) {
                        String rowcode = e.getDescription();
                        int row = Integer.parseInt(rowcode.replace("#row",""));
    
                        System.out.println(row);
                    }
                }
    
            });
    
        }
    }