我正在开发一个相当简单的javafx应用程序,它有几个不同的场景,我的第一个场景是将一个Show对象添加到一个通用链表,然后在第二个场景中向该对象添加其中一个节目的表演(例如,在节目Annie中添加日场或晚间表演(
我已经创建了第一个场景,可以将 Show 对象添加到链表,但我无法使用创建的 show 填充第二个场景中的表视图供用户选择和添加表演
这是我的添加显示控制器的样子:
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()));
}
如您所见,我尝试创建控制器的实例,以便我可以从添加性能控制器访问链表,但没有乐趣。
这是添加性能控制器:
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);
}
我们不确定这是您要做的,但看看逻辑
我们正在使用模型视图控制器,因为我们的数据在数据库中
这就是我们填充名为 ctable 的表的方式 没有创意
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();
}
好的,当我们单击表中的一行时,我们转到另一个控制器,这是该代码
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 移动到 Main 类,并将其设置为公共静态以从两个控制器访问它
public static TheLinkedList<Show> showList = new TheLinkedList<>();
为了使用 ObservableList,我在 TheLinkedList 类中使用以下方法将 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);
}