当在编译之前固定列时,我可以生成TableView。 但是,当实例启动后列数不固定时,我无法创建TableView。
for each(Window win in Application.Current.Windows)
{
string windowType = win.GetType().ToString();
if (!windowType.Equals(nameSpace + "." + ABC))
{
ABC abc= new ABC();
abc.Show();
}
}
的大小在每个实例中都可以变化unitArray
的大小都与VariableData
相同main.kt
unitArray
resource / fxml / VBox.fxml
package tDynamicTable
import javafx.application.Application
import javafx.collections.FXCollections
import javafx.fxml.FXMLLoader
import javafx.scene.*
import javafx.scene.control.*
import javafx.scene.control.cell.*
import javafx.scene.layout.*
import javafx.stage.*
fun main(args : Array<String>) {
println("Dynamic Columns by data")
Application.launch(DynamicTable().javaClass, *args)
}
class DynamicTable : Application() {
override fun start(primaryStage: Stage) {
primaryStage.title = "Dynamic Table"
primaryStage.isAlwaysOnTop = true
val fxml = javaClass.getResource("/fxml/VBox.fxml")
val root: Parent = FXMLLoader.load(fxml)
val scene = Scene(root)
val vBox = root.lookup("#VBox") as VBox
val tableView: TableView<VariableData> = TableView()
val colStatus: TableColumn<VariableData,Boolean> = TableColumn()
val colName: TableColumn<VariableData,String> = TableColumn()
// Should load data here - size of `unitArray` is not defined before here
var aList= FXCollections.observableArrayList<VariableData>()
// Every elements in aList have same size of unitArray
aList.add(a)
aList.add(b)
aList.add(c)
var colSize = aList[0].unitArray.size
colStatus.text = "Status"
colStatus.cellValueFactory = PropertyValueFactory("status")
colName.text = "Name"
colName.cellValueFactory = PropertyValueFactory("name")
tableView.columns.add(colStatus)
tableView.columns.add(colName)
for (i in 1..colSize) {
// I may have to do something here
// tableView.columns.add(TableColumn())
}
tableView.items = aList
vBox.children.addAll(tableView)
primaryStage.scene = scene
primaryStage.show()
}
}
class VariableData (
val status: Boolean,
val name: String,
val unitArray: Array<UnitData>
)
class UnitData (
val type: FileType,
val path: String
)
enum class FileType { ARCHIVE, FILE, UNKNOWN }
fun FileType.toString() = when (this) {
FileType.ARCHIVE -> "A"
FileType.FILE -> "F"
FileType.UNKNOWN -> "U"
}
val a = VariableData(true, "A",
arrayOf(
UnitData(FileType.ARCHIVE, "a.zip"),
UnitData(FileType.FILE,"a.txt")))
val b = VariableData(true, "B",
arrayOf(
UnitData(FileType.ARCHIVE, "b.zip"),
UnitData(FileType.FILE,"b.txt")))
val c = VariableData(true, "C",
arrayOf(
UnitData(FileType.UNKNOWN, "c.exe"),
UnitData(FileType.FILE,"c.txt")))
我尝试遵循JavaFX TableView dynamic column and data values和How to add dynamic columns and rows to TableView in java fxml,但这可能不适用于自定义数据,而仅适用于字符串数据。