Search code examples
javamacosnstouchbarmacbookpro-touch-bar

Create java applications with MacBook Pro touchbar support?


as the tile describe, I would love to have the ability to add some cool touchbar buttons to my java application for MacBook Pro 2016 users.. I've not seen yet if there is a way to implement it in java yet.

Anyone got some knowledge on that?


Solution

  • There is a new Java library for interacting with the NSTouchBar API called JTouchBar.

    For example using SWT

    Shell shell = ...
    
    JTouchBar jTouchBar = new JTouchBar();
    jTouchBar.setCustomizationIdentifier("MySWTJavaTouchBar");
    
    // flexible space
    jTouchBar.addItem(new TouchBarItem(TouchBarItem.NSTouchBarItemIdentifierFlexibleSpace));
    
    // button
    TouchBarButton touchBarButtonImg = new TouchBarButton();
    touchBarButtonImg.setTitle("Button 1");
    touchBarButtonImg.setAction(new TouchBarViewAction() {
        @Override
        public void onCall( TouchBarView view ) {
            System.out.println("Clicked Button_1.");
        }
    });
    
    Image image = new Image();
    img.setName(ImageName.NSImageNameTouchBarColorPickerFill);
    touchBarButtonImg.setImage(image);
    
    jTouchBar.addItem(new TouchBarItem("Button_1", touchBarButtonImg, true));
    
    // label
    TouchBarTextField touchBarTextField = new TouchBarTextField();
    touchBarTextField.setStringValue("TextField 1");
    
    jTouchBar.addItem(new TouchBarItem("TextField_1", touchBarTextField, true));
    
    // enable touchbar
    jTouchBar.enableForShell(shell);
    

    You can find the library on Github: https://github.com/Thizzer/JTouchBar