Moving to fields not in visible part of window

8 views
Skip to first unread message

Sreenivas Kothapalli

unread,
Nov 16, 2010, 5:30:24 AM11/16/10
to jmatter
When we tabbing into a field that is not visible in the view, the
window does not scroll automatically. The user has to use the <Page-
Down> button. If I want to tinker with viewport where should I do it?

Sreenivas Kothapalli

unread,
Nov 27, 2010, 8:38:49 AM11/27/10
to jmatter
I created a class OnFocusScroller and modified
com.u2d.view.swing.FormView class like this.

public class FormView extends JXPanel implements IFormView
{
....
private OnFocusScroller _scroller;

....
// Added the following code to layItOut() wherever JScollPane is
used
// Note that there are two locations where it is used within the
method.

private void layItOut()
{
....
if (tabbedPane().getTabCount() > 0)
{
....
JScrollPane scrollPane = new JScrollPane(mainPane);
this._scroller = new OnFocusScroller(scrollPane);
tabbedPane().insertTab(_ceo.type().mainTabCaption(), null,
scrollPane, "", 0);
...
}
else
{
// whether the formview is in expandable tree or not, use
scrollpane
JScrollPane scrollPane = new JScrollPane(mainPane);
this._scroller = new OnFocusScroller(scrollPane);
add(scrollPane, BorderLayout.CENTER);
}
}

private void attach(ComplexObject ceo)
{
....
if( this._scroller != null ) {
this._scroller.setScrollingEnabled(true);
}
...
}

private void detach()
{
...
if( this._scroller != null ) {
this._scroller.setScrollingEnabled(false);
}
....
}
}

class OnFocusScroller implements java.beans.PropertyChangeListener
{
JScrollPane _scrollPane = null;
public OnFocusScroller(JScrollPane scrollPane)
{
this._scrollPane = scrollPane;
}
/**
* Enable automatic scrolling on the form.
*
* @param scrollingEnabled enable/disable scrolling
*/
public void setScrollingEnabled(boolean scrollingEnabled)
{
KeyboardFocusManager.getCurrentKeyboardFocusManager()
.removePropertyChangeListener("permanentFocusOwner", this);

if (scrollingEnabled)
{
KeyboardFocusManager.getCurrentKeyboardFocusManager()
.addPropertyChangeListener("permanentFocusOwner", this);
}
}

@Override
public void propertyChange(PropertyChangeEvent evt)
{
if( this._scrollPane == null ) {
return;
}
Component component = (Component) evt.getNewValue();

if (component == null) {
return;
}

// Make sure the component with focus is in the viewport

JComponent view = (JComponent)
this._scrollPane.getViewport().getView();

if (! SwingUtilities.isDescendingFrom(component, view)) {
return;
}

// Scroll the viewport

Rectangle bounds = component.getBounds();
bounds = SwingUtilities.convertRectangle(component.getParent(),
bounds, view);
view.scrollRectToVisible(bounds);
}
}
Reply all
Reply to author
Forward
0 new messages