0

I am using an IntelliJIdea. I want to display all the numbers that I submitted in Textfield. So I created an ArrayList to put all the numbers I submitted there. However, every time I pressed the button, it does not add all the list, instead only the 1 number I submitted. How can I show all the numbers that I've submitted in Textfield and display it using label. I am using a separate fxml.

enter image description here

sample.fxml:

<?import javafx.geometry.Insets?>
<?import javafx.scene.layout.GridPane?>

<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.TextField?>
<?import javafx.scene.control.ScrollPane?>
<?import javafx.scene.layout.VBox?>
<GridPane fx:controller="com.binarySearch.Controller"
          xmlns:fx="http://javafx.com/fxml" alignment="TOP_LEFT" hgap="10" vgap="10">

    <Label text="Please enter number" GridPane.rowIndex="0" GridPane.columnIndex="0"/>
    <TextField  fx:id="inputNumber" GridPane.rowIndex="1" GridPane.columnIndex="0" />
    <Button text="Submit" fx:id="submit" GridPane.rowIndex="2" GridPane.columnIndex="0" onAction="#handleSubmitPress"/>
    <Button text="Search" fx:id="search" GridPane.rowIndex="3" GridPane.columnIndex="0" onAction="#handleSearchPress" />
    <Label text="You input: " GridPane.rowIndex="4" GridPane.columnIndex="0" />
    <VBox GridPane.rowIndex="5" GridPane.columnIndex="0" >
        <Label fx:id="display" />
    </VBox>

</GridPane>

Controller.java

package com.binarySearch;

import javafx.fxml.FXML;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.input.KeyEvent;

import java.util.ArrayList;
import java.util.List;

public class Controller {
    @FXML
    private TextField inputNumber;

    @FXML
    private Button submit;

    @FXML
    private Button search;

    @FXML
    private Label display;

    public void handleSubmitPress() {
        List<Integer> list = new ArrayList<>();
        int num = Integer.parseInt(inputNumber.getText());
        list.add(num);

        for(int x=0; x<list.size();x++) {
            display.setText(list.get(x).toString());
        }
    }

}

1 Answer 1

2

You create a new list in the method itself. This list is initially empty, so after adding the text, it contains a single element. Furthermore using setText replaces the text of the label. It doesn't assign a combination of the strings passed as text; it doesn't add more Labels either.

The first issue could be fixed declaring the list as a field, but the second issue is probably solved best by just adding a new Label to the VBox which may make keeping track of the data in a list unnecessary:

private final List<Integer> list = new ArrayList<>();
@FXML
private VBox vbox;

public void handleSubmitPress() {
    int num = Integer.parseInt(inputNumber.getText());
    list.add(num);

    vbox.getChildren().add(new Label(Integer.toString(num)));
}
    <VBox GridPane.rowIndex="5" GridPane.columnIndex="0" fx:id="vbox" />
Sign up to request clarification or add additional context in comments.

2 Comments

Wow, it works! Thank you very much! I appreciate your help. It's because I am having a hard time getting all the attributes or properties of all the fxml components. Seems like I couldn't find the proper documentation of fxml but rather look at this site docs.oracle.com/javase/8/javafx/api/toc.htm but that's confusing though
@user11880836 fxml basically corresponds to calls to certain setters/constructors or adding children to list properties. A tutorial about JavaFX layouts in java may be helpful. This is at least partially documented here: openjfx.io/javadoc/12/javafx.fxml/javafx/fxml/doc-files/… (Iirc you constructors with parameters (see @NamedArg) and default properties (@DefaultProperty) are not explained.) Unless you're using SceneBuilder, it's best to know how to construct scene the in java first...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.