9

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?

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
Christian Moen
  • 1,253
  • 2
  • 17
  • 31

2 Answers2

15

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

Thizzer
  • 16,153
  • 28
  • 98
  • 139
  • Is `JTouchBar` compatible with the latest version of `JavaFX`? See https://stackoverflow.com/questions/72297649/java-unsatisfiedlinkerror-unable-to-open-file. @Thizzer – SedJ601 May 19 '22 at 03:41
  • 2
    Unfortunately I have not been able to work on jtouchbar for a long while and there is little contribution from others. I would assume that there are compatibility issues with more recent libraries. – Thizzer May 19 '22 at 08:40
  • could you comment on the linked question? – SedJ601 May 19 '22 at 14:21
3

By the looks of it, apple doesn't provide support for adding items to the touch bar, never mind doing it in java.

While going through some documentation for the touch bar, it would appear that you will also need an instance of the NSTouchBarItem class. Java doesn't have that, nor provide a way to get that. I doubt that using native methods would work as well, seeing how the instance is app specific and is passed to the application through apple.

Accessing the bar is possible, but only natively.

Luke Melaia
  • 1,470
  • 14
  • 22
  • Thanks for the reply.. Kinda sucks, but just gonna deal with it until they open up for more than one programming language. – Christian Moen Dec 18 '16 at 19:43
  • 1
    NSTouchBarItem is a Cocoa API. But isn't it possible to call Cocoa APIs from within Java apps? There was an Apple supported official Java-Cocoa bridge back when Mac OS X was brand new and POSIX, Carbon, Cocoa, and Java were considered four equal. I'm not sure whether Apple still supports this (for example xcode no longer offers Java as an option). But answers to http://stackoverflow.com/questions/7907918/how-do-you-make-a-java-app-with-a-cocoa-ui suggest several third party cocoa-java bridges. Could these not be used to call the NSTouchBarItem API and add touchbar support to a Java app on Mac? – ziggurism Feb 08 '17 at 21:40
  • @ziggurism did you manage to find a way? I was trying to use https://github.com/shannah/Java-Objective-C-Bridge - but I don't know enough about objective-c – Mark Hughes Sep 30 '17 at 07:42