Replies: 3 comments 2 replies
-
|
Use JImplements and JOverride.
https://jpype.readthedocs.io/en/latest/api.html#decorators
Java:
public interface MyInterface
{
void doSomething(int i);
}
Python:
@jpype.JImplements(MyInterface)
class MyImplemention(object):
@jpype.JOverride
def doSomething(self, i):
print(i)
There is nothing more to it.
|
Beta Was this translation helpful? Give feedback.
2 replies
-
|
Wow, that was quick.
My use case is different:
I have an industrial tool with a main controller (Windows computer) that
runs a Java application to manage the tool.
From time to time the tool requires calibration. The Java has a scheduler
to handle that.
I would like to write the calibration as a python app that the Java
scheduler can invoke (shell script to run compiled python) and the java
needs to be able to control the python app. not just by passing arguments
on startup, but by calling python methods.
I was thinking of creating an interface to python methods that the Java
will be able to call.
Again, appreciate you time and effort
Oded
…On Mon, Feb 10, 2025 at 5:55 PM Karl Nelson ***@***.***> wrote:
I am not sure exactly how one would run both a Java and Tk in the same
python application. I wouldn't use the pattern. Generally I write a GUI in
Java first then have Python launch it.
I will have to assume that your awt application actually works (I use
JavaFX not awt)
import javax.swing.;
import java.awt.;
public class TestWindow extends JFrame {// implements TestInterface {
public DoubleSupplier call;
private JButton button;
public testWindow() {
// Set window properties
setTitle("Simple Window");
setSize(300, 200);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null);
// Create button
button = new JButton("Test Button");
button.addActionListener(e -> testMethod());
// Add button to center of window
setLayout(new FlowLayout(FlowLayout.CENTER));
add(button);
}
public void testMethod() {
double v = call.getAsDouble();
System.out.println("Test method called from button click "+ v);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(() -> {
TestWindow window = new TestWindow();
window.setVisible(true);
});
}
}
--------------Python App------------
import random
import jpype
import jpype.imports
jpype.startJVM(classpath="blah blah")
from mypkg import TestWindow
from javax.swing import SwingUtilities
window = TestWindow()
def generate_random():
random_num = random.randint(1, 100)
result_label.config(text=f"Random Number: {random_num}")
return random_num
# We can pass a Python function to a Java functor and connect functionality between languages.
window.call = generate_random
def mylaunch():
window.setVisible()
# We pass a function in as a runnable and Java will run it in the GUI thread
SwingUtilities.invokeLater(mylaunch)
#There is a little special code required if running on a OSX machine as we are required to ensure the main loop gets serviced,
# But otherwise stuff just runs.
—
Reply to this email directly, view it on GitHub
<#1260 (reply in thread)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AFM2Z3VQYBYA6NQOE7XJCDD2PDDWJAVCNFSM6AAAAABWZH75J6VHI2DSMVQWIX3LMV43URDJONRXK43TNFXW4Q3PNVWWK3TUHMYTEMJSGI2DINA>
.
You are receiving this because you authored the thread.Message ID:
***@***.***>
|
Beta Was this translation helpful? Give feedback.
0 replies
-
|
You can push as much as you want through functors or implementation of interfaces. I have exposed large Python sections to Java for my GUI work. Just decide if you need to make it class like (requiring bridge classes for each object) or if it is more functional in which case you just copy all the Python methods to corresponding functors in Java. Both methods can work. Unfortunately, I never found enough users for the reverse bridge which automatically writes the bridge classes for Java. With that you could do things like run matplotlib and numpy from Java side. It was very cool, but required a full test bench and a lot of user review. The manual method is more cumbersome, but it works well enough for most applications.
Good luck.
|
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
Hi,
Im looking for a good example or explanation of how to implement a Java interface, that is, I would like to provide a Java app an interface to methods in a python app. Read the documentation, but i think it assumes a deeper Java knowledge than i have.
Thanks
Beta Was this translation helpful? Give feedback.
All reactions