Skip to content

Commit 9259f6c

Browse files
hansonrhansonr
authored andcommitted
ScrollPane/JScrollPane fixes for painted components needing paint()
1 parent 3f70c3b commit 9259f6c

File tree

2 files changed

+74
-33
lines changed

2 files changed

+74
-33
lines changed
Lines changed: 71 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,57 +1,95 @@
11
package swingjs.a2s;
22

3+
import java.awt.Adjustable;
4+
import java.awt.Color;
35
import java.awt.Component;
6+
import java.awt.Point;
47

58
import javax.swing.JScrollPane;
9+
import javax.swing.border.Border;
10+
import javax.swing.border.LineBorder;
611

712
public class ScrollPane extends JScrollPane {
813

9-
/**
10-
* Specifies that horizontal/vertical scrollbar should be shown
11-
* only when the size of the child exceeds the size of the scrollpane
12-
* in the horizontal/vertical dimension.
13-
*/
14-
public static final int SCROLLBARS_AS_NEEDED = 0;
15-
16-
/**
17-
* Specifies that horizontal/vertical scrollbars should always be
18-
* shown regardless of the respective sizes of the scrollpane and child.
19-
*/
20-
public static final int SCROLLBARS_ALWAYS = 1;
21-
22-
/**
23-
* Specifies that horizontal/vertical scrollbars should never be shown
24-
* regardless of the respective sizes of the scrollpane and child.
25-
*/
26-
public static final int SCROLLBARS_NEVER = 2;
27-
28-
public void isAWT() {}
29-
14+
/**
15+
* Specifies that horizontal/vertical scrollbar should be shown only when the
16+
* size of the child exceeds the size of the scrollpane in the
17+
* horizontal/vertical dimension.
18+
*/
19+
public static final int SCROLLBARS_AS_NEEDED = 0;
20+
21+
/**
22+
* Specifies that horizontal/vertical scrollbars should always be shown
23+
* regardless of the respective sizes of the scrollpane and child.
24+
*/
25+
public static final int SCROLLBARS_ALWAYS = 1;
26+
27+
/**
28+
* Specifies that horizontal/vertical scrollbars should never be shown
29+
* regardless of the respective sizes of the scrollpane and child.
30+
*/
31+
public static final int SCROLLBARS_NEVER = 2;
32+
33+
public void isAWT() {
34+
}
35+
3036
public ScrollPane() {
31-
super();
37+
this(SCROLLBARS_AS_NEEDED);
3238
}
33-
39+
3440
public ScrollPane(int scrollbars) {
3541
super();
3642
switch (scrollbars) {
3743
case SCROLLBARS_NEVER:
38-
setVerticalScrollBarPolicy(VERTICAL_SCROLLBAR_NEVER);
39-
setHorizontalScrollBarPolicy(HORIZONTAL_SCROLLBAR_NEVER);
40-
break;
44+
setVerticalScrollBarPolicy(VERTICAL_SCROLLBAR_NEVER);
45+
setHorizontalScrollBarPolicy(HORIZONTAL_SCROLLBAR_NEVER);
46+
break;
4147
case SCROLLBARS_ALWAYS:
42-
setVerticalScrollBarPolicy(VERTICAL_SCROLLBAR_ALWAYS);
43-
setHorizontalScrollBarPolicy(HORIZONTAL_SCROLLBAR_ALWAYS);
44-
break;
48+
setVerticalScrollBarPolicy(VERTICAL_SCROLLBAR_ALWAYS);
49+
setHorizontalScrollBarPolicy(HORIZONTAL_SCROLLBAR_ALWAYS);
50+
break;
4551
case SCROLLBARS_AS_NEEDED:
46-
break;
52+
break;
4753
}
54+
setBorder(new LineBorder(Color.black));
4855
}
49-
56+
5057
@Override
5158
public Component add(Component c) {
5259
getViewport().add(c);
5360
return c;
5461
}
55-
56-
62+
63+
public Adjustable getVAdjustable() {
64+
return getVerticalScrollBar();// vAdjustable;
65+
}
66+
67+
public Adjustable getHAdjustable() {
68+
return getHorizontalScrollBar();// hAdjustable;
69+
}
70+
71+
public void setScrollPosition(int x, int y) {
72+
synchronized (getTreeLock()) {
73+
if (getComponentCount() == 0) {
74+
throw new NullPointerException("child is null");
75+
}
76+
getHorizontalScrollBar().setValue(x);
77+
getVerticalScrollBar().setValue(y);
78+
}
79+
}
80+
81+
public void setScrollPosition(Point p) {
82+
setScrollPosition(p.x, p.y);
83+
}
84+
85+
public Point getScrollPosition() {
86+
synchronized (getTreeLock()) {
87+
if (getComponentCount() == 0) {
88+
throw new NullPointerException("child is null");
89+
}
90+
return new Point(getHorizontalScrollBar().getValue(),
91+
getVerticalScrollBar().getValue());
92+
}
93+
}
94+
5795
}

sources/net.sf.j2s.java.core/src/swingjs/plaf/JSScrollPaneUI.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1249,6 +1249,9 @@ private void hsbStateChanged(JViewport viewport, ChangeEvent e) {
12491249

12501250
private void viewportStateChanged(ChangeEvent e) {
12511251
syncScrollPaneWithViewport();
1252+
// painted label, button, or canvas anywhere in the tree will need to be repainted after the shift in origin.
1253+
if (jc.selfOrChildIsPainted())
1254+
jc.repaint();
12521255
}
12531256

12541257
//

0 commit comments

Comments
 (0)