Skip to content

Commit

Permalink
add: AlwaysShowThumb
Browse files Browse the repository at this point in the history
  • Loading branch information
aterai committed Mar 27, 2022
1 parent c61158c commit 14f2fef
Show file tree
Hide file tree
Showing 2 changed files with 137 additions and 0 deletions.
44 changes: 44 additions & 0 deletions AlwaysShowThumb/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
buildscript {
ext.kotlin_version = 'latest.release'
repositories {
mavenCentral()
}
dependencies {
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
}
}

apply plugin: 'kotlin'
apply plugin: 'application'

// group 'KotlinSwingTips'
// version '1.0-SNAPSHOT'

mainClassName = 'example.AppKt'

defaultTasks 'run'

repositories {
mavenCentral()
}

dependencies {
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8:$kotlin_version"
testImplementation 'junit:junit:4.11'
testImplementation "org.jetbrains.kotlin:kotlin-test-junit:$kotlin_version"
}

sourceCompatibility = 1.8

compileKotlin {
kotlinOptions.jvmTarget = "$sourceCompatibility"
}

compileTestKotlin {
kotlinOptions.jvmTarget = "$sourceCompatibility"
}

jar {
manifest { attributes 'Main-Class': "$mainClassName" }
from { configurations.compileClasspath.collect { it.isDirectory() ? it : zipTree(it) } }
}
93 changes: 93 additions & 0 deletions AlwaysShowThumb/src/main/kotlin/example/App.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
package example

import java.awt.* // ktlint-disable no-wildcard-imports
import javax.swing.* // ktlint-disable no-wildcard-imports

fun makeUI(): Component {
val scroll = JScrollPane(JTree())
scroll.verticalScrollBarPolicy = ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS

val key = "ScrollBar.alwaysShowThumb"
val check = object : JCheckBox(key) {
override fun updateUI() {
super.updateUI()
EventQueue.invokeLater { isSelected = UIManager.getLookAndFeelDefaults().getBoolean(key) }
}
}
check.addActionListener {
UIManager.getLookAndFeelDefaults()[key] = check.isSelected
SwingUtilities.updateComponentTreeUI(scroll)
}

return JPanel(BorderLayout()).also {
val mb = JMenuBar()
mb.add(LookAndFeelUtil.createLookAndFeelMenu())
EventQueue.invokeLater { it.rootPane.jMenuBar = mb }
it.add(check, BorderLayout.NORTH)
it.add(scroll)
it.preferredSize = Dimension(320, 240)
}
}

private object LookAndFeelUtil {
private var lookAndFeel = UIManager.getLookAndFeel().javaClass.name
fun createLookAndFeelMenu() = JMenu("LookAndFeel").also {
val lafRadioGroup = ButtonGroup()
for (lafInfo in UIManager.getInstalledLookAndFeels()) {
it.add(createLookAndFeelItem(lafInfo.name, lafInfo.className, lafRadioGroup))
}
}

private fun createLookAndFeelItem(
lafName: String,
lafClassName: String,
lafGroup: ButtonGroup
): JMenuItem {
val lafItem = JRadioButtonMenuItem(lafName, lafClassName == lookAndFeel)
lafItem.actionCommand = lafClassName
lafItem.hideActionText = true
lafItem.addActionListener { e ->
val m = lafGroup.selection
runCatching {
setLookAndFeel(m.actionCommand)
}.onFailure {
UIManager.getLookAndFeel().provideErrorFeedback(e.source as? Component)
}
}
lafGroup.add(lafItem)
return lafItem
}

@Throws(
ClassNotFoundException::class,
InstantiationException::class,
IllegalAccessException::class,
UnsupportedLookAndFeelException::class
)
private fun setLookAndFeel(lookAndFeel: String) {
val oldLookAndFeel = LookAndFeelUtil.lookAndFeel
if (oldLookAndFeel != lookAndFeel) {
UIManager.setLookAndFeel(lookAndFeel)
LookAndFeelUtil.lookAndFeel = lookAndFeel
updateLookAndFeel()
}
}

private fun updateLookAndFeel() {
for (window in Window.getWindows()) {
SwingUtilities.updateComponentTreeUI(window)
}
}
}

fun main() {
EventQueue.invokeLater {
JFrame().apply {
defaultCloseOperation = WindowConstants.EXIT_ON_CLOSE
contentPane.add(makeUI())
pack()
setLocationRelativeTo(null)
isVisible = true
}
}
}

0 comments on commit 14f2fef

Please sign in to comment.