Can anyone help me in creating an X11 window in java swing using eclipse?And also the function to get the x11 id also.What are the basic requirement for creating an X11 window in java.
5 Answers
Tom answered the first part of your question. The second part of the answer is: to get the id of an X11 window you are going to have to use native code (code written in C or C++) and access it through the JNI interface.
You may have to run a search by title through all existing windows to get the one you desire.
Here is a recursive function that will search (starting from the root window) for a window with the desired name
Window windowWithName(Display *dpy, Window top, char *name)
{
Window *children, dummy;
unsigned int nchildren;
unsigned int i;
Window w = 0;
char *window_name;
if (XFetchName(dpy, top, &window_name) && !strcmp(window_name, name))
return (top);
if (!XQueryTree(dpy, top, &dummy, &dummy, &children, &nchildren))
return (0);
for (i = 0; i < nchildren; i++)
{
w = windowWithName(dpy, children[i], name);
if (w)
break;
}
if (children)
XFree((char *) children);
return (w);
}
Note: **unfortunately there is a well documented memory leak in the XFetchName function implemented in X11 that was never fixed. If you run valgrind and have minor memory leak issues this is whats causing them.

- 11,707
- 10
- 54
- 70
-
No, Java does not implement this. Keep in mind that once you get a Xwindow id in Java you will not be able to do anything with it except manipulate it like any regular integer in Java. This means that if you are going to do *something* with your window id, it's most likely going to have to be done in native code again. – ldog Oct 07 '09 at 16:37
-
Thanks a lot for the information but wht i am planning to do is to invoke an executable c prgm to which i cud pass the window id for displaying a video stream.Hope it might work. – Raji Oct 08 '09 at 04:53
Creating an X11 window in Swing is as easy as new Frame()
followed by setVisible(true)
. Getting any of the unabstracted details will be harder. Of course, you can always open a java.net.Socket
to port 6000 or so and speak X11 yourself.

- 145,806
- 30
- 211
- 305
-
-
Not the same, but creating an AWT frame will create at least one X11 window. It's many years since I looked at X11. IIRC, X11 will create a "window" for every heavyweight `Component`. – Tom Hawtin - tackline Oct 07 '09 at 16:09
-
JFrame in java swing is also a heavy weight component so hope tht will also be creating an X window – Raji Oct 08 '09 at 07:32
Just to expand the answers @Zubzub and @ArtemGr gave, the following minimalistic AWT example works for me, at least in Sun's Java 1.8:
import java.awt.Dimension;
import javax.swing.JFrame;
import sun.awt.X11.XWindow;
class C {
public static void main(final String args[]) {
final JFrame frame = new JFrame();
frame.setPreferredSize(new Dimension(200, 200));
frame.pack();
frame.setVisible(true);
final XWindow xWindow = (XWindow) frame.getPeer();
frame.setTitle("Window id: 0x" + Long.toHexString(xWindow.getWindow()));
}
}
Once the window is visible, you can verify its id with xwininfo
utility.
Please note, however, that even provided your AWT application will only run under X11, the above solution is not portable:
- GCJ will use
gnu.java.awt.peer.gtk.GtkFramePeer
, and - older Sun JDK versions (1.4 and below) will use
sun.awt.motif.MFramePeer
. DespiteXToolkit
is the default on Sun JDK starting with 1.5, oldMToolkit
can still be re-enabled on 1.5 and 1.6, and - I'm not sure about IBM J9, BEA JRockit, Oracle
lwAWT
and Apache Harmony.
If you're using a Sun JVM and don't mind overriding package access using reflection and "setAccessible(true)", then you can create an X11 window using the Sun's toolkit. https://www.docjar.com/docs/api/sun/awt/X11/XWindow.html
Not all methods of the toolkit might be present, though: I suspect methods not used in the toolkit itself were removed. For example, most of the XlibUtil aren't there. Here's an example how to use reflection to access the toolkit methods, in Scala: http://gist.github.com/567076
You can also use JNA to code xlib parts in Java; see question: Using Xlib via JNA to move a window
For those out there still looking for this:
You need to find the most toplevel component of your program and get the component's 'componentpeer'. Under linux this will be of type XComponent peer which extends XWindow which extends XBaseWindow. XBaseWindow has an attribute window of type long. That's what you're looking for. You probably need to use reflection to get to it.
hf

- 782
- 7
- 18