fxml和xml一样吗(解析FXML表格的使用)
fxml和xml一样吗(解析FXML表格的使用)public class userSelectControll { @FXML private TextField selectbook; @FXML private Button commit; @FXML private Button backbtn; @FXML private TableColumn<book String > pressCol; @FXML private TableColumn<book Integer> idCol; @FXML private TableColumn<book String > authorCol; @FXML private TableColumn<book String >
解析 FXML 表格的使用,图书列表功能的实现1.基本操作- 安装
首先要配置FXML,安装JavaFX Scene Builder ,这里可以参考网上教程
- 创建
FXML使用的基本步骤(基本)
- xxx.fxml文件
- xxxApplication文件
- xxxControll文件
扩展
- 可以建立css文件,在文件中写自己的css样式
- 第一步: 选中fxml文件右击选择Open In SceneBuilder ,即可打开视图页面,如图
- 第二步骤
然后选择tableview 创建表格,在设计完表格之后,选择view中show sample controller skeleton 复制代码,粘贴到xxxControll文件中
- xxxControll中展示的文件
public class userSelectControll {
@FXML
private TextField selectbook;
@FXML
private Button commit;
@FXML
private Button backbtn;
@FXML
private TableColumn<book String > pressCol;
@FXML
private TableColumn<book Integer> idCol;
@FXML
private TableColumn<book String > authorCol;
@FXML
private TableColumn<book String > nameCol;
@FXML
private TableView<book> myTable;
@FXML
private TableColumn<book Integer> borrowCol;
@FXML
private TableColumn<book Float> priceCol;
@FXML
void commitClick(ActionEvent event) {
String sb=selectbook.getText();
System.out.println(sb);
System.out.println("查找如下:");
if (bookDao.selectAllStudent(sb).isEmpty()==false){
ArrayList<book> list = bookDao.selectAllStudent(sb);
idCol.setCellValueFactory(new PropertyValueFactory<>("bid"));
nameCol.setCellValueFactory(new PropertyValueFactory<>("bname"));
authorCol.setCellValueFactory(new PropertyValueFactory<>("author"));
pressCol.setCellValueFactory(new PropertyValueFactory<>("publish"));
priceCol.setCellValueFactory(new PropertyValueFactory<>("bprice"));
borrowCol.setCellValueFactory(new PropertyValueFactory<>("isborrow"));
myTable.getItems().clear();//清空表格里面所有数据
myTable.getItems().addAll(list);
userMenuApplication open1=new userMenuApplication();
try {
} catch (Exception e) {
e.printStackTrace();
}
}else{
Alert alert = new Alert(Alert.AlertType.INFORMATION "未查找到此书!");
alert.showAndWait();
try {
} catch (Exception e) {
e.printStackTrace();
}
}
}
@FXML
void backbtnClick(ActionEvent event) throws Exception {
userMenuApplication open = new userMenuApplication();
open.start(new Stage());
userSelectApplication.st.hide();//选择后开启一个新的窗体的同时,关闭当前窗体
}
}
- 解析表格的使用
- 在图二设计视图的使用,每个表格对应的id和fxid 都要设置,并且要一致,Contrller class 要选择自己创建的xxxcontrller
- 表格中:idCol.setCellValueFactory(new PropertyValueFactory<>("bid"));
- idCol 对应的是自己在图二设置的id,bid对应的是要展示数据库中的字段名称
- tableColumn中传入的泛型第一个是对象,第二个是数据类型,注意如果数据类型是int 要写成Integer 引用数据类型
@FXML
private TableColumn<book Integer> borrowCol;
- 关联的是自己要打印在报个中的对象
@FXML
private TableView<book> myTable;
- 绑定的是图书表的字段名 通过这个方法,可以将获取到的数据库字段中的属性绑定到表格中
idCol.setCellValueFactory(new PropertyValueFactory<>("bid"));
- 展示表格,注意这里要先清空在展示表格信息,不然内容会重复,list是一个集合,存放图书对应的数据
myTable.getItems().clear();//清空表格里面所有数据
myTable.getItems().addAll(list);
- book 基础包的封装,返回的是book类型的集合
这样就可以通过fxml展示一个表格,结果: