Skip to main content

Working Workaround to integrate JavaFX in Java

3 replies [Last post]
riepi
Offline
Joined: 2008-03-27
Points: 0

Hello, I wonder if there is now an easy way to integrate my JavaFX classes, the whole GUI, in an existing Java Application? I have found the article "Using Java Objects Within JavaFX Programs" (http://java.sun.com/developer/technicalArticles/scripting/javafx/javafx_...). I followed the article and tried the different ways, but always got problems. Would be nice to see an example that works. Any ideas?
Greetings riepi

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
riepi
Offline
Joined: 2008-03-27
Points: 0

ok, thx. This seems to work. I will try around with the older examples again this week and post the problems.

alexsch
Offline
Joined: 2007-07-12
Points: 0

- Create a [b]CustomComponent.fx[/b] class

[code]
import javafx.ext.swing.*;

import java.awt.BorderLayout;

import javax.swing.JPanel;
import javax.swing.JComponent;

import javafx.ext.swing.Layout.*;

public abstract class CustomComponent extends Component{

public abstract function create(): Component;

protected function createJComponent(): JComponent {
var panel = new JPanel(new BorderLayout());
panel.add (create().getJComponent(), BorderLayout.CENTER);
return panel;
}

}
[/code]

- Create a JavaFX component extending [b]CustomComponent[/b] class:

[code]
import javafx.ext.swing.*;

public class MyComponent extends CustomComponent{

public attribute value:Integer= 10;

public function create():Component{
return FlowPanel {
content:[
Slider{
minimum: -30
maximum: 30
value: bind value with inverse
},
Label{ text: bind "Value: {value}!"}
]
}
}
}
[/code]

- Put the component to java code using [b]createJComponent()[/b] method:

[code]
import javax.swing.JFrame;

public class MyFrame extends JFrame{

public MyFrame(){
setSize(200, 100);
setTitle("MyFrame");

getContentPane().add(new MyComponent().createJComponent());

}

public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new MyFrame().setVisible(true);
}
});
}

}
[/code]

That is it.

michael_heinrichs
Offline
Joined: 2007-11-15
Points: 0

Sorry, the examples worked when this article was written, but that's a long time ago. At least a long time in the JavaFX universe. :-)

Can you please describe the problems you had using the examples?