Refresh label in JAVAFX


Question

So i have this code in which i'm trying to do a scene for my game. I'm really a beginner in a Java and especially JAVAFX world and doing this as a school project (Once again..) and trying to figure out a way to refresh my label.

I've found one URL from stackoverflow, which was a similar issue but didn't work for my problem (or was i too stupid to make it work..) anyways, link is here

This is the part where the problem occurs - i have a text box, from which you have to enter player names. Every time a user inputs player name the label shows how many names have been entered, according to the nimedlist.size() which holds the names inside.

Label mängijate_arv = new Label("Mängijaid on sisestatud: "+nimedlist.size());

                            // if we press enter, program will read the name
                            nimiTekst.setOnKeyPressed(new EventHandler<KeyEvent>() {
                                    public void handle(final KeyEvent keyEvent) {
                                            if (keyEvent.getCode() == KeyCode.ENTER) {
                                                    if (nimiTekst.getText() != null) {

                                                            nimedlist.add(nimiTekst.getText());
                                                            nimiTekst.setText(null);

                                                    }

                                            }
                                    }
                            });

                            startBox.getChildren().addAll(sisestus_mängijad, nimiTekst, mängijate_arv,
                                            startButton2);

This is the whole code:

package application;

import java.util.ArrayList;

import javafx.application.Application;
import javafx.beans.property.StringProperty;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.ListView;
import javafx.scene.control.RadioButton;
import javafx.scene.control.TextField;
import javafx.scene.input.KeyCode;
import javafx.scene.input.KeyEvent;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.FlowPane;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.StackPane;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Color;
import javafx.scene.text.Font;
import javafx.scene.text.Text;
import javafx.stage.Stage;
import javafx.stage.WindowEvent;

public class Baila2 extends Application {

        public static void main(String[] args) {
                launch(args);
        }

        public void start(final Stage peaLava) {
                final Group root = new Group();
                final BorderPane piir = new BorderPane();

                piir.setPrefSize(960, 540);

                final Text tekst = new Text();
                tekst.setText("JOOMISMÄNG");
                tekst.setFont(Font.font("Verdana", 40));

                VBox nupudAlam = new VBox();
                Button startButton = new Button("Start");
                nupudAlam.setSpacing(20);
                Button reeglidButton = new Button("Reeglid");
                nupudAlam.setAlignment(Pos.CENTER);

                startButton.setId("btn3");
                startButton.setMaxWidth(160);
                reeglidButton.setMaxWidth(160);
                reeglidButton.setId("btn3");
                nupudAlam.getChildren().addAll(startButton, reeglidButton);
                piir.setTop(tekst);
                piir.setAlignment(tekst, Pos.CENTER);
                piir.setCenter(nupudAlam);
                root.getChildren().add(piir);

                // START NUPP TÖÖ
                startButton.setOnAction(new EventHandler<ActionEvent>() {
                        public void handle(final ActionEvent event) {

                                final ArrayList nimedlist = new ArrayList();
                                piir.setVisible(false);
                                final BorderPane startPiir = new BorderPane();
                                final VBox startBox = new VBox();

                                Button startButton2 = new Button("ALUSTA!");
                                startButton2.setId("btn2");
                                startButton2.setMaxWidth(160);
                                startPiir.setPrefSize(960, 540);
                                final Text startTekst = new Text();
                                startTekst.setText("JOOMISMÄNG");
                                startTekst.setFont(Font.font("Verdana", 40));
                                startPiir.setTop(startTekst);
                                startPiir.setAlignment(startTekst, Pos.CENTER);

                                final TextField nimiTekst = new TextField();
                                nimiTekst.setText(null);
                                nimiTekst.setMaxWidth(250);

                                Label sisestus_mängijad = new Label(
                                                "Sisesta 3-9 mängija nimed:");
                                sisestus_mängijad.setFont(Font.font("Verdana", 30));
                                sisestus_mängijad.setTextFill(Color.ORANGE);


                                Label mängijate_arv = new Label("Mängijaid on sisestatud: "+nimedlist.size());

                                // kui vajutatakse ENTER,siis loeme nime
                                nimiTekst.setOnKeyPressed(new EventHandler<KeyEvent>() {
                                        public void handle(final KeyEvent keyEvent) {
                                                if (keyEvent.getCode() == KeyCode.ENTER) {
                                                        if (nimiTekst.getText() != null) {

                                                                nimedlist.add(nimiTekst.getText());
                                                                nimiTekst.setText(null);

                                                        }

                                                }
                                        }
                                });

                                startBox.getChildren().addAll(sisestus_mängijad, nimiTekst, mängijate_arv,
                                                startButton2);

                                startBox.setSpacing(20);
                                startBox.setAlignment(Pos.CENTER);
                                startPiir.setCenter(startBox);
                                root.getChildren().add(startPiir);

                        }
                });

                // aknasündmuse lisamine
                peaLava.setOnHiding(new EventHandler<WindowEvent>() {
                        public void handle(WindowEvent event) {
                                // luuakse teine lava
                                final Stage kusimus = new Stage();
                                // küsimuse ja kahe nupu loomine
                                Label label = new Label("Kas tõesti tahad kinni panna?");
                                Button okButton = new Button("Jah");
                                Button cancelButton = new Button("Ei");

                                // sündmuse lisamine nupule Jah
                                okButton.setOnAction(new EventHandler<ActionEvent>() {
                                        public void handle(ActionEvent event) {
                                                kusimus.hide();
                                        }
                                });

                                // sündmuse lisamine nupule Ei
                                cancelButton.setOnAction(new EventHandler<ActionEvent>() {
                                        public void handle(ActionEvent event) {
                                                peaLava.show();
                                                kusimus.hide();
                                        }
                                });

                                // nuppude grupeerimine
                                FlowPane pane = new FlowPane(10, 10);
                                pane.setAlignment(Pos.CENTER);
                                pane.getChildren().addAll(okButton, cancelButton);

                                // küsimuse ja nuppude gruppi paigutamine
                                VBox vBox = new VBox(10);
                                vBox.setAlignment(Pos.CENTER);
                                vBox.getChildren().addAll(label, pane);

                                // stseeni loomine ja näitamine
                                Scene stseen2 = new Scene(vBox);
                                kusimus.setScene(stseen2);
                                kusimus.show();
                        }
                }); // siin lõpeb aknasündmuse kirjeldus

                // stseeni loomine ja näitamine
                Scene stseen1 = new Scene(root, 960, 540, Color.GREEN);
                peaLava.setTitle("BAILA 2.0");
                // peaLava.setResizable(false);
                stseen1.getStylesheets().add(
                                getClass().getClassLoader().getResource("test.css")
                                                .toExternalForm());
                peaLava.setScene(stseen1);
                peaLava.show();
        }

}

