7
\$\begingroup\$

I'm introducing someone to JavaFX. It still feels relatively new and I don't take being someone's first exposure to something lightly. I want to know if there's anything I'm doing unconventionally, inefficiently, or just plain oddly in this code? Although it is relatively simple, If fresh eyes could discern something unaccounted or discern a should-be inclusion, I'd be remiss not to illuminate self before attempting to do so for another.

import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.control.Tooltip;
import javafx.scene.layout.HBox;
import javafx.stage.Stage;

public class Hello extends Application {
    Scene scene;

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

    @Override
    public void start(Stage stage) {
        TextField entry = new TextField();
        entry.setPromptText("Enter your name");

        Button hello = new Button("Greeting");
        hello.setOnAction(e -> greet(entry));
        hello.setDefaultButton(true);
        hello.setTooltip(new Tooltip("Click me!"));

        HBox layout = new HBox();
        layout.getChildren().addAll(entry, hello);
        layout.setPadding(new Insets(2, 12, 2, 12));

        scene = new Scene(layout);

        stage.setScene(scene);
        stage.setTitle("Welcome to JavaFX");
        stage.show();
        hello.requestFocus();
    }

    public void greet(TextField entry) {
        String name = entry.getText();
        Label label = name.isEmpty() ? new Label("Hello user!") : new Label("How's it going, " + name + "?");
        scene.setRoot(label);
    }
}
\$\endgroup\$
3
  • \$\begingroup\$ ...Is "Looks good to me" a valid response? \$\endgroup\$
    – anon
    Commented Jun 9, 2015 at 2:15
  • \$\begingroup\$ @QPaysTaxes Depends who you're asking. \$\endgroup\$
    – Mast
    Commented Jun 9, 2015 at 3:49
  • \$\begingroup\$ Curious downvote. \$\endgroup\$
    – Legato
    Commented Jun 9, 2015 at 13:29

2 Answers 2

7
\$\begingroup\$

Code-wise I can't really see any major problems. The only glaring issue I see is that scene should be private.

The remainder of the code is neat, and consistent. I like that.

The rest of my review is about the usability. It irritated me that I had to click on the text entry to add my name. I realized that you forced the focus on to the hello button in order to make the "prompt" text visible in the Text Field. When you click on the field the prompt goes away though.... leaving you with nothing.

I prefer the use of labels, in combination with the prompt, in order to guide the user. Prompts that disappear are a problem.

So, I would add a label "Name:" before the TextField, and I would force the focus on to entry instead of hello. With those changes you can start the app, type text, hit enter, and get a reply - no mouse needed....

... which leads to the next problem, the only way to reset the system is to exit, and restart.... you should have something to help with that, but, I know, this is just an entry-level system.

As an aside, when I added my Name: label, I also had to set the HBox alignment to Pos.CENTER and I set the spacing to 5 on the layout too.

\$\endgroup\$
8
\$\begingroup\$

Pretty neat. I think this:

Label label = name.isEmpty() ? new Label("Hello user!") : new Label("How's it going, " + name + "?");

Could be neater like this:

Label label = name.isEmpty() 
    ? new Label("Hello user!") 
    : new Label("How's it going, " + name + "?");

Actually... what changes depending on the result name.isEmpty() isn't the label itself, but its content.

String greeting = name.isEmpty()
    ? "Hello user!"
    : "How's it going, " + name + "?";
Label label = new Label(greeting);

I'd probably rename label to greetingLabel, too.

\$\endgroup\$

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.