ses
ses

Reputation: 178

Java Swing: Event for java.swing.text.View.modelToView() up-to-date

Is there an event that ensures that the modelToView call returns the Shape with the latest bounds?

I have a JEditorPane containing some text. Next to the editorpane is a panel, which shows the line numbers. In order to draw the line numbers at the correct position, I need the bounds for the lines. Therefore I get the view of the editorpane and call modelToView for each line, e.g.:

View view = editorPane.getUI().getRootView(editorPane);
Rectangle bounds = view.modelToView(startOffsetOfLine, Position.Bias.Forward, endOffsetOfLine, Position.Bias.Backward, new Rectangle()).getBounds();

In my case sometimes outdated bounds are returned. Is there any listener or event that ensures that modelToView returns the latest bounds?

Upvotes: 0

Views: 48

Answers (1)

camickr
camickr

Reputation: 324167

In my case sometimes outdated bounds are returned

Wrap the code in a SwingUtilities.invokeLater(...).

This will make sure the code is added to the end of the EDT so it will be processed after all updates to the Document have been made.

SwingUtilities.invokeLater( () -> 
{
    View view = editorPane.getUI().getRootView(editorPane);
    Rectangle bounds = view.modelToView(startOffsetOfLine, Position.Bias.Forward, endOffsetOfLine, Position.Bias.Backward, new Rectangle()).getBounds();
});

Upvotes: 0

Related Questions