1.表格視圖 TableView
ableView是JavaFX提供的一個(gè)強(qiáng)大的控件,可以用于顯示表格數(shù)據(jù)。它通過為TableView設(shè)定items屬性(存儲(chǔ)行數(shù)據(jù)的ObservableList對象)和列屬性(TableColumn對象)來完成數(shù)據(jù)填充與展示。
以下是一個(gè)簡單的TableView的使用示例:
public class TableViewExample extends Application {
//創(chuàng)建表格
private TableView<Person> table = new TableView<Person>();
private ObservableList<Person> data =
FXCollections.observableArrayList(
new Person("John", "Doe"),
new Person("Jane", "Deer"));
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage stage) {
//構(gòu)建場景
Scene scene = new Scene(new Group());
stage.setTitle("Table View Sample");
stage.setWidth(300);
stage.setHeight(500);
//創(chuàng)建列
TableColumn firstNameCol = new TableColumn("First Name");
firstNameCol.setMinWidth(100);
firstNameCol.setCellValueFactory(
new PropertyValueFactory<Person, String>("firstName"));
TableColumn lastNameCol = new TableColumn("Last Name");
lastNameCol.setMinWidth(100);
lastNameCol.setCellValueFactory(
new PropertyValueFactory<Person, String>("lastName"));
//表格加入列
table.setItems(data);
table.getColumns().addAll(firstNameCol, lastNameCol);
((Group) scene.getRoot()).getChildren().addAll(table);
stage.setScene(scene);
stage.show();
}
//對象
public static class Person {
private final String firstName;
private final String lastName;
private Person(String fName, String lName) {
this.firstName = fName;
this.lastName = lName;
}
public String getFirstName() {
return firstName;
}
public String getLastName() {
return lastName;
}
}
}
?1.1 TableView 選中事件
public class TableSelectApp extends Application {
@Override
public void start(Stage primaryStage) throws Exception {
//創(chuàng)建表格
TableView<Item> tblItems = new TableView<>();
tblItems.setColumnResizePolicy(TableView.CONSTRAINED_RESIZE_POLICY);
VBox.setVgrow(tblItems, Priority.ALWAYS );
//創(chuàng)建列
TableColumn<Item, String> colSKU = new TableColumn<>("SKU");
TableColumn<Item, String> colDescr = new TableColumn<>("Item");
TableColumn<Item, Float> colPrice = new TableColumn<>("Price");
TableColumn<Item, Boolean> colTaxable = new TableColumn<>("Tax");
//列對應(yīng)實(shí)體屬性
colSKU.setCellValueFactory( new PropertyValueFactory<>("sku") );
colDescr.setCellValueFactory( new PropertyValueFactory<>("descr") );
colPrice.setCellValueFactory( new PropertyValueFactory<>("price") );
colTaxable.setCellValueFactory( new PropertyValueFactory<>("taxable") );
//表格加入列
tblItems.getColumns().addAll(
colSKU, colDescr, colPrice, colTaxable
);
//添加數(shù)據(jù)
tblItems.getItems().addAll(
new Item("KBD-0455892", "Mechanical Keyboard", 100.0f, true),
new Item( "145256", "Product Docs", 0.0f, false ),
new Item( "OR-198975", "O-Ring (100)", 10.0f, true)
);
//創(chuàng)建按鈕
Button btnInventory = new Button("Inventory");
Button btnCalcTax = new Button("Tax");
//按鈕是否可用屬性綁定表格選中的數(shù)據(jù)對應(yīng)的descr屬性
btnInventory.disableProperty().bind(
tblItems.getSelectionModel().selectedItemProperty().isNull()
);
//按鈕是否可用屬性綁定表格選中的數(shù)據(jù)對應(yīng)的taxable屬性
btnCalcTax.disableProperty().bind(
tblItems.getSelectionModel().selectedItemProperty().isNull().or(
Bindings.select(
tblItems.getSelectionModel().selectedItemProperty(),
"taxable"
).isEqualTo(false)
)
);
//構(gòu)建場景
HBox buttonHBox = new HBox( btnInventory, btnCalcTax );
buttonHBox.setSpacing( 8 );
VBox vbox = new VBox( tblItems, buttonHBox );
vbox.setPadding( new Insets(10) );
vbox.setSpacing( 10 );
Scene scene = new Scene(vbox);
primaryStage.setTitle("TableSelectApp");
primaryStage.setScene( scene );
primaryStage.setHeight( 376 );
primaryStage.setWidth( 667 );
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
public class Item {
private final String sku;
private final String descr;
private final Float price;
private final Boolean taxable;
public Item(String sku, String descr, Float price, Boolean taxable) {
this.sku = sku;
this.descr = descr;
this.price = price;
this.taxable = taxable;
}
public String getSku() {
return sku;
}
public String getDescr() {
return descr;
}
public Float getPrice() {
return price;
}
public Boolean getTaxable() {
return taxable;
}
}
}
演示應(yīng)用程序是一個(gè)TableView
和一對按鈕。有TableView
四個(gè)表列:SKU、商品、價(jià)格、稅費(fèi)。該TableView
圖顯示三行中的三個(gè)對象:機(jī)械鍵盤、產(chǎn)品文檔、O 形圈。以下屏幕截圖顯示了應(yīng)用程序啟動(dòng)后立即出現(xiàn)的情況。按鈕Inventory綁定了Item屬性,Tax按鈕綁定了Tax屬性
?
點(diǎn)擊第一行,兩個(gè)按鈕都被激活
文章來源:http://www.zghlxwxcb.cn/news/detail-726801.html
?若此文檔不夠詳細(xì),?可以參考十分鐘教你JAVAFX基礎(chǔ)入門_嗶哩嗶哩_bilibili?文章來源地址http://www.zghlxwxcb.cn/news/detail-726801.html
到了這里,關(guān)于JavaFx 用戶界面控件3——TableView的文章就介紹完了。如果您還想了解更多內(nèi)容,請?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!