Skip to content

Commit

Permalink
an attempt to build a sample JavaFX app for JDBC/SQLite, #32
Browse files Browse the repository at this point in the history
  • Loading branch information
Miha-x64 committed Jul 29, 2018
1 parent be9fe05 commit 952f264
Showing 1 changed file with 41 additions and 5 deletions.
@@ -1,17 +1,51 @@
package net.aquadc.properties.fx.sqlSample

import com.jfoenix.controls.JFXListCell
import com.jfoenix.controls.JFXListView
import javafx.application.Application
import javafx.collections.FXCollections
import javafx.scene.Scene
import javafx.stage.Stage
import net.aquadc.properties.fx.fx
import net.aquadc.properties.mapWith
import net.aquadc.properties.sql.*
import java.sql.Connection
import java.sql.DriverManager
import java.sql.Statement


fun main(args: Array<String>) {
DriverManager.getConnection("jdbc:sqlite:sample.db").use { conn ->
createNeededTables(conn)
val sess = JdbcSqliteSession(conn)
fillIfEmpty(sess)
class SqliteApp : Application() {

private val connection = DriverManager.getConnection("jdbc:sqlite:sample.db").also(::createNeededTables)
private val sess = JdbcSqliteSession(connection).also(::fillIfEmpty)

override fun start(stage: Stage) {
stage.scene = Scene(
JFXListView<Human>().apply {
items = FXCollections.observableArrayList(sess.select(HumanTable).value)
setCellFactory { object : JFXListCell<Human>() {
override fun updateItem(item: Human?, empty: Boolean) {
super.updateItem(item, empty)
if (item != null) {
textProperty().bind(item.name.mapWith(item.surname) { n, s -> "$n $s" }.fx())
}
}
} }
},
400.0, 600.0)
stage.show()
}

override fun stop() {
connection.close()
}

companion object {
@JvmStatic fun main(args: Array<String>) {
launch(SqliteApp::class.java)
}
}

}

private fun createNeededTables(conn: Connection) {
Expand Down Expand Up @@ -49,6 +83,8 @@ private fun fillIfEmpty(session: Session) {
if (session.count(HumanTable).value == 0L) {
session.transaction { transaction ->
transaction.insertHuman("Stephen", "Hawking", null)
transaction.insertHuman("Albert", "Einstein", null)
transaction.insertHuman("Dmitri", "Mendeleev", null)
}
}
}

0 comments on commit 952f264

Please sign in to comment.