-
Enhancement
-
Resolution: Duplicate
-
P5
-
None
-
6
-
x86
-
windows_xp
A DESCRIPTION OF THE REQUEST :
JScrollPane calls setViewport() on a JViewport that does not yet have an associated view Component, so the resulting call to JViewport.getView() always throws an ArrayIndexOutOfBoundsException.
The fix is simple: both the JScrollPane constructor and its setViewportView() method should initalise the JViewport's view before calling setViewport().
// this is the modified constructor
public JScrollPane(Component view, int vsbPolicy, int hsbPolicy)
{
setLayout(new ScrollPaneLayout.UIResource());
setVerticalScrollBarPolicy(vsbPolicy);
setHorizontalScrollBarPolicy(hsbPolicy);
if (view != null) {
setViewportView(view);
}
setVerticalScrollBar(createVerticalScrollBar());
setHorizontalScrollBar(createHorizontalScrollBar());
setOpaque(true);
updateUI();
if (!this.getComponentOrientation().isLeftToRight()) {
viewport.setViewPosition(new Point(Integer.MAX_VALUE, 0));
}
}
// this is the modified mutator for the JScrollPane's view Component
public void setViewportView(Component view) {
if (getViewport() == null) {
JViewport port = createViewport();
if (view != null) {
port.setView(view);
}
setViewport(port);
return;
}
getViewport().setView(view);
}
JUSTIFICATION :
Exceptions are an inefficient way to detect error situations; it is worse when a standard use of Swing causes exceptions to be thrown every time, even if they are caught internally.
EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
Swing should use it's own API's in a way that don't trigger exceptions. This is more efficient (faster, which Swing needs to be) and it makes it easier to debug application code.
JScrollPane calls setViewport() on a JViewport that does not yet have an associated view Component, so the resulting call to JViewport.getView() always throws an ArrayIndexOutOfBoundsException.
The fix is simple: both the JScrollPane constructor and its setViewportView() method should initalise the JViewport's view before calling setViewport().
// this is the modified constructor
public JScrollPane(Component view, int vsbPolicy, int hsbPolicy)
{
setLayout(new ScrollPaneLayout.UIResource());
setVerticalScrollBarPolicy(vsbPolicy);
setHorizontalScrollBarPolicy(hsbPolicy);
if (view != null) {
setViewportView(view);
}
setVerticalScrollBar(createVerticalScrollBar());
setHorizontalScrollBar(createHorizontalScrollBar());
setOpaque(true);
updateUI();
if (!this.getComponentOrientation().isLeftToRight()) {
viewport.setViewPosition(new Point(Integer.MAX_VALUE, 0));
}
}
// this is the modified mutator for the JScrollPane's view Component
public void setViewportView(Component view) {
if (getViewport() == null) {
JViewport port = createViewport();
if (view != null) {
port.setView(view);
}
setViewport(port);
return;
}
getViewport().setView(view);
}
JUSTIFICATION :
Exceptions are an inefficient way to detect error situations; it is worse when a standard use of Swing causes exceptions to be thrown every time, even if they are caught internally.
EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
Swing should use it's own API's in a way that don't trigger exceptions. This is more efficient (faster, which Swing needs to be) and it makes it easier to debug application code.
- duplicates
-
JDK-6244501 JViewport.getView() invokes unnecessary exceptions
- Resolved