I neeed add to listView a new item from external fxml button event. If I click external fxml button I have to insert a new item in a TitledPane that contains a listView but button don't work or I have nullPointExeption
TableModel
java public class MarketItemListView_Model { private String stringa1; private String stringa2; private String stringa3; private String stringa4; private String stringa5; private String stringa6; private String stringa7;
public MarketItemListView_Model(String stringa1, String stringa2, String stringa3, String stringa4, String stringa5, String stringa6, String stringa7) { this.stringa1 = stringa1; this.stringa2 = stringa2; this.stringa3 = stringa3; this.stringa4 = stringa4; this.stringa5 = stringa5; this.stringa6 = stringa6; this.stringa7 = stringa7; } public String getStringa1() { return stringa1; } public void setStringa1(String stringa1) { this.stringa1 = stringa1; } public String getStringa2() { return stringa2; } public void setStringa2(String stringa2) { this.stringa2 = stringa2; } public String getStringa3() { return stringa3; } public void setStringa3(String stringa3) { this.stringa3 = stringa3; } public String getStringa4() { return stringa4; } public void setStringa4(String stringa4) { this.stringa4 = stringa4; } public String getStringa5() { return stringa5; } public void setStringa5(String stringa5) { this.stringa5 = stringa5; } public String getStringa6() { return stringa6; } public void setStringa6(String stringa6) { this.stringa6 = stringa6; } public String getStringa7() { return stringa7; } public void setStringa7(String stringa7) { this.stringa7 = stringa7; }}
controller
public class Controller implements Initializable { public AnchorPane topPane; public AnchorPane leftPane; public AnchorPane rightPane; public AnchorPane centerPane; public AnchorPane titledPaneAnchorPane;
@FXML private Label label; @FXML private ListView listView; public TitledPane titledPane; public ListView getListViewTtiledPane() { return listViewTtiledPane; } public ListView<SelectionPane_Model> listViewTtiledPane; private HBox box; ObservableList<MarketItemListView_Model> list = FXCollections.observableArrayList(); @Override public void initialize(URL location, ResourceBundle resources) { label.setText("text"); list.add(new MarketItemListView_Model("1","2", "3", "Selection", "5", "6", "7")); list.add(new MarketItemListView_Model("1","2", "3", "Selection", "5", "6", "7")); list.add(new MarketItemListView_Model("1","2", "3", "Selection", "5", "6", "7")); listView.getItems().addAll(list); try { getBox(); } catch (IOException e) { e.printStackTrace(); } } public void getBox() throws IOException { listView.setCellFactory(lv -> new ListCell<MarketItemListView_Model>(){ private Node graphic; private FXML2Controller controller; { try { FXMLLoader loader = new FXMLLoader(getClass().getResource("FXML2.fxml")); //FXMLLoader loader = new FXMLLoader(getClass().getResource("marketItemListView.fxml")); graphic = loader.load(); controller = loader.getController(); } catch (IOException e) { e.printStackTrace(); } } @Override protected void updateItem(MarketItemListView_Model model, boolean empty){ super.updateItem(model,empty); if(empty){ setGraphic(null); }else{ controller.getItemRootPane().autosize(); controller.setBack1(model.getStringa1()); controller.setBack2(model.getStringa2()); controller.setBack3(model.getStringa3()); controller.setSelection(model.getStringa4()); controller.setLay1(model.getStringa5()); controller.setLay2(model.getStringa6()); controller.setLay3(model.getStringa7()); setGraphic(graphic); } } }); }}
FXML2Controller
public class FXML2Controller implements Initializable {
public HBox itemRootPane; public Label selection; public Button back1; public Button back2; public Button back3; public Button lay1; public Button lay2; public Button lay3; public HBox getItemRootPane() { return itemRootPane; } public void setItemRootPane(HBox itemRootPane) { this.itemRootPane = itemRootPane; } public Label getSelection() { return selection; } public void setSelection(String text) { this.selection.setText(text); } public Button getBack1() { return back1; } public void setBack1(String text) { back1.setText(text); } public Button getBack2() { return back2; } public void setBack2(String text) { back2.setText(text); } public Button getBack3() { return back3; } public void setBack3(String text) { back3.setText(text); } public Button getLay1() { return lay1; } public void setLay1(String text) { lay1.setText(text); } public Button getLay2() { return lay2; } public void setLay2(String text) { this.lay2.setText(text); } public Button getLay3() { return lay3; } public void setLay3(String text) { this.lay3.setText(text); } public void showBackPane(ActionEvent actionEvent) throws IOException { FXMLLoader loader = new FXMLLoader(); loader.setLocation(getClass().getResource("../home/home.fxml")); try { loader.load(); }catch (Exception e){ System.out.println("e.getMessage() = " + e.getMessage()); } Controller controller = loader.getController(); //?????????????? /* I NEED TO IMPLEMENT BUTTON ACTION EVENT */ } @Override public void initialize(URL location, ResourceBundle resources) { }}
Asked
Active
Viewed 66 times
0
-
[mcve] please.. mind the __M__! and unrelated: stick to java naming conventions – kleopatra May 05 '20 at 08:02
-
Just pass the list view's items to the controller for the second FXML file. (See https://stackoverflow.com/questions/14187963). Then you can add the item to the list directly in the second controller. – James_D May 05 '20 at 13:38