Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BridJ] ITaskbarList3 doesn't work with JavaFX stage #47

Open
ochafik opened this issue Mar 18, 2015 · 2 comments
Open

[BridJ] ITaskbarList3 doesn't work with JavaFX stage #47

ochafik opened this issue Mar 18, 2015 · 2 comments
Labels

Comments

@ochafik
Copy link
Member

ochafik commented Mar 18, 2015

From @AnirvanSarkar on January 27, 2015 10:1

ITaskbarList3 works as expected with JFrame but not with JavaFX stage.

For example, the following Swing code displays a progress in taskbar:

import javax.swing.JFrame;
import org.bridj.Pointer;
import org.bridj.cpp.com.COMRuntime;
import org.bridj.cpp.com.shell.ITaskbarList3;
import org.bridj.jawt.JAWTUtils;

public class SwingTaskbar extends JFrame {

    ITaskbarList3 list;
    Pointer<?> hwnd;

    public SwingTaskbar() throws ClassNotFoundException {

        list = COMRuntime.newInstance(ITaskbarList3.class);

    }

    @Override
    protected void finalize() throws Throwable {

        super.finalize();
        list.Release();

    }

    @Override
    public void setVisible(boolean visible) {

        super.setVisible(visible);

        long hwndVal = JAWTUtils.getNativePeerHandle(this);
        hwnd = Pointer.pointerToAddress(hwndVal);
        list.SetProgressValue((Pointer) hwnd, 50, 100);

    }

    public static void main(String[] args) {

        try {

            SwingTaskbar f = new SwingTaskbar();
            f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            f.pack();
            f.setVisible(true);

        } catch (Throwable ex) {

            ex.printStackTrace();

        }

    }

}

But its JavaFX counterpart doesn't show one:

import javafx.application.Application;
import javafx.stage.Stage;
import org.bridj.Pointer;
import org.bridj.cpp.com.COMRuntime;
import org.bridj.cpp.com.shell.ITaskbarList3;

public class FXTaskbar extends Application {

    ITaskbarList3 list;
    Pointer<?> hwnd;

    @Override
    public void init() throws ClassNotFoundException {

        list = COMRuntime.newInstance(ITaskbarList3.class);

    }

    @Override
    public void stop() {
        list.Release();
    }

    @Override
    public void start(Stage primaryStage) {

        primaryStage.show();

        long hwndVal = com.sun.glass.ui.Window.getWindows().get(0).getNativeWindow();
                //com.sun.glass.ui.Window.getFocusedWindow().getNativeHandle();
        hwnd = Pointer.pointerToAddress(hwndVal);
        list.SetProgressValue((Pointer) hwnd, 50, 100);

    }

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

}
OS: Windows 7
Java Version: 1.8.0_31
BridJ Version: bridj-0.7-20141231.010908-73-windows-only

Copied from original issue: nativelibs4java/nativelibs4java#556

@GregoryHlavac
Copy link

I recently found the fix to this when digging into BridJ because I want to do some windows taskbar stuff with an FX application, turns out the COM stuff was being created on the main thread (The one that the VM is launched with) and then the rest of the modification (In the 'start') method was being called on a different thread (The JavaFX application thread) and that was throwing the COM/RPC stuff for a loop.

I'm not sure if this is something to be fixed in BridJ or something that you'll just have to be aware of when trying to use this stuff in JavaFX.

import javafx.application.Application;
import javafx.stage.Stage;
import org.bridj.Pointer;
import org.bridj.cpp.com.COMRuntime;
import org.bridj.cpp.com.shell.ITaskbarList3;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class FXTaskbar extends Application
{
  private ExecutorService es;

  ITaskbarList3 list;
  Pointer<?> hwnd;

  @Override
  public void init()
  {
    es = Executors.newSingleThreadExecutor(r -> {
      Thread t = new Thread(r);

      t.setDaemon(true);

      return t;
    });


    es.execute(() -> {
      try
      {
        list = COMRuntime.newInstance(ITaskbarList3.class);
      }
      catch (ClassNotFoundException e)
      {
        e.printStackTrace();
      }
    });
  }

  @Override
  public void stop()
  {
    es.submit(() -> list.Release());
  }

  @Override
  public void start(Stage primaryStage)
  {
    primaryStage.show();
    long hwndVal = com.sun.glass.ui.Window.getWindows().get(0).getNativeWindow();
    //com.sun.glass.ui.Window.getFocusedWindow().getNativeHandle();
    hwnd = Pointer.pointerToAddress(hwndVal);

    es.execute(() -> {
      list.SetProgressState((Pointer) hwnd, ITaskbarList3.TbpFlag.TBPF_ERROR);
      list.SetProgressValue((Pointer) hwnd, 50, 100);
    });
  }

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

}

@GregoryHlavac
Copy link

@ochafik You can probably close this issue following my answer.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants