/
App.kt
123 lines (114 loc) · 3.36 KB
/
App.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
package example
import com.sun.java.swing.plaf.windows.WindowsScrollBarUI
import java.awt.*
import java.awt.event.InputEvent
import java.awt.event.MouseEvent
import javax.swing.*
import javax.swing.plaf.basic.BasicScrollBarUI
fun makeUI(): Component {
UIManager.put("ScrollBar.allowsAbsolutePositioning", true)
val h1 = "middle mouse click in the track will set"
val h2 = " the position of the track to where the mouse is.\n"
val help = h1 + h2
val txt = help.repeat(100)
val scroll = JScrollPane(JTextArea("override TrackListener#mousePressed(...)\n$txt"))
scroll.verticalScrollBar = object : JScrollBar(Adjustable.VERTICAL) {
override fun updateUI() {
super.updateUI()
val tmp = if (ui is WindowsScrollBarUI) {
AbsolutePositioningWindowsScrollBarUI()
} else {
AbsolutePositioningBasicScrollBarUI()
}
setUI(tmp)
putClientProperty("JScrollBar.fastWheelScrolling", true)
}
}
scroll.horizontalScrollBar = object : JScrollBar(Adjustable.HORIZONTAL) {
override fun updateUI() {
super.updateUI()
val tmp = if (ui is WindowsScrollBarUI) {
AbsolutePositioningWindowsScrollBarUI()
} else {
AbsolutePositioningBasicScrollBarUI()
}
setUI(tmp)
putClientProperty("JScrollBar.fastWheelScrolling", true)
}
}
val p = JPanel(GridLayout(1, 2))
p.add(JScrollPane(JTextArea(txt)))
p.add(scroll)
return JPanel(BorderLayout(5, 5)).also {
it.add(JLabel("ScrollBar.allowsAbsolutePositioning: true"), BorderLayout.NORTH)
it.add(p)
it.preferredSize = Dimension(320, 240)
}
}
private class AbsolutePositioningWindowsScrollBarUI : WindowsScrollBarUI() {
override fun createTrackListener() = object : BasicScrollBarUI.TrackListener() {
override fun mousePressed(e: MouseEvent) {
if (SwingUtilities.isLeftMouseButton(e)) {
super.mousePressed(
MouseEvent(
e.component,
e.id,
e.getWhen(),
InputEvent.BUTTON2_DOWN_MASK xor InputEvent.BUTTON2_MASK,
e.x,
e.y,
e.xOnScreen,
e.yOnScreen,
e.clickCount,
e.isPopupTrigger,
MouseEvent.BUTTON2,
),
)
} else {
super.mousePressed(e)
}
}
}
}
private class AbsolutePositioningBasicScrollBarUI : BasicScrollBarUI() {
override fun createTrackListener() = object : BasicScrollBarUI.TrackListener() {
override fun mousePressed(e: MouseEvent) {
if (SwingUtilities.isLeftMouseButton(e)) {
super.mousePressed(
MouseEvent(
e.component,
e.id,
e.getWhen(),
InputEvent.BUTTON2_DOWN_MASK xor InputEvent.BUTTON2_MASK,
e.x,
e.y,
e.xOnScreen,
e.yOnScreen,
e.clickCount,
e.isPopupTrigger,
MouseEvent.BUTTON2,
),
)
} else {
super.mousePressed(e)
}
}
}
}
fun main() {
EventQueue.invokeLater {
runCatching {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName())
}.onFailure {
it.printStackTrace()
Toolkit.getDefaultToolkit().beep()
}
JFrame().apply {
defaultCloseOperation = WindowConstants.EXIT_ON_CLOSE
contentPane.add(makeUI())
pack()
setLocationRelativeTo(null)
isVisible = true
}
}
}