当前位置 主页 > 服务器问题 > Linux/apache问题 > 最大化 缩小
我们需要实现的效果是,在加载这个页面时,表格中自动加载数据。填写我们创建的控制器类如下:
package sample;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.fxml.FXML;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.TextField;
import java.time.LocalDate;
public class MainController {
@FXML
private TextField username;
@FXML
private TextField password;
@FXML
private TableView<Courses> allCoursesTable;
@FXML
private TableColumn<Courses,String> CourseNameAttribute;
@FXML
private TableColumn<Courses,String> DepartmentAttribute;
@FXML
private TableColumn<Courses,String> LectureAttribute;
@FXML
private TableColumn<Courses, LocalDate> TimeAttribute;
@FXML
private TableColumn<Courses,String> LocationAttribute;
@FXML
private TableColumn<Courses,Number> CourseIDAttribute;
@FXML
private void initialize() {
ObservableList<Courses> data= FXCollections.observableArrayList(new Courses("MACHINE LEARNING","COMPUTER","ZHANGYI",LocalDate.of(2012,01,01),"A101",4011));//创建ObservableList对象,将数据装进去
CourseNameAttribute.setCellValueFactory(cellData->cellData.getValue().nameProperty());
DepartmentAttribute.setCellValueFactory(cellData->cellData.getValue().departmentProperty());
LectureAttribute.setCellValueFactory(cellData->cellData.getValue().lecturerProperty());
TimeAttribute.setCellValueFactory(cellData->cellData.getValue().timeProperty());
LocationAttribute.setCellValueFactory(cellData->cellData.getValue().locationProperty());
CourseIDAttribute.setCellValueFactory(cellData->cellData.getValue().IDProperty());
allCoursesTable.setItems(data);//加载数据
}
}
这就是为什么要用单独的控制器类了,否则initialize方法会在每次创建页面的时候都加载一次,而只有某一个页面有我们说的这些Tabel和Column对象,会报错的。
写一个方法来跳转到这个页面。
如何实现页面之间的传参呢?
对于要传参的页面,我们就不能直接获取parent对象了,而是先要获取FXMLLoader对象:
FXMLLoader fxmlLoader = new FXMLLoader(getClass().getClassLoader().getResource("MainPanel.fxml"));
Parent root = fxmlLoader.load();
MainController mc=fxmlLoader.getController();
注意这个MainController是我为这个页面写的控制器类
获取了Controller对象后,我们就可以调用方法,将参数传进去了:
mc.setPassword(pass);
mc.setUsername(user);
mc.handleAllCourses();
我在MainController这个类中是这样写的:
public void setUsername(String username){
usernameString=username;
}
public void setPassword(String password){
passwordString=password;
}
这就是入门的FX教程了,有了这些基本的方法,相信设计一个稍微复杂一点的桌面应用程序已经不是问题了。
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持IIS7站长之家。