如何将ArrayList单个项放入JavaFX库存GUI的对应Tile中?
Hey there! No need to overthink this—you’re already most of the way there. Let’s get those item descriptions into your tiles smoothly.
First, let’s recap your setup to make sure we’re on the same page:
- You’ve got a tile-based inventory GUI like this:

- You can print each item from your
ArrayList<String> descsto the console - You’re creating 30
HBoxtiles, but right now they just show a placeholder like "Item: 0"
The Core Issue
Right now, you’re generating tile labels with a static placeholder ("Item: " + i) instead of pulling the actual description from your descs list. Since you already refresh the entire GUI when deleting an item (via displayItems2()), we can fix this by using the latest inventory data directly when building each tile.
Modified Code Solution
First, update the tile creation logic (I assume this is inside your displayInvStructure() method):
// Grab the latest item descriptions first—this ensures we use up-to-date data ArrayList<String> descs = InventoryManager.getInstance().getItemDescriptions(); HBox itemTile[] = new HBox[31]; for (int i = 0; i < 30; i++) { Button deleteButton = new Button("Delete Item"); deleteButton.setOnAction((ActionEvent event) -> { displayItems2(); // Refresh GUI after deletion JOptionPane.showMessageDialog(null, "Item has been deleted", null, 1); }); // Get the correct description for this tile, or show "Empty" if no item exists String itemDescription = (i < descs.size()) ? descs.get(i) : "Empty"; Label itemLabel = new Label("Item: " + itemDescription); // Build the tile with the dynamic label itemTile[i] = new HBox(itemLabel); itemTile[i].setStyle("-fx-background-color: #e5efff; -fx-border-color: black;"); itemTile[i].setPadding(new Insets(5)); itemTile[i].getChildren().add(deleteButton); itemTile[i].setAlignment(Pos.CENTER_LEFT); this.getChildren().add(itemTile[i]); }
Your displayItems2() method can stay almost the same—since we now pull the latest descriptions when rebuilding tiles, the refresh will automatically reflect any changes:
private void displayItems2(){ this.getChildren().removeAll(this.getChildren()); // Clear old tiles displayInvStructure(); // Rebuild tiles with current inventory // Optional: Keep this console log for debugging ArrayList<String> descs = InventoryManager.getInstance().getItemDescriptions(); for (int i = 0; i < descs.size(); i++) { String retString = descs.get(i); System.out.println("Array item is = " + " " + i + " " + retString); } }
What Changed & Why
- Pull descriptions upfront: We get the
descslist right before creating tiles, so we always use the most recent inventory state. - Dynamic label text: For each tile, we check if there’s a corresponding item in
descs(to avoidIndexOutOfBoundsExceptionif the list is shorter than 30). If there’s an item, we use its description; if not, we show "Empty". - Simplified refresh: Instead of trying to update existing tiles, we rebuild the entire set every time. This is straightforward for a beginner setup and ensures consistency.
This approach keeps things simple and leverages the refresh logic you already have in place. No need to track individual tiles or update them later—just let the tile creation loop do the work with your actual inventory data!
内容的提问来源于stack exchange,提问作者wastedyears1981

