-
Notifications
You must be signed in to change notification settings - Fork 5
/
App.kt
165 lines (152 loc) · 4.59 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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
package example
import java.awt.*
import java.awt.image.BufferedImage
import javax.imageio.ImageIO
import javax.swing.*
import javax.swing.plaf.ColorUIResource
import javax.swing.table.DefaultTableModel
import javax.swing.table.TableCellEditor
import javax.swing.table.TableCellRenderer
private val alphaZero = Color(0x0, true)
private val color = Color(255, 0, 0, 50)
fun makeUI(): Component {
val columnNames = arrayOf("String", "Integer", "Boolean")
val data = arrayOf(
arrayOf("aaa", 12, true),
arrayOf("bbb", 5, false),
arrayOf("CCC", 92, true),
arrayOf("DDD", 0, false),
)
val model = object : DefaultTableModel(data, columnNames) {
override fun getColumnClass(column: Int) = getValueAt(0, column).javaClass
}
val table = object : JTable(model) {
override fun updateUI() {
setSelectionForeground(ColorUIResource(Color.RED))
setSelectionBackground(ColorUIResource(Color.RED))
super.updateUI()
val m = getModel()
for (i in 0..<m.columnCount) {
(getDefaultRenderer(m.getColumnClass(i)) as? Component)?.also {
SwingUtilities.updateComponentTreeUI(it)
}
}
}
override fun prepareEditor(
editor: TableCellEditor,
row: Int,
column: Int,
): Component {
val c = super.prepareEditor(editor, row, column)
if (c is JTextField) {
c.isOpaque = false
} else if (c is JCheckBox) {
c.setBackground(getSelectionBackground())
}
return c
}
}
table.autoCreateRowSorter = true
table.rowSelectionAllowed = true
table.fillsViewportHeight = true
table.setSelectionMode(ListSelectionModel.SINGLE_INTERVAL_SELECTION)
table.setDefaultRenderer(Boolean::class.javaObjectType, TranslucentBooleanRenderer())
table.isOpaque = false
table.background = alphaZero
val texture = makeImageTexture()
val scroll = object : JScrollPane(table) {
override fun createViewport() = object : JViewport() {
override fun paintComponent(g: Graphics) {
val g2 = g.create() as? Graphics2D ?: return
g2.paint = texture
g2.fillRect(0, 0, width, height)
g2.dispose()
super.paintComponent(g)
}
}
}
scroll.viewport.isOpaque = false
scroll.viewport.background = alphaZero
val check = JCheckBox("setBackground(new Color(255, 0, 0, 50))")
check.addActionListener { e ->
val b = (e.source as? JCheckBox)?.isSelected ?: false
table.background = if (b) color else alphaZero
}
return JPanel(BorderLayout()).also {
it.add(check, BorderLayout.NORTH)
it.add(scroll)
it.preferredSize = Dimension(320, 240)
}
}
private fun makeImageTexture(): TexturePaint {
// unkaku_w.png https://www.viva-edo.com/komon/edokomon.html
val path = "example/unkaku_w.png"
val url = Thread.currentThread().contextClassLoader.getResource(path)
val bi = url?.openStream()?.use(ImageIO::read) ?: makeMissingImage()
return TexturePaint(bi, Rectangle(bi.width, bi.height))
}
private fun makeMissingImage(): BufferedImage {
val missingIcon = UIManager.getIcon("OptionPane.errorIcon")
val w = missingIcon.iconWidth
val h = missingIcon.iconHeight
val bi = BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB)
val g2 = bi.createGraphics()
missingIcon.paintIcon(null, g2, 0, 0)
g2.dispose()
return bi
}
private class TranslucentBooleanRenderer :
JCheckBox(),
TableCellRenderer {
override fun updateUI() {
super.updateUI()
horizontalAlignment = CENTER
isBorderPainted = true
border = BorderFactory.createEmptyBorder(1, 1, 1, 1)
}
override fun getTableCellRendererComponent(
table: JTable,
value: Any?,
isSelected: Boolean,
hasFocus: Boolean,
row: Int,
column: Int,
): Component {
horizontalAlignment = CENTER
if (isSelected) {
isOpaque = true
foreground = table.selectionForeground
super.setBackground(table.selectionBackground)
} else {
isOpaque = false
foreground = table.foreground
background = table.background
}
setSelected(value == true)
return this
}
override fun paintComponent(g: Graphics) {
if (!isOpaque) {
g.color = background
g.fillRect(0, 0, width, height)
}
super.paintComponent(g)
}
}
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
}
}
}