HiveBrain v1.2.0
Get Started
← Back to all entries
patternjavaMinor

Options, options, options. None for JavaFX?

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
javafxforoptionsnone

Problem

I was looking through the JavaFX library, and I spotted that JavaFX DOES NOT HAVE A JOptionPane EQUIVALENT. (Or at least not in my version - as @Legato has said in comments, as of 8u40, there is a Dialog class. I use 8u25.) I decided the next step was to write a OptionPane class.

Since JOptionPane has three types of option panes:

  • showInputDialog



  • showMessageDialog



  • showConfirmDialog



plus its Internal equivalents (e.g. showInternalConfirmDialog), I would have to display three different kinds of OptionPanes. If that was the case, then I would need three classes to use @Eric Stein's design (in this answer). So the question for me was, three good classes, or one bad class?

Three good classes was my choice.

Concerns:

  • I'm fairly sure there's a clean way to combine this into one class, but I can't think of one. Any suggestions?



  • Is this the best it can get?



  • Does my code make sense?



  • Anything else?



The code is below:

InputPane.java

```
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

public class InputPane {

private static final MessagePane ILLEGAL_INPUT_PANE = new MessagePane.Builder("Illegal Input.").title("Illegal Input").build();

private static final Insets MAIN_PANE_PADDING = new Insets(10, 20, 10, 20);
private static final int MAIN_PANE_SPACING = 10;

private static final Insets BUTTON_PANE_PADDING = new Insets(10, 50, 10, 50);
private static final int BUTTON_PANE_SPACING = 20;

private static final boolean IS_RESIZABLE = false;

private static String inputResult = null;

private final Stage stage = new Stage();

private InputPane(final Builder builder) {
inputResult = null;

Label msgLabel = new Label(builder.message);

Text

Solution

I'm not sure I like these Builder nested classes. Oh sure, like the rest of the code you're showing us, it's very nice & tidy, readable, consistent and well-formatted code.

That builder looks like a builder, but in reality this:

ConfirmPane builder = new Builder("foo").title("title")
                                        .okButtonText("ok")
                                        .cancelButtonText("cancel")
                                        .canClose(false)
                                        .build();


Is IMO pretty much a fancy/look-ma/convoluted way to do this:

ConfirmPaneModel = new ConfirmPaneModel("foo", "title", "ok", "cancel", false);


What happens if you do this?

ConfirmPane builder = new Builder("foo").title("title")
                                        .okButtonText("ok")
                                        .okButtonText("foobarred")
                                        .cancelButtonText("cancel")
                                        .okButtonText("foobarred")
                                        .title("foobarred")
                                        .canClose(false)
                                        .canClose(true)
                                        .build();


That's right. You're foobarred. The Builder Pattern works much better when there's no specific order or number of times a method could or should be called before build(). When a method can only ever be called once per "builder" for the result to make sense, you're looking at an attribute of your "builder" type, a property. Property. Get. Set. We love properties, they're so unsurprising!

ConfirmPaneModel model = new confirmPaneModel();
model.title = "title";
model.okButtonText = "ok";
model.cancelButtonText = "cancel";
model.canClose = false;


Almost boring :-)

Code Snippets

ConfirmPane builder = new Builder("foo").title("title")
                                        .okButtonText("ok")
                                        .cancelButtonText("cancel")
                                        .canClose(false)
                                        .build();
ConfirmPaneModel = new ConfirmPaneModel("foo", "title", "ok", "cancel", false);
ConfirmPane builder = new Builder("foo").title("title")
                                        .okButtonText("ok")
                                        .okButtonText("foobarred")
                                        .cancelButtonText("cancel")
                                        .okButtonText("foobarred")
                                        .title("foobarred")
                                        .canClose(false)
                                        .canClose(true)
                                        .build();
ConfirmPaneModel model = new confirmPaneModel();
model.title = "title";
model.okButtonText = "ok";
model.cancelButtonText = "cancel";
model.canClose = false;

Context

StackExchange Code Review Q#108327, answer score: 3

Revisions (0)

No revisions yet.