java fx2 使用tableview把資料SHOW出來
fx2要使用tableview SHOW資料出來有幾個地方要注意:
1.宣告tableVIew
@FXML // fx:id="table1"
private TableView<EmpCard> table1; // Value injected by FXMLLoader
2.宣告有那些COLUMN要SHOW出來
@FXML // fx:id="autoId"
private TableColumn<EmpCard, Integer> autoId; // Value injected by FXMLLoader
@FXML // fx:id="cardNo"
private TableColumn<EmpCard, String> cardNo; // Value injected by FXMLLoader
@FXML // fx:id="serNo"
private TableColumn<EmpCard, String> serNo; // Value injected by FXMLLoader
@FXML // fx:id="empNo"
private TableColumn<EmpCard, String> empNo; // Value injected by FXMLLoader
@FXML // fx:id="empNo"
private TableColumn<EmpCard, String> empName; // Value injected by FXMLLoader
3.設定COLUMN要SHOW的對應欄位
autoId.setCellValueFactory(new PropertyValueFactory<EmpCard, Integer>("autoId"));
cardNo.setCellValueFactory(new PropertyValueFactory<EmpCard, String>("cardNo"));
serNo.setCellValueFactory(new PropertyValueFactory<EmpCard, String>("serNo"));
empNo.setCellValueFactory(new PropertyValueFactory<EmpCard, String>("empNo"));
empName.setCellValueFactory(new PropertyValueFactory<EmpCard, String>("empName"));
4.在畫面的FXML把每個COLUMN的對應FXID指定好
<TableView fx:id="table1" prefHeight="511.0" prefWidth="800.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="89.0">
<columns>
<TableColumn prefWidth="75.0" text="autoId" fx:id="autoId"/>
<TableColumn prefWidth="75.0" text="門禁卡號" fx:id="cardNo"/>
<TableColumn prefWidth="75.0" text="內碼" fx:id="serNo"/>
<TableColumn prefWidth="75.0" text="使用員工ID" fx:id="empNo"/>
<TableColumn prefWidth="100.0" text="使用員工姓名" fx:id="empName"/>
</columns>
</TableView>
5.把資料ADD進table1去
BeanListHandler<EmpCard> h = new BeanListHandler<EmpCard>(EmpCard.class);
Object[] ps = new Object[]{new String("Z")};
cardLists = (ArrayList<EmpCard>) qr.query(conn, sql, h, ps);
configureTable();-->做第3個步驟的動作
table1.getItems().addAll(cardLists);
1.宣告tableVIew
@FXML // fx:id="table1"
private TableView<EmpCard> table1; // Value injected by FXMLLoader
2.宣告有那些COLUMN要SHOW出來
@FXML // fx:id="autoId"
private TableColumn<EmpCard, Integer> autoId; // Value injected by FXMLLoader
@FXML // fx:id="cardNo"
private TableColumn<EmpCard, String> cardNo; // Value injected by FXMLLoader
@FXML // fx:id="serNo"
private TableColumn<EmpCard, String> serNo; // Value injected by FXMLLoader
@FXML // fx:id="empNo"
private TableColumn<EmpCard, String> empNo; // Value injected by FXMLLoader
@FXML // fx:id="empNo"
private TableColumn<EmpCard, String> empName; // Value injected by FXMLLoader
3.設定COLUMN要SHOW的對應欄位
autoId.setCellValueFactory(new PropertyValueFactory<EmpCard, Integer>("autoId"));
cardNo.setCellValueFactory(new PropertyValueFactory<EmpCard, String>("cardNo"));
serNo.setCellValueFactory(new PropertyValueFactory<EmpCard, String>("serNo"));
empNo.setCellValueFactory(new PropertyValueFactory<EmpCard, String>("empNo"));
empName.setCellValueFactory(new PropertyValueFactory<EmpCard, String>("empName"));
4.在畫面的FXML把每個COLUMN的對應FXID指定好
<TableView fx:id="table1" prefHeight="511.0" prefWidth="800.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="89.0">
<columns>
<TableColumn prefWidth="75.0" text="autoId" fx:id="autoId"/>
<TableColumn prefWidth="75.0" text="門禁卡號" fx:id="cardNo"/>
<TableColumn prefWidth="75.0" text="內碼" fx:id="serNo"/>
<TableColumn prefWidth="75.0" text="使用員工ID" fx:id="empNo"/>
<TableColumn prefWidth="100.0" text="使用員工姓名" fx:id="empName"/>
</columns>
</TableView>
5.把資料ADD進table1去
BeanListHandler<EmpCard> h = new BeanListHandler<EmpCard>(EmpCard.class);
Object[] ps = new Object[]{new String("Z")};
cardLists = (ArrayList<EmpCard>) qr.query(conn, sql, h, ps);
configureTable();-->做第3個步驟的動作
table1.getItems().addAll(cardLists);
留言
張貼留言