1

I am new to programming in java and javafx. I have a listview populated with a pane which has some controls on it and also an deletebutton. How can I delete the item by pressing the item's deletbutton? Every item has a Deletebutton.

MyListview.fxml

<AnchorPane prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="Test.MyListviewController">
   <children>
      <ListView fx:id="lvGames" layoutX="14.0" layoutY="14.0" prefHeight="339.0" prefWidth="577.0" />
      <Button fx:id="btnAdd" layoutX="14.0" layoutY="365.0" mnemonicParsing="false" onAction="#btnAddPressed" text="Add" />

   </children>
</AnchorPane>

MyListviewController.java How Can I access the method removeGamesItem from the MyListviewItem Class?

public class MyListviewController implements Initializable {
    @FXML
    ListView lvGames;
    private ObservableList<Pane> listGames = FXCollections.observableArrayList();
    public void removeGamesItem(int index) {
        this.listGames.remove(index);
    }

    @Override
    public void initialize(URL location, ResourceBundle resources) {
        lvGames.setItems(listGames);
    }

    @FXML
    private void btnAddPressed() throws IOException {
        FXMLLoader loader = new FXMLLoader();
        loader.setLocation(getClass().getResource("MyListviewItem.fxml"));
        Pane gamePane = loader.load();
        listGames.add(gamePane);
    }
}

MyListviewItem.fxml

<Pane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="79.0" prefWidth="334.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="Test.MyListviewItemController">
   <children>
      <HBox fx:id="paneItem" prefHeight="79.0" prefWidth="334.0">
         <children>
            <Label text="press the button to delete">
               <HBox.margin>
                  <Insets bottom="20.0" left="20.0" right="20.0" top="20.0" />
               </HBox.margin>
            </Label>
            <Button fx:id="btnDelete" mnemonicParsing="false"  prefHeight="25.0" prefWidth="105.0" text="delete">
               <HBox.margin>
                  <Insets top="20.0" />
               </HBox.margin>
            </Button>
         </children>
      </HBox>
   </children>
</Pane>

MyLisctviewController.java

public class MyListviewItemController implements Initializable {
    @FXML
    Pane paneItem;
    @FXML
    Button btnDelete;

    @Override
    public void initialize(URL location, ResourceBundle resources) {
 
    }
  
    public void btnDeletePressed(Event event){
        
    }

}
Nickske88
  • 11
  • 2
  • _listview populated with a pane which has some controls on it and also an deletebutton_ I hope you don't mean that literally (because storing nodes as data is wrong ;) and you have listCells that consist of a pane having a button: the cell knows its listView from which you might remove its current item. If you need a broader interaction between controllers see the duplicate. – kleopatra Nov 29 '21 at 10:47

0 Answers0