|
1 | 1 | package swingjs.a2s; |
2 | 2 |
|
| 3 | +import java.awt.Adjustable; |
| 4 | +import java.awt.Color; |
3 | 5 | import java.awt.Component; |
| 6 | +import java.awt.Point; |
4 | 7 |
|
5 | 8 | import javax.swing.JScrollPane; |
| 9 | +import javax.swing.border.Border; |
| 10 | +import javax.swing.border.LineBorder; |
6 | 11 |
|
7 | 12 | public class ScrollPane extends JScrollPane { |
8 | 13 |
|
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 | + |
30 | 36 | public ScrollPane() { |
31 | | - super(); |
| 37 | + this(SCROLLBARS_AS_NEEDED); |
32 | 38 | } |
33 | | - |
| 39 | + |
34 | 40 | public ScrollPane(int scrollbars) { |
35 | 41 | super(); |
36 | 42 | switch (scrollbars) { |
37 | 43 | 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; |
41 | 47 | 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; |
45 | 51 | case SCROLLBARS_AS_NEEDED: |
46 | | - break; |
| 52 | + break; |
47 | 53 | } |
| 54 | + setBorder(new LineBorder(Color.black)); |
48 | 55 | } |
49 | | - |
| 56 | + |
50 | 57 | @Override |
51 | 58 | public Component add(Component c) { |
52 | 59 | getViewport().add(c); |
53 | 60 | return c; |
54 | 61 | } |
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 | + |
57 | 95 | } |
0 commit comments