views:

34

answers:

1

I have a popup menu that displays dynamically created custom JPanel objects in a JPanel in a JScrollPane. The popup menu displays recommendations to the user and the topmost element is the most relevant recommendation. I am using JPopupMenu to display the window:

JPanelTemplatePopup jptep = new JPanelTemplatePopup();
JPopupMenu popup = new JPopupMenu();
popup.add(jptep);
popup.show(this, 500, 100);

The problem is, I can't make the JScrollPane scroll to the topmost element to display it first. I have tried:

  • .getViewPort().setViewPosition(new Point(0,0));
  • .scrollRectToVisible(firstelement.getBounds());

before and after validate()s. No matter what I do, when the window pops up, the scroll pane always stays at the same place.

I have even suspected that the operations that took place before displaying the window were ignored, so I created and called a public method from the class to make the window scroll it up after being displayed. Nothing changed.

Please help,

Emre

A: 

Yeah, well, found a workaround for this.

I have narrowed the problem down to a JEditorPane that was in the custom JPanel objects. Its contents were dynamically updated by the program. While generating the objects, I was setting JEditorPanes' contents by their setText methods. Updating the caret position by setting the text to a string forced the scroll pane to scroll below.

I just inserted this to the constructor and the problem was fixed:

DefaultCaret caret = (DefaultCaret) jEditorPaneContents.getCaret();
caret.setUpdatePolicy(DefaultCaret.NEVER_UPDATE);
Emre