4

We're building a JavaFX application in Windows.

I've tried to use JNA Native.getComponentPointer() method which works with java.awt.panel, but I can't figure out a good way to do this with a javafx.scene.layout.Pane

Does anyone know of any way to do get hWnd for a Pane ?

Akila
  • 1,258
  • 2
  • 16
  • 25
yeyb
  • 41
  • 1
  • 3
    Does it have one? AWT uses the native OS libraries to draw components, so every AWT component is a Windows component (on Windows), but I thought JavaFX draws its own components without relying on an underlying toolkit... – Itai Dec 20 '17 at 09:31
  • 1
    @sillyfly As far as I know, that is right. This is exactly why JavaFX can afford so much customization in how things are actually rendered. If this is done by native components, then things like CSS wouldn't have work right. – Jai Dec 20 '17 at 10:05
  • JavaFX nodes are rendered within windows. All windows on the Windows OS system have an `hWnd`, which is used by the native Windows toolkit. It is not directly exposed via any public API though. – jewelsea Dec 21 '17 at 01:23
  • @yeyb I'm struggling with same issue. Did you get a solution to this or some other way to work with JavaFX and JNA? – codeur Jan 31 '20 at 10:18

2 Answers2

2

Unfortunately, I do not think that this is possible with JavaFX. The only thing that I can see that has a native peer is the Stage. You can verify this yourself by using a ui analyzer tool, such as WinSpy, or "inspect.exe" or "visualuiaverifynative.exe" from the Windows SDK.

From Mixing Heavyweight and Lightweight Components by Oracle:

There are two kinds of graphics components in the Java programming language: heavyweight and lightweight. A heavyweight component is associated with its own native screen resource (commonly known as a peer). Components from the java.awt package, such as Button and Label, are heavyweight components.

[...]

A lightweight component has no native screen resource of its own, so it is "lighter." A lightweight component relies on the screen resource from an ancestor in the containment hierarchy, possibly the underlying Frame object. Components from the javax.swing package, such as JButton and JLabel, are lightweight components.

In this case, JavaFX is just like Swing; a lightweight toolkit where the various ui components are all written in Java and do not have a native peer.

Community
  • 1
  • 1
Cypher
  • 2,608
  • 5
  • 27
  • 41
1

As far as my reading of the doc goes, there is a 1:1 relationship between an hWnd and a window object. Panes and Nodes aren't Windows, so trying to get an hwnd specific node doesn't seem to make a lot of sense.

The most you could ask for is the hwnd of the window containing the node. See:

Applying the function from the answer to the linked question gets:

Pointer hwnd = getWindowPointer(node.getScene().getWindow());
jewelsea
  • 150,031
  • 14
  • 366
  • 406
  • thank you ,but i do need this hwnd ,We're building a JavaFX application in Windows which do something about video.the video SDK need this hwnd to point the video plays where.does it possible if i add a awt.Panel in javaFx ,or i hava to give up javaFx ,use awt instead – yeyb Dec 21 '17 at 01:38
  • I don't understand your point. Does the answer I linked not demonstrate to you how to get the hWnd? – jewelsea Dec 21 '17 at 05:25
  • i don't need a window handle ,i need a component handle ,but only heavyweight component has a handle .how to get a component handle in javaFx , add a awt.Panel in javaFx ? – yeyb Dec 21 '17 at 09:11
  • That makes no sense. You asked how to get hWnd. A hWnd only exists for a window, its [definition](https://msdn.microsoft.com/en-us/library/windows/desktop/aa383751(v=vs.85).aspx) is "a handle to a window". It is not a "component handle" (whatever that is, I don't know what you mean by that). – jewelsea Dec 21 '17 at 09:30
  • sorry for that . JNA Native.getComponentPointer() method which works with java.awt.panel ,return the panel pointer --aPointer .then use new W32API.HWND(aPointer ) get the panel handle .how to do somthing like this in javaFx – yeyb Dec 21 '17 at 09:48
  • Some control types do have an hWnd. ControlType.Pane, for example, which is what I believe the java.awt.Panel may be mapping to. @yeyb If your application requires you to have a native handle to anything but the Stage itself, then switch to AWT. What you want does not exist in JavaFX. – Cypher Dec 23 '17 at 00:25
  • @yeyb However, if you haven't looked already, you may want to see if the javafx.scene.media package can load and play the video formats you want. – Cypher Dec 23 '17 at 00:28