Java: JEditorPane weird behavior wrapped in JScrollPane

I am trying to create a simple help system for my software.
The help system created from JEditorPane (loaded using an HTML file), wrapped inside JScrollPane, inside the same window there is JLabel.
When a user hovers over a specific word on a JEditorPane, more explanation appears in JLabel.

I succeed, but the problem is that for some reason it only works at the beginning of the text (the HTML file is long and needs to be scrolled ...)
After I scroll down the page and hover over the word, it throws me BadLocationException.

The code below has a JEditorPane wrapped inside a JScrollPane.
When the user moves the mouse, it prints the current letter that the mouse point points to. (in the help system, I find the meaning of the word by this position and print the explanations of JLabel according to it)<b> But, as I said, this only works at the beginning of the text.
Why?



import java.awt.Component;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.LayoutManager;
import java.awt.Point;
import java.io.IOException;

import javax.swing.JEditorPane;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.text.BadLocationException;

public class JEditorPaneTestApp extends JFrame {

    private JEditorPane editorPan;
    private JScrollPane scrollPan;

    public JEditorPaneTestApp() {
        super();
        try {
            editorPan = new javax.swing.JEditorPane("file:///path/toHTML/file/helpFile.html");
        } 
        catch (IOException e) {e.printStackTrace();}

        scrollPan = new JScrollPane(editorPan);

        this.add(scrollPan);

        editorPan.addMouseMotionListener(new java.awt.event.MouseMotionAdapter() {
                    public void mouseMoved(java.awt.event.MouseEvent evt) {
                        Point p = new Point(evt.getX(), evt.getY());
                        int pos = editorPan.viewToModel(p);
                        try {
                        System.out.println(editorPan.getText(pos--, pos).charAt(0)); 
                        }
                        catch (BadLocationException e1) {
                            System.out.println("Invalid location");/* e1.printStackTrace();*/
                        }
                    }
                });
        scrollPan.setViewportView(editorPan);
        this.add(scrollPan);

        //
        this.getContentPane().setLayout(new LayoutManager() {
            @Override public Dimension preferredLayoutSize(Container arg0) {return null;} 
            @Override public Dimension minimumLayoutSize(Container arg0) {return null;}
            @Override public void removeLayoutComponent(Component arg0) {}
            @Override public void addLayoutComponent(String arg0, Component arg1) {}
            @Override public void layoutContainer(Container conter) {
                scrollPan.setBounds(0, 0, conter.getWidth(),  conter.getHeight());
            }
        });

        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setVisible(true);

    }

    public static void main(String[] args) {
        JEditorPaneTestApp test = new JEditorPaneTestApp();
    }
}


thank

+3
source share
1 answer
System.out.println(editorPan.getText(pos--, pos).charAt(0));

it should be:

System.out.println(editorPan.getText(pos--, 1).charAt(0));
+3
source

Source: https://habr.com/ru/post/1794862/


All Articles