Create X window (X11) in java swing and get its id

Can someone help me in creating an X11 window in java swing using eclipse ?, as well as a function to get the x11 id. Which is the basic requirement for creating an X11 window in java.

+3
source share
5 answers
Tom answered the first part of your question. The second part of the answer: to get the X11 window identifier, you have to use your own code (code written in C or C ++) and access it through the JNI interface.

You may need to run a name search through all existing windows to get the one you need.

Here is a recursive function that will search (starting from the root window) for the 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 has never been fixed. If you run valgrind and have problems with a memory leak, this causes them.

+3
source

To deploy @Zubzub and @ArtemGr answers , the following minimalistic AWT example is given to me, at least in Sun 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 check its identifier using the utility xwininfo.

Please note, however, that even if your AWT application will only run on X11, the above solution will not migrate:

  • GCJ gnu.java.awt.peer.gtk.GtkFramePeer,
  • Sun JDK (1.4 ) sun.awt.motif.MFramePeer. , XToolkit Sun JDK, 1,5, MToolkit - 1.5 1.6,
  • IBM J9, BEA JRockit, Oracle lwAWT Apache Harmony.
+2

X11 Swing , new Frame(), setVisible(true). . , java.net.Socket 6000 , X11.

+1

Sun JVM "setAccessible (true)", X11 Sun. https://www.docjar.com/docs/api/sun/awt/X11/XWindow.html

, : , , , . , XlibUtil . Scala: http://gist.github.com/567076

JNA xlib Java; . : Xlib JNA

+1

, :

You need to find the most suitable component of your program and get the component component. On linux, it will be an XComponent peer type that extends XWindow, which extends XBaseWindow. XBaseWindow has an attribute window of type long. This is what you are looking for. You probably need to use reflection to get to it.

HF

+1
source

Source: https://habr.com/ru/post/1719481/


All Articles