user11880836
user11880836

Reputation: 31

Javafx: How to print string of array in label using button and textfield?

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());
        }
    }

}

Upvotes: 0

Views: 1941

Answers (1)

fabian
fabian

Reputation: 82461

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" />

Upvotes: 2

Related Questions