![](/img/trans.png)
[英]How to add background-image to AnchorPane by using Scene Builder in JavaFX?
[英]How to add Linked list of objects to TableView using javafx/scene builder?
我正在开发一个相当简单的 javafx 应用程序,它有几个不同的场景,我的第一个场景是将 Show object 添加到通用链表中,然后在第二个场景中将其中一个节目的表演添加到 ZA8CFDE6331BD49EB66AC96F8911C (在节目中添加日场或晚间表演 Annie)
我已经创建了第一个场景,并且可以将 Show 对象添加到链接列表中,但是我无法使用为用户创建的节目填充第二个场景中的 tableview 到 select 并添加性能
这是我添加显示 controller 的样子:
public class AddShowController {
@FXML private TextField titleField;
@FXML private TextField cirField,stlField, balField;
@FXML private Slider runTimeSlider;
@FXML private DatePicker startDatePicker, endDatePicker;
TheLinkedList<Show> showList = new TheLinkedList<>();
private static AddShowController instance;
public AddShowController() {
instance = this;
}
public static AddShowController getInstance(){
return instance;
}
public void addShowButtonPushed (ActionEvent e){
showList.addElement(new Show (titleField.getText(),
(int) runTimeSlider.getValue(),
startDatePicker.getValue().toString(),
endDatePicker.getValue().toString(),
cirField.getText(),
stlField.getText(),
balField.getText()));
}
如您所见,我已尝试创建 controller 的实例,因此我可以从添加性能 controller 访问链接列表,但没有任何乐趣。
这是添加性能 controller:
public class AddPerfController {
@FXML private ChoiceBox perfTimeChoiceBox;
@FXML private TableView<Show> showTable;
@FXML private TableColumn<Show, String> titleColumn;
@FXML private TableColumn<Show, String> runTimeColumn;
@FXML private TableColumn<Show, String> startDateColumn;
@FXML private TableColumn<Show, String> endDateColumn;
@FXML private TableColumn<Show, String> cirTicketPriceColumn;
@FXML private TableColumn<Show, String> stlTicketPriceColumn;
@FXML private TableColumn<Show, String> balTicketPriceColumn;
private ObservableList<Show> showData = FXCollections.observableList(AddShowController.getInstance().showList);
public void initialize() {
perfTimeChoiceBox.getItems().addAll("Matinee","Evening");
perfTimeChoiceBox.setValue("Matinee");
titleColumn.setCellValueFactory(new PropertyValueFactory<>("title"));
runTimeColumn.setCellValueFactory(new PropertyValueFactory<>("runningTime"));
startDateColumn.setCellValueFactory(new PropertyValueFactory<>("startDate"));
endDateColumn.setCellValueFactory(new PropertyValueFactory<>("endDate"));
cirTicketPriceColumn.setCellValueFactory(new PropertyValueFactory<>("cirTicketPrice"));
stlTicketPriceColumn.setCellValueFactory(new PropertyValueFactory<>("stlTicketPrice"));
balTicketPriceColumn.setCellValueFactory(new PropertyValueFactory<>("balTicketPrice"));
showData.add(new Show("Annie", 100,"12/12/2019", "14/12/2019","10","15", "20"));
showTable.setItems(showData);
}
我们不确定这是你想要做的,但看看逻辑
我们正在使用 Model 查看 Controller 因为我们的数据在数据库中
这就是我们填充名为 ctable not creative 的表的方式
private void ReadFromChild() throws SQLException{
ObservableList<ChildModel> TableData = FXCollections.observableArrayList();
stmnt = conn.createStatement();
ResultSet rs = stmnt.executeQuery("SELECT * FROM Child WHERE cMonth ='"+strMONTH+"'AND cYear ='"+strYEAR+"'" );//works);
while (rs.next()){// Add data to observableArrayList TableData to select a table ROW
TableData.add(new ChildModel(rs.getString("CID"),rs.getString("cDisplayDate"),rs.getString("cTitle")));
displayDATE = rs.getString("cDisplayDate");
}
PropertyValueFactory<ChildModel, String> IDCellValueFactory = new PropertyValueFactory<>("CID");
colID.setCellValueFactory(IDCellValueFactory);
PropertyValueFactory<ChildModel, String> DateCellValueFactory = new PropertyValueFactory<>("cDisplayDate");
colDD.setCellValueFactory(DateCellValueFactory);
PropertyValueFactory<ChildModel, String> TypeCellValueFactory = new PropertyValueFactory<>("cTitle");
colTitle.setCellValueFactory(TypeCellValueFactory);
Collections.sort(TableData, (p2, p1)-> p1.getCID().compareToIgnoreCase(p2.getCID()));
// Line of Code above Sorts Database Columns based on VARIABLE in ChildModel
// Change p2,p1 to sort Ascending or Descending
if(TableData.size() < 15) {// Format TableView to display Vertical ScrollBar
ctable.setPrefWidth(746);// 19 is the off set
}else {
ctable.setPrefWidth(765);
} ctable.setItems(TableData);
stmnt.close();
}
好的,当我们单击表格中的一行时,我们将 go 转换为另一个 Controller 这是代码
private void toDetailView() throws IOException{
stage = (Stage)childTVPane.getScene().getWindow();// pane you are ON
detailviewPane = FXMLLoader.load(getClass().getResource("detailview.fxml"));// pane you are GOING TO
Scene scene = new Scene(detailviewPane);// pane you are GOING TO
scene.getStylesheets().add(getClass().getResource("diary.css").toExternalForm());
stage.setScene(scene);
stage.setTitle("Diary Entry for "+displayDATE);
stage.show();
stage.sizeToScene();
stage.centerOnScreen();
}
private void showTableDataDetails(ChildModel info) throws IOException{
if (info != null) {
info = (ChildModel) ctable.getSelectionModel().getSelectedItem();
String str = info.getCID();
strID = Integer.valueOf(str);
toDetailView();
}
}
@Override
public void initialize(URL url, ResourceBundle rb) {
ctable.getSelectionModel().selectedItemProperty().addListener((ObservableValue<? extends ChildModel>
observable,ChildModel oldValue, ChildModel newValue) -> {
try {
showTableDataDetails((ChildModel) newValue); // When a row of the table is Selected call
// Proper Construction // showTableDataDetails method
} catch (IOException ex) {
Logger.getLogger(ParentTableViewController.class.getName()).log(Level.SEVERE, null, ex);
}
});
}
这不是最好的解决方案,但它解决了我的问题,只是想更新这个问题。
我将我的 LinkedList 移动到主 class 并将其公开 static 以从两个控制器访问它
public static TheLinkedList<Show> showList = new TheLinkedList<>();
为了使用 ObservableList,我在我的 TheLinkedList class 中使用以下方法将我的 LinkedList 转换为 arrayList
public Show[] getShowList(){
Show[] newShows = new Show[counter];
sample.Node temp = Main.showList.getHead();
for(int i = 0; i<counter;i++ ){
newShows[i] = (Show)temp.getData();
temp = temp.getNext();
}
return newShows;
}
public void initialize() {
showData = FXCollections.observableArrayList((Main.showList.getShowList()));
(column data) .......
showTable.setItems(showData);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.