Aug 20

Poor mans input constraints

For me the best way to prevent the user from annoying errors and error messages is simply in the first line to try not to allow the user e.g. to enter invalid data or to execute actions while data in incomplete (disabled buttons/menu entries).
If only numbers allowed in an input field I think its a better approach to restict user input to numbers only instead of validating and popping up with error messages after applying or “OK”.

Swing has a JFormattedTextField which allows setting a format pattern to verify the input data. Sadly the current JavaFX API doesn’t contain such kind of control but thers is a way by adding an EventHandler for KEY_TYPED-Events and to consume the event at certain conditions. Consuming the event will end up the event bubbling phase. (see Handling JavaFX Events for more informations).
Clearly this can be implemented in a new control or by creating a subclass of TextField. But there is also a simple approach by attaching the wanted constraints to existing instances of standard controls. As you my know the editor controls of Cells and ComboBoxes are also TextFields. Thus such a handler can also be attached e.g. to a ComboBox.

Limitation/Disclaimer:
Don’t sue me for this very simple approach ;-)!
As this is just a limitaion of the INPUT while TYPING it can be bypassed e.g. by copy-paste.
Clearly in the long run a JFormattedTextField like control supporting pattern matching, validation and user feedback is needed!

Runnable demo and code can be found here.

InputConstraintsDemo

        InputConstraints.noBlanks(noBlanksField);
        InputConstraints.noLeadingBlanks(noLeadingBlanksField);
        InputConstraints.numbersOnly(numbersOnlyField);
        InputConstraints.lettersOnly(lettersOnlyField);

        InputConstraints.noLeadingBlanks(valueComboBox.getEditor());
        InputConstraints.noLeadingBlanks(noLeadingBlanksComboBox.getEditor());
        InputConstraints.noBlanks(noBlanksComboBox.getEditor());
        InputConstraints.numbersOnly(numbersOnlyComboBox.getEditor());
        InputConstraints.lettersOnly(lettersOnlyComboBox.getEditor());
public class InputContraints{

    private InputContraints() {
        // not needed here
    }

    public static void noLeadingAndTrailingBlanks(final TextField textField) {
        textField.addEventFilter(KeyEvent.KEY_TYPED, createNoLeadingBlanksInputHandler());
        textField.focusedProperty().addListener((Observable observable) -> {
            textField.setText(textField.getText().trim());
        });
    }

    public static void noLeadingBlanks(final TextField textField) {
        textField.addEventFilter(KeyEvent.KEY_TYPED, createNoLeadingBlanksInputHandler());
    }

    public static void noBlanks(final TextField textField) {
        textField.addEventFilter(KeyEvent.KEY_TYPED, createNoBlanksInputHandler());
        textField.focusedProperty().addListener((Observable observable) -> {
            textField.setText(textField.getText().trim());
        });
    }

    public static void numbersOnly(final TextField textField) {
        numbersOnly(textField, Integer.MAX_VALUE);
    }

    public static void numbersOnly(final TextField textField, final Integer maxLenth) {
        textField.addEventFilter(KeyEvent.KEY_TYPED, createNumbersOnlyInputHandler(maxLenth));
        textField.focusedProperty().addListener((Observable observable) -> {
            textField.setText(textField.getText().trim());
        });
    }

    public static void lettersOnly(final TextField textField) {
        lettersOnly(textField, Integer.MAX_VALUE);
    }

    public static void lettersOnly(final TextField textField, final Integer maxLenth) {
        textField.addEventFilter(KeyEvent.KEY_TYPED, createLettersOnlyInputHandler(maxLenth));
        textField.focusedProperty().addListener((Observable observable) -> {
            textField.setText(textField.getText().trim());
        });
    }

    public static EventHandler<KeyEvent> createNoLeadingBlanksInputHandler() {
        return (KeyEvent event) -> {
            if (event.getSource() instanceof TextField) {
                TextField textField = (TextField) event.getSource();
                if (" ".equals(event.getCharacter()) && textField.getCaretPosition() == 0) {
                    event.consume();
                }
            }
        };
    }

    public static EventHandler<KeyEvent> createNumbersOnlyInputHandler(final Integer maxLength) {
        return createPatternInputHandler(maxLength, "[0-9.]");
    }

    public static EventHandler<KeyEvent> createLettersOnlyInputHandler(final Integer maxLength) {
        return createPatternInputHandler(maxLength, "[A-Za-z]");
    }

    public static EventHandler<KeyEvent> createNoBlanksInputHandler() {
        return (KeyEvent event) -> {
            if (event.getSource() instanceof TextField) {
                if (" ".equals(event.getCharacter())) {
                    event.consume();
                }
            }
        };
    }

    public static EventHandler<KeyEvent> createPatternInputHandler(final Integer maxLength, String pattern) {
        return (KeyEvent event) -> {
            if (event.getSource() instanceof TextField) {
                TextField textField = (TextField) event.getSource();
                if (textField.getText().length() >= maxLength || !event.getCharacter().matches(pattern)) {
                    event.consume();
                }
            }
        };
    }
}
Aug 12

MQTT.fx 0.0.6 released

I have just released a new version of MQTT.fx (0.0.6).

Introducing Connection Profiles
Instead of choosing a certain broker address and a broker port using the same connection options, I now have implemented Connection Profiles.

A named Connection Profile contains all information for a certain broker connection (address, port, Auth/SSL/TLS settings, Last Will/Testament, etc.). It’s much easier now to connect to different brokers with changed setups and connection options:

mqtt-connprofiles

Now a connection can be established by just choosing the name of a previous defined profile:

mqtt-connprofiles2

Support for SSL/TLS

Also I have started to implement support for SSL/TLS secured connections.
Unsecure:
MQTT.fx - v0.0.6 - connected unsecure
Secured:
MQTT.fx - v0.0.6 - connected secure

Support for Certificate Files
mqtt-certificates

Support for Keystores
Edit Connection Profiles keytores

Hint:
Again the configuration-format has changed, v0.0.6 will conflict with former versions and MQTT.fx won’t start.
In this case just delete (or rename) the mqttfx-config.xml.
Windows: [USER_HOME]\AppData\Local\MQTT-FX\mqttfx-config.xml
OS X: [USER_HOME]/Library/Application Support/MQTT-FX/mqttfx-config.xml

The new configration is automatically created on next app start.

Download binaries HERE.

Jul 28

“PiMac unibody” or a new home for my touch screen

In preparation for the JavaOne 2014 Session my 7″ touch screen from Chalk-Elec finally found a new home.
I bought a 1 mm sheet of aluminium and treated it with my jig saw and a rasp and folded a triangle stand for the screen. Basically there would be room enough for e.g. a Rapsberry Pi and it also stand touching the display:

DSC_9673

DSC_9675

DSC_9672

DSC_9668

Some anti slip pads on the button side

IMG_3589

IMG_3587

The Sketch

PiMac Scetch

Jul 27

MQTT.fx 0.0.5 released

Just released a new version of MQTT.fx.

New:

  • “Publish”, “Subscribe” and “Broker Status” can now be detached to be opened in a separate window: Just drag a tab title and drop it where the window should be placed. Closing of the window restores the content at the former position.
  • also I have done some UI polish

Download binaries HERE.

MQTT.fx0.0.5

Jul 15

TabPaneDetacher: Make your tabs detachable

I though it would be cool to detach e.g. the pane to publish messages in MQTT.fx if needed by just dragging the tab and dropping it at the wanted position to open its content as a dedicated new window.

drag tab

The TabPaneDetacher provides an easy to use way to enable the detach capability of with any JavaFX TabPane instance just by calling:

public class TabPaneDetacherDemoController {

    @FXML
    private TabPane demoTabPane;
    
    @FXML
    public void initialize() {
        TabPaneDetacher.create().makeTabsDetachable(demoTabPane);
    }    
    
}

The tab is removed from the tab pane and a new window is created containing the content of the tab, the window title is set to the tab name value. On closing that window the tab is restored at the original position:

Code can be found here in my Bitbucket repo.

Jul 10

UI logic with using JavaFX Bindings

For MQTT.fx I needed a ComboBox to enter new Topics or to choose recent topics from list.
But I want the user to enter or choose only (more or less) reasonable values otherwise the “Publish”/”Subscribe” buttons should be disabled:

ComboBox Bindings Demo 1

ComboBox Bindings Demo 2

Using the JavaFX Bindings API prevents some boilerplate code (implementing Listeners) and it is even more flexible as the result can be bound to several controls and at the end of the day its much more readable.

Contraint 1: The selection must not be empty.

  1. First create a JavaFX BooleanBinding.
  2. Then bind it to the disabledProperty of the button.

Contraint 2: Entered values must not contain only blanks (leading/trailing blanks can be trimmed later).

My approach is to

  1. add an EventFilter and to consume the event in case of only entered character is “SPACE”
  2. and if there are just blanks left when editing the value the content is cleared.
public class ComboBoxBindingsDemoController {

    @FXML
    private Label valueLabel;

    @FXML
    private Button goButton;

    @FXML
    private ComboBox<String> valueComboBox;

    private ObservableList<String> demoItemsList;
    private static final Pattern LEADING_BLANKS_PATTERN = Pattern.compile("^\\s*+");

    @FXML
    public void initialize() {
        valueComboBox.setItems(getDemoItemsList());

        BooleanBinding goButtonState = valueComboBox.editorProperty().get().textProperty().isEmpty()
                .or(valueComboBox.valueProperty().asString().isEmpty());

        goButton.disableProperty().bind(goButtonState);

        valueComboBox.getEditor().addEventFilter(KeyEvent.KEY_TYPED, (KeyEvent event) -> {
            Matcher matcher = LEADING_BLANKS_PATTERN.matcher(valueComboBox.getEditor().getText());
            if (" ".equals(event.getCharacter()) && valueComboBox.getEditor().getText().length() == 0) {
                event.consume();
            }
            else if(matcher.matches()){
                valueComboBox.getEditor().clear();
            }
        });

    }

    public ObservableList<String> getDemoItemsList() {
        if (demoItemsList == null) {
            demoItemsList = FXCollections.observableArrayList();
            demoItemsList.add("Erlangen");
            demoItemsList.add("Nürnberg");
            demoItemsList.add("Büchenbach");
        }
        return demoItemsList;
    }

    @FXML
    public void goAction() {
        System.out.println("Value '" + valueComboBox.getValue().trim() + "'");
    }

}

Complete runnable demo can be found at Bitbucket.

Jul 03

MQTT.fx 0.0.4 released

Just released new version of MQTT.fx.
Download binaries HERE.

MQTT.fx - v0.0.4

New:
- revisited UI (return of “real” buttons)
- extended support for $SYS-topics: subscription for mosquitto or Hive MQ can be choosen (in respect to @dobermai).
- publish and subcrive/receive are now different tabs
- experimental support for http-proxy (in respect to @dimaki123).
- last choosen broker & port is restored at next app start
- updated 3rd party libs (e.g. now using features of ControlsFX 8.0.6)
MQTT.fx - v0.0.4 - broker status
MQTT.fx - v0.0.4 - subscribe
MQTT.fx - v0.0.4 - connection options
MQTT.fx - v0.0.4 - validation

Fixed:
- publish to topics even if not subscribed before (in respect to @_efwe_)

Hint:
Configuration-format has changed, v0.0.4 might conflict with former versions.
In this case just delete (or rename) the mqttfx-config.xml.
Windows: [USER_HOME]\MQTT-FX\mqttfx-config.xml
OS X: [USER_HOME]/Library/Application Support/MQTT-FX/mqttfx-config.xml
The new configration is automatically created on next app start.

Jul 03

How to get rid of focus highlighting in JavaFX

Today I was asked if I know a way to get rid of the focus-highlighting of JavaFX controls (respectively buttons):

button_hover

Most posts and tipps regarding this issue suggesting to adding:

.button:focused {
    -fx-focus-color: transparent; 
}

But with this style a glow like this is still left:

button_glow

To get rid of this glow also often suggested to play around with -fx-background-insets additionally:

.button:focused {
    -fx-focus-color: transparent; 
    -fx-background-insets: -1.4, 0, 1, 2;
}

But this results in a button rendered with out an outer border:

button_insets

Compared to the default button style

button

this is still a kind of “highlighting”:

Why is that? (And why are there actually 4 inset values ?)
(note he comment of David Grieve below)

Having a look at the JavaFX default style as defined by modena.css bares some more information:

/* A bright blue for the focus indicator of objects. Typically used as the
* first color in -fx-background-color for the "focused" pseudo-class. Also
* typically used with insets of -1.4 to provide a glowing effect.
*/
-fx-focus-color: #f25f29;
-fx-faint-focus-color: #f25f2933;

Obviously there is not only one focus color -fx-focus-color but also -fx-faint-focus-color which is meant to create this glow-effect (that is remaining when setting -fx-focus-color:transparent;).

A closer look at the .button:focused pseudo class (in modena.css):

.button:focused {
    -fx-background-color: -fx-faint-focus-color, -fx-focus-color, -fx-inner-border, -fx-body-color; 
    -fx-background-insets: -2, -0.3, 1, 2;
    -fx-background-radius: 7, 6, 4, 3;
}

Playing around with some (extreme ;-)) colouring reveals the arrangement:

.button:focused {
    -fx-focus-color: red;
    -fx-faint-focus-color: green;
    -fx-inner-border: blue;
    -fx-body-color: orange;

    -fx-background-color: -fx-faint-focus-color, -fx-focus-color, -fx-inner-border, -fx-body-color; 
    -fx-background-insets: -2, -0.3, 1, 2;
    -fx-background-radius: 7, 6, 4, 3;
}

button_hover_style

Getting back to the topic maybe a clent way to remove the focus hightlight is to use the default button styles also for .button:focus (same approach for other controls):

.button:focused {
    -fx-background-color: -fx-outer-border, -fx-inner-border, -fx-body-color; 
    -fx-background-insets: 0, 1, 2;
    -fx-background-radius: 5, 4, 3;
}
Jun 26

At your Service!

The Service class (javafx.concurrency.Service) may not only be used to keep the UI reactive on long duration tasks but can also be used to change the state of controls or keep UI in a reasonable state on short term actions.

Recall that a Service is like a reusable Task. A Task can be executed once and has to be re-instantiated to be started again. The Service class creates a new Task each time it is (re-)started (createTask() is called then).

This is the start()-method from javafx.concurrency.Service class:

public void start() {

   checkThread();

    if (getState() != State.READY) {
        throw new IllegalStateException(
                "Can only start a Service in the READY state. Was in state " + getState());
    }

    // Create the task
    task = createTask();

    // Wire up all the properties so they use this task
    state.bind(task.stateProperty());
    value.bind(task.valueProperty());
    exception.bind(task.exceptionProperty());
    workDone.bind(task.workDoneProperty());
    totalWorkToBeDone.bind(task.totalWorkProperty());
    progress.bind(task.progressProperty());
    running.bind(task.runningProperty());
    message.bind(task.messageProperty());
	title.bind(task.titleProperty());

    // Advance the task to the "SCHEDULED" state
    task.setState(State.SCHEDULED);

    // Start the task
    executeTask(task);
}

Note that the properties binding of the Task (reset() is cleaning up all stuff).

In this post want to show how I’m using Services together with a JavaFX UI, like in MQTT.fx to connect or disconnect to a MQTT broker.

A simple connection pane with 5 controls:

connectionPane

Wanted appearance / behavior:
connectButton: only enabled if: not connected AND no service is running
disconnectButton: only enabled if: connected AND no service is running
cancelButton & progressIndicator: only visible if: any service is running
serviceMessageLabel: user feedback about the states of the services and the application

start

connecting

connected

disconnecting

The controller has two properties and contains two services (inner classes):

StringProperty statusMessagesProperty
BooleanProperty connectedProperty

ConnectService

private class ConnectService extends Service<Void> {

        @Override
        protected void succeeded() {
            statusMessagesProperty().set("Connected.");
            connectedProperty().set(true);
        }

        @Override
        protected void failed() {
            statusMessagesProperty().set("Connecting failed.");
            LOGGER.severe(getException().getMessage());
            connectedProperty().set(false);
        }

        @Override
        protected void cancelled() {
            statusMessagesProperty().set("Connecting cancelled.");
            connectedProperty().set(false);
        }

        @Override
        protected Task<Void> createTask() {
            return new Task<Void>() {
                @Override
                protected Void call() throws Exception {
                    updateMessage("Connecting....");
                    Thread.sleep(1000);
                    // DEMO: un-comment to provoke "Not on FX application thread"-Exception:
                    // connectButton.setVisible(false);
                    updateMessage("Waiting for server feedback.");
                    Thread.sleep(2000);
                    return null;
                }
            };
        }

    }

