diff --git a/libraries/kotlin-swing/src/main/kotlin/kotlin/swing/Components.kt b/libraries/kotlin-swing/src/main/kotlin/kotlin/swing/Components.kt index 8dc0536f67e..46cff27d79a 100644 --- a/libraries/kotlin-swing/src/main/kotlin/kotlin/swing/Components.kt +++ b/libraries/kotlin-swing/src/main/kotlin/kotlin/swing/Components.kt @@ -1,11 +1,59 @@ package kotlin.swing +import java.awt.Dimension +import java.awt.Component import javax.swing.JComponent var JComponent.description: String? -get() { - return getAccessibleContext()?.getAccessibleDescription() -} -set(value) { - getAccessibleContext()?.setAccessibleDescription(value) -} \ No newline at end of file + get() { + return getAccessibleContext()?.getAccessibleDescription() + } + set(value) { + getAccessibleContext()?.setAccessibleDescription(value) + } + +var JComponent.preferredWidth: Int + get() = getPreferredSize().width() + set(value) { + setPreferredSize(getPreferredSize().width(value)) + } +var JComponent.preferredHeight: Int + get() = getPreferredSize().height() + set(value) { + setPreferredSize(getPreferredSize().height(value)) + } + +var Component.minimumWidth: Int + get() = getMinimumSize().width() + set(value) { + setMinimumSize(getMinimumSize().width(value)) + } +var Component.minimumHeight: Int + get() = getMinimumSize().height() + set(value) { + setMinimumSize(getMinimumSize().height(value)) + } + +var Component.maximumWidth: Int + get() = getMaximumSize().width() + set(value) { + setMaximumSize(getMaximumSize().width(value)) + } +var Component.maximumHeight: Int + get() = getMaximumSize().height() + set(value) { + setMaximumSize(getMaximumSize().height(value)) + } + +var Component.width: Int + get() = getSize().width() + set(value) { + setSize(getSize().width(value)) + } +var Component.height: Int + get() = getSize().height() + set(value) { + setSize(getSize().height(value)) + } + + diff --git a/libraries/kotlin-swing/src/main/kotlin/kotlin/swing/Dimensions.kt b/libraries/kotlin-swing/src/main/kotlin/kotlin/swing/Dimensions.kt new file mode 100644 index 00000000000..74778e43d9a --- /dev/null +++ b/libraries/kotlin-swing/src/main/kotlin/kotlin/swing/Dimensions.kt @@ -0,0 +1,32 @@ +package kotlin.swing + +import java.awt.Dimension +import java.awt.Component +import javax.swing.JComponent + +/** + * Returns the width of the dimension or zero if its null + */ +fun Dimension?.width(): Int { + return if (this == null) 0 else this.width +} + +/** + * Returns the height of the dimension or zero if its null + */ +fun Dimension?.height(): Int { + return if (this == null) 0 else this.height +} + +/** + * Returns a new dimension with the same height and the given width + */ +fun Dimension?.width(newValue: Int): Dimension { + return Dimension(newValue, height()) +} +/** + * Returns a new dimension with the same width and the given height + */ +fun Dimension?.height(newValue: Int): Dimension { + return Dimension(width(), newValue) +} diff --git a/libraries/kotlin-swing/src/main/kotlin/kotlin/swing/Layouts.kt b/libraries/kotlin-swing/src/main/kotlin/kotlin/swing/Layouts.kt new file mode 100644 index 00000000000..e071902c9ff --- /dev/null +++ b/libraries/kotlin-swing/src/main/kotlin/kotlin/swing/Layouts.kt @@ -0,0 +1,37 @@ +package kotlin.swing + +import java.awt.GridBagConstraints + +/** + * Helper function to create a [GridBagConstraints] + */ +fun gridBagContraints(gridx: Int? = null, gridy: Int? = null, fill: Int? = null, anchor: Int? = null, + gridwidth: Int? = null, gridheight: Int? = null, + weightx: Double? = null, weighty: Double? = null): GridBagConstraints { + val answer = GridBagConstraints() + if (gridx != null) { + answer.gridx = gridx + } + if (gridy != null) { + answer.gridy = gridy + } + if (fill != null) { + answer.fill = fill + } + if (anchor != null) { + answer.anchor = anchor + } + if (gridwidth != null) { + answer.gridwidth = gridwidth + } + if (gridheight != null) { + answer.gridheight = gridheight + } + if (weightx != null) { + answer.weightx = weightx + } + if (weighty != null) { + answer.weighty = weighty + } + return answer +} \ No newline at end of file