JavaFX Draggable Tabs

The TabPane in JavaFX unfortunately does not provide mouse dragging / re-ording out of the box which is quite fustrating as users are now accustomed to doing that in their Browsers whey have Tabbed Browsing switched on.

There is a Jira Raised for this feature :

https://bugs.openjdk.java.net/browse/JDK-8092098

There Jira was targeted for Java 9, however Java 9 is now feature complete, so it’s likely Java 10 is the earliest we will see this being available.

Available Solutions

  1. https://github.com/sibvisions/javafx.DndTabPane

    This implementation draws a nice marker line as you drag the tabs around.

  2. http://berry120.blogspot.co.uk/2014/01/draggable-and-detachable-tabs-in-javafx.html
    This seems to be the simplest solution, its relatively easy to understand how it’s working (it works both Java 8 and Java 9-ea b158), in addition to the draggable tabs feature, it’s also detaching (tabs can be dragged out of the window into their own window).

    There is a few things you have to observe if you want to use it, as detailed in his blog.

  3. https://github.com/xylo/DraggableTabs

    There is this project which seems to based on similar solution to above, it’s more polished, has some sample code under tests, it’s also published onto maven central

JavaFX Undecorated Window Task Icon Minimize

The JavaFX undecorated window gives you a blank canvas, giving freedom to how your window looks without having to accept the default Windows title bar and buttons such as the Minimize, Maximise and Close buttons.

On Windows, the disadvantage of the Undecorated stage is if you click on the icon on the Taskbar, you don’t get the minimise behaviour compared to other Windows app. Likewise, if you use the keyboard shortcut “Windows Key + M” all your apps except for the Undecorated JavaFX app will minimise.

This can be quite fustrating.

There is a stackoverflow post on this issue:

http://stackoverflow.com/questions/26972683/javafx-minimizing-undecorated-stage

This answer from StackOverflow post seems to work

import com.sun.jna.Pointer;
import com.sun.jna.platform.win32.User32;
import com.sun.jna.platform.win32.WinDef;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.layout.Pane;
import javafx.stage.Stage;
import javafx.stage.StageStyle;

import static com.sun.jna.platform.win32.WinUser.GWL_STYLE;

public class SimpleWindowApplication extends Application {



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

    @Override
    public void start(final Stage stage) {
        Scene scene = new Scene(new Pane(new Label("Hello World")));
        stage.initStyle(StageStyle.UNDECORATED);
        stage.setTitle("Find this window");
        stage.setScene(scene);
        stage.show();
        long lhwnd = com.sun.glass.ui.Window.getWindows().get(0).getNativeWindow();
        Pointer lpVoid = new Pointer(lhwnd);
        WinDef.HWND hwnd = new WinDef.HWND(lpVoid);
        final User32 user32 = User32.INSTANCE;
        int oldStyle = user32.GetWindowLong(hwnd, GWL_STYLE);
        System.out.println(Integer.toBinaryString(oldStyle));
        int newStyle = oldStyle | 0x00020000;//WS_MINIMIZEBOX
        System.out.println(Integer.toBinaryString(newStyle));
        user32.SetWindowLong(hwnd, GWL_STYLE, newStyle);
    }
}

It uses the JNA library add the WS_MINIMIZEBOX style to the existing Window.

Dependencies required if your using gradle are

    compile 'net.java.dev.jna:jna:4.3.0'
    compile 'net.java.dev.jna:jna-platform:4.3.0'


Java 9

Java 9 modularity adds stronger encapsulation, if you attempt to run the sample program under Java 9, you’ll get IllegalAccessError as the com.sun.glass.ui package is not visible to unnamed modules

To make the example run on Java 9 you will need to add this additional VM Option

--add-exports=javafx.graphics/com.sun.glass.ui=ALL-UNNAMED

Enforcing Coding Standards using Checkstyle

Checkstyle is a great tool for enforcing agreed code style across a team.

To make sure everyone in your team is observing the agreed code style. You can get the checkstyle maven plugin to fail the build if a rule is violated

Here is a simple example demonstration a simple project with checkstyle rules applied. Here is an example of checkstyle failing our build, we have setup checkstyle config in this example project to enforce left curly is a line by itself, but as you can see in the example project, the Hello class has violated the rule.

Checkstyle has an an extensive list of checks you can configure and you even write your checks aswell.

checkstyle-build-check-goal