All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.marid.fx.extensions.TableExtensions.kt Maven / Gradle / Ivy

There is a newer version: 0.9.8.10
Show newest version
package org.marid.fx.extensions

import javafx.beans.value.ObservableValue
import javafx.collections.ObservableList
import javafx.scene.control.TableColumn
import javafx.scene.control.TableView
import javafx.scene.input.KeyCode
import javafx.scene.input.KeyEvent
import javafx.scene.input.MouseEvent
import javafx.util.Callback
import org.marid.fx.i18n.localized

fun  TableView.column(width: Int, text: String, value: (T) -> ObservableValue) =
  column(width, text.localized, value)

fun  TableView.column(width: Int, text: ObservableValue, value: (T) -> ObservableValue) =
  TableColumn()
    .apply {
      minWidth = width.toDouble() * 0.9
      prefWidth = width.toDouble()
      maxWidth = width.toDouble() * 3.0
      textProperty().bind(text)
      cellValueFactory = Callback { value(it.value) }
    }
    .also { columns += it }

fun  TableView.installEdit(handler: (ObservableList) -> Unit) {
  addEventHandler(KeyEvent.KEY_PRESSED) {
    if ((it.code == KeyCode.ENTER || it.code == KeyCode.F2) && !it.isAltered) {
      if (selectionModel.selectedItems.isNotEmpty()) {
        handler(selectionModel.selectedItems)
      }
    }
  }
  addEventHandler(MouseEvent.MOUSE_CLICKED) {
    if (it.clickCount == 2 && !it.isAltered) {
      if (selectionModel.selectedItems.isNotEmpty()) {
        handler(selectionModel.selectedItems)
      }
    }
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy