Giter Club home page Giter Club logo

java-gtk's Introduction

Build on Debian Build on Fedora Build on Ubuntu Build on Windows JitPack

Java-GTK

GTK 4 bindings for Java. It uses Java Native Access (JNA) to access GTK libraries.

POC screenshot

Example

package examples;

import ch.bailu.gtk.gio.ApplicationFlags;
import ch.bailu.gtk.gtk.Application;
import ch.bailu.gtk.gtk.ApplicationWindow;
import ch.bailu.gtk.gtk.Button;
import ch.bailu.gtk.type.Strs;

public class HelloWorld {
    public static void main(String[] args) {
        var app = new Application("com.example.hello",
                ApplicationFlags.FLAGS_NONE);

        app.onActivate(() -> {
            // Create a new window
            var window = new ApplicationWindow(app);

            // Create a new button
            var button = new Button();

            // Set button label
            button.setLabel("Hello, World!");

            // When the button is clicked, close the window
            button.onClicked(window::close);

            window.setChild(button);
            window.show();
        });

        // Start application main loop
        var result = app.run(args.length, new Strs(args));

        // Terminate with exit code
        System.exit(result);
    }
}

Build and run

./gradlew generate or make gen
Compiles and runs the code generator. This will generate Java code from GIR files.

./gradlew build or make
Creates library, javadoc and run tests

  • Output: jar, javadoc.jar and sources.jar in libray/build/libs/

./gradlew run or make run
Run the default demo application. There are more demo applications in examples/src/main/java/examples including GeoClue2 and Adwaita samples.

make install
Compile Java library, generate JAR archive and copy JAR archive as artifact to local Maven repository (~/.m2/repository).

Integration

Library and Javadoc is available via JitPack.

// build.gradle.kts
plugins {
    application
}

repositories {
    maven { url = uri("https://jitpack.io") }
}

dependencies {
    implementation("com.github.bailuk:java-gtk:0.5.0")
}

application {
    mainClass.set("examples.HelloWorld")
}

Modules

  • generator/: Kotlin application that generates Java code from GIR files (xml parser -> model builder -> writer). GIR files are taken from Debian dev packages.
  • java-gtk/ : java-gtk library depends on generated Java code.
  • examples/ : Some examples to test the bindings. Mostly ported from https://gitlab.gnome.org/GNOME/gtk/-/tree/main/demos/gtk-demo.

License

java-gtk's People

Contributors

bailuk avatar jcwasmx86 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

java-gtk's Issues

macos startup of example causes exception

On Macos 10, 12 and 13 using either java 17 or java 19 with homebrew gtk 4.10.4 on a paired down example from v0.4.0:

results.txt

2023-06-08 10:06:16.095 java[7961:51584] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'nextEventMatchingMask should only be called from the Main Thread!'
*** First throw call stack:
(
0 CoreFoundation 0x00007ff80ec2a773 __exceptionPreprocess + 242
1 libobjc.A.dylib 0x00007ff80e98abc3 objc_exception_throw + 48
2 AppKit 0x00007ff8115e72d1 -[NSApplication(NSEvent) _nextEventMatchingEventMask:untilDate:inMode:dequeue:] + 4633
3 libgtk-4.1.dylib 0x00000001094ceb43 poll_func + 179
4 libglib-2.0.0.dylib 0x0000000102b4901b g_main_context_iterate + 349
5 libglib-2.0.0.dylib 0x0000000102b490cc g_main_context_iteration + 55
6 libgio-2.0.0.dylib 0x0000000108d74ca3 g_application_run + 521
7 jna16203219141762940508.tmp 0x00000001029eb17a ffi_prep_go_closure + 1370
8 ??? 0x0000700001efc688 0x0 + 123145334802056
)
libc++abi: terminating with uncaught exception of type NSException

Runs fine on Fedora 38 with openjdk 17.

Invalid Memory Access on windows

whenever i use various Adw related classes in my main method, such as running App.run() just like the example, when running on windows, the program immediately crashes, citing an "Invalid memory access" error. the same thing happens when i use Adw.init().

i am on 0.5.0

the libraries were downloaded from msys2 and added to PATH

Not working, finished with non-zero exit value 139

The example or anything coded with this library isn't working.
I tested it with self compiling and with the jitpack version. Both not working.
the example with "make run" does also not work.

image

(Running on Garuda Linux, gtk 4 is installed and other gtk 4 application does work)

Question: could this be combined via graalvm in theory?

Question: could this be combined via graalvm in theory?

i am asking because graalvm is pretty cool, and others have managed to get SWING applications to run.

My use case would be more like taking an existing python or ruby code base, commandline stuff,
and add a simple GUI around it. I don't really want to write in C though (Java is ok but Swing is
ugly, and JavaFX does not seem to be supported via Graalvm; also the graalvm team focuses
not so much on GUIs)

Broken button text in FileChooserDialog

This code sample:

public static void main(String[] args) {
    var app = new Application(new Str("example.javagtk.FileChooserDialog"), ApplicationFlags.FLAGS_NONE);
    app.onActivate(() -> {
        var appWin = new ApplicationWindow(app);
        var fcd = new FileChooserDialog(new Str("Open savefile"), appWin, FileChooserAction.OPEN, new Str("Open"));
        appWin.show();
        fcd.show();
    });
    System.exit(app.run(args.length, new Strs(args)));
}

Produces a GTK file chooser dialog with broken button text, seen below:
image
The broken text seems to be read from an incorrect place in memory by GTK. Pressing any of the buttons in my case also results in a segfault.
This happens on a Debian Unstable machine, using the GTK4 library from apt, and OpenJDK 17 installed through sdkman.

Offer a Gradle Plugin

I'm very interested in this library and I'd like to know if there's any interest in offering a Gradle plugin to ease the code generation aspect. Of course I'm also interested in helping with code if needed.

Support request: Help gtk-kt with GIR

Hello, I know that this is a weird request, so feel free to just say no, but I was wondering if you will be open to help the gtk-kt project ^^
The gtk-kt project is focused on Kotlin native, and not JNA so isn't exactly a direct competitor of your project.
Currently there is only one maintainer that started writing the bindings manually but now he's working on a GIR generator for the Kotlin native code. But it isn't an easy task to do without previous knowledge of GIR so I was wondering if you could help us.
If you are wondering who am I, I'm just an Android developer that would love to be able to write GTK application in Koltin native. I started contributing to the gtk-kt project, very recently and, since I do not have a lot of C interop knowledge, my contributions are mostly on Gradle and pure Kotlin code level. And maybe a little of PR ^^.

does this work on windows?

i'd like to make a cross-platform app using this and it would be very nice to know before i start if this can run on windows. as i dont have a windows installation right now, i have no way to verify

cairo-custom.gir

I've noticed that two methods I use in gtk.cairo.Context are not present and have added them. They are really needed to easily use showText. Was there a reason they were not included?

select_font.txt

How to use

How to setup a project? What do i need to do to make a new gradle project with this library?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.