forked from aterai/kotlin-swing-tips
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
137 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) } } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} | ||
} |