DisconnectService

    private class DisconnectService extends Service<Void> {

        @Override
        protected void succeeded() {
            statusMessagesProperty().set("");
            connectedProperty().set(false);
        }

        @Override
        protected void cancelled() {
            statusMessagesProperty().set("Disconnecting cancelled.");
            connectedProperty().set(false);
        }

        @Override
        protected Task<Void> createTask() {
            return new Task<Void>() {
                @Override
                protected Void call() throws Exception {
                    updateMessage("Disconnecting....");
                    Thread.sleep(1000);
                    updateMessage("Waiting for server feedback.");
                    Thread.sleep(2000);
                    return null;
                }
            };
        }

    }

The wanted UI-behavior can be accomplished just by some Bindings

BooleanBinding anyServiceRunning = connectService.runningProperty().or(disconnectService.runningProperty());
serviceRunningIndicator.visibleProperty().bind(anyServiceRunning);
cancelButton.visibleProperty().bind(anyServiceRunning);
connectButton.disableProperty().bind(connectedProperty().or(anyServiceRunning));
disconnectButton.disableProperty().bind(connectedProperty().not().or(anyServiceRunning));
messagesLabel.textProperty().bind(statusMessagesProperty());

ChangeListeners are passing the messages from the services to the statusMessagesProperty:

connectService.messageProperty().addListener((ObservableValue observableValue, String oldValue, String newValue) -> {
statusMessagesProperty().set(newValue);
});
disconnectService.messageProperty().addListener((ObservableValue observableValue, String oldValue, String newValue) -> {
statusMessagesProperty().set(newValue);
});

Three methods to control the services:

   @FXML
    public void cancel() {
        LOGGER.info("cancel");
        connectService.cancel();
        disconnectService.cancel();
    }

    @FXML
    public void connect() {
        LOGGER.info("connect");
        disconnectService.cancel();
        connectService.restart();
    }

    @FXML
    public void disconnect() {
        LOGGER.info("disconnect");
        connectService.cancel();
        disconnectService.restart();
    }

Nice & convenient:
Instead of verifying the state of the Service you can just call restart().
restart() executes:
1. cancel() (if running): cancel the executed service-task
2. reset(): clear the Service (task=null and unbind all stuff)
and then
3. start(): create new Task and bind its properties

You can get the complete code of this tutorial in my BitBucket repo:

ServiceDemoApp
https://bitbucket.org/Jerady/servicedemoapp/src

Jun 14

MQTT.fx 0.0.3 released

Most significant change: I have added a pane to show some status values of the broker:

mqtt-0.0.3

In v0.0.3 these $SYS topics are recognised (Tooltips are showing the descriptions):

Version: $SYS/broker/version
Build: $SYS/broker/timestamp
Uptime: $SYS/broker/uptime
Subscriptions Count: $SYS/broker/subscriptions/count
Changeset: $SYS/broker/changeset
Clients Connected: $SYS/broker/clients/active
Clients Expired: $SYS/broker/clients/expired
Clients Disconnected: $SYS/broker/clients/inactive
Clients Maximum: $SYS/broker/clients/maximum
Clients Total: $SYS/broker/clients/total
Connection: $SYS/broker/connection/#
Messages Inflight: $SYS/broker/messages/inflight
Messages Received: $SYS/broker/messages/received
Messages Sent: $SYS/broker/messages/sent
Messages Stored: $SYS/broker/messages/stored
Messages Publish Dropped: $SYS/broker/publish/messages/dropped
Messages Publish Received: $SYS/broker/publish/messages/received
Messages Publish Sent: $SYS/broker/publish/messages/sent
Messages Retained Count: $SYS/broker/retained/messages/count
Bytes Received: $SYS/broker/bytes/received
Bytes Sent: $SYS/broker/bytes/sent
Load Connections: $SYS/broker/load/connections/+
Load Bytes Received: $SYS/broker/load/bytes/received/+
Load Bytes Sent: $SYS/broker/load/bytes/sent/+
Load Messages Received: $SYS/broker/load/messages/received/+
Load Messages Sent: $SYS/broker/load/messages/sent/+
Load Publish Dropped: $SYS/broker/load/publish/dropped/+
Load Publish Received: $SYS/broker/load/publish/received/+
Load Publish Sent: $SYS/broker/load/publish/sent/+
Load Sockets Count: $SYS/broker/load/sockets/+

Download binaries:
Mac OSX DMG
Windows 32bit Installer
Zipped executable jar