向GridPane动态添加行触发IllegalArgumentException问题求助
Hey there! Let's work through these two frustrating issues you're facing—IllegalArgumentException when adding GridPane rows, and display errors when reading the second line of your text file. Here are common fixes and checks to try:
Fixing GridPane's IllegalArgumentException on Dynamic Row Addition
These are the most frequent causes for this error:
Reusing the same UI node across rows
JavaFX nodes can only belong to one parent container at a time, or occupy one position in a container. If you're trying to add aTextField,Label, or other control that's already in the GridPane, you'll get this exception. Make sure you create new instances for each row:// Do this for new rows TextField nameField = new TextField(); Label ageLabel = new Label("Age:"); gridPane.addRow(gridPane.getRowCount(), ageLabel, nameField);Invalid row/column index values
Double-check that you're using valid, non-negative indices when adding rows. UsinggridPane.getRowCount()as the index for the new row is a safe way to avoid out-of-bounds issues:// This ensures you're adding to the next empty row int nextRow = gridPane.getRowCount(); gridPane.addRow(nextRow, newControl1, newControl2);Avoid hardcoding indices unless you're certain of the GridPane's current row count.
Conflicting layout constraints
If you manually setGridPane.rowIndexorGridPane.columnIndexconstraints on a node before adding it, make sure you're not setting duplicate or invalid values. For example, don't set the row index twice for the same node.
Fixing Text File Second-Line Display Errors
For the text file reading issue, try these checks:
Handle newline characters correctly
If you're usingScanner, mixing methods likenextInt()ornext()withnextLine()can cause skipped lines. After using a non-line-reading method, callscanner.nextLine()to consume the leftover newline before reading the next full line:Scanner scanner = new Scanner(new File("yourfile.txt")); String firstLine = scanner.nextLine(); // If you used scanner.next() before, add scanner.nextLine() here to skip the newline String secondLine = scanner.nextLine(); System.out.println(secondLine);Verify file encoding
If your text file uses a non-UTF-8 encoding (like Windows-1252), reading it without specifying the encoding can lead to garbled text or partial reads. UseBufferedReaderwith explicit encoding:try (BufferedReader br = new BufferedReader( new InputStreamReader(new FileInputStream("yourfile.txt"), StandardCharsets.UTF_8))) { String firstLine = br.readLine(); String secondLine = br.readLine(); // Display secondLine in your UI } catch (IOException e) { e.printStackTrace(); }Confirm file content and path
Double-check that your text file actually has a second line (no trailing newline issues) and that you're reading the correct file path. Using an absolute path temporarily can help rule out relative path mistakes.
If you can share snippets of your code for adding GridPane rows and reading the text file, we can narrow this down even further!
内容的提问来源于stack exchange,提问作者Utsav Dave