Sorry about Estonian language, it's compulsory in our school to write in our native language..

1
0
5/23/2017 12:05:37 PM

Accepted Answer

You can just do

 nimiTekst.setOnKeyPressed(new EventHandler<KeyEvent>() {
                                public void handle(final KeyEvent keyEvent) {
                                        if (keyEvent.getCode() == KeyCode.ENTER) {
                                                if (nimiTekst.getText() != null) {

                                                        nimedlist.add(nimiTekst.getText());
                                                        nimiTekst.setText(null);
                                                        mängijate_arv.setText("Mängijaid on sisestatud: "+nimedlist.size());

                                                }

                                        }
                                }
                        });

If you are not using Java 8 (you appear not to be, since you are implementing all the handlers the old, long way...), you will have to declare mängijate_arv as final:

final Label mängijate_arv = new Label("Mängijaid on sisestatud: "+nimedlist.size());

If you want to be extra cool with this, you can use bindings instead. You will have to make nimidlist an observable list:

final ObservableList<String> nimedlist = FXCollections.observableArrayList();

and then:

mängijate_arv.bind(Bindings.format("Mängijaid on sisestatud: %d", Bindings.size(nimedList)));

and don't put the mängijate_arv.setText(...) call in the handler. This solution is nicer in many ways, as if you remove items from the list (or add other items elsewhere in your code), then the label will still remain properly updated without any additional code.

One other thing: it's a bit better to use an action handler on the text field, instead of a low-level key event handler:

 nimiTekst.setOnAction(new EventHandler<ActionEvent>() {
                                public void handle(final ActionEvent keyEvent) {
                                        if (nimiTekst.getText() != null) {
                                                     nimedlist.add(nimiTekst.getText());
                                                     nimiTekst.setText(null);
                                                     mängijate_arv.setText("Mängijaid on sisestatud: "+nimedlist.size());
                                        }
                                }
                        });

(Sorry if I mangled your variable names. My Estonian is a bit weak ;). Your school's policy is a good one, for what it's worth.)

1
4/8/2014 7:12:08 PM

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Icon