开发者

Java: Strange behavior of JEditorPane wrapped inside JScrollPane

开发者 https://www.devze.com 2023-02-13 07:58 出处:网络
I\'m trying to build a simple help system to my software. The help system built from JEditorPane(Loaded with HTML file) wrapped inside of JScrollPane, inside of the same window there is a JLabel.

I'm trying to build a simple help system to my software.

The help system built from JEditorPane(Loaded with HTML file) wrapped inside of JScrollPane, inside of the same window there is a JLabel.

When the user move the mouse over the JEditorPane on a specific word - more explanations appear in the JLabel.

I succeed doing it, but the problem is, that for some reason it work just on the beginning of the text.(the HTML file is long and must be scrolled...)

After i scroll down the page and hover over a word, it throw me BadLocationException.

On the code below there is a JEditorPane wrapped inside JScrollPane.

When the user move the mouse it print the current letter which the mouse point on.(on the help system i find the value of the word by this position and print explanations to the JLabel according to it)

But, as i said it work just on 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 P开发者_Go百科oint(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();
    }
}

Thanks


System.out.println(editorPan.getText(pos--, pos).charAt(0));

should be:

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

精彩评论

暂无评论...
验证码 换一张
取 消