123ArticleOnline Logo
Welcome to 123ArticleOnline.com!
ALL >> Computer-Programming >> View Article

Integration Of Swing And Swt With Javafx 2.0

Profile Picture
By Author: mayur aegis
Total Articles: 64
Comment this article
Facebook ShareTwitter ShareGoogle+ ShareTwitter Share

This article contains some observations, thoughts related to Java Development. Based on the observation some tips and tricks would help to get solutions for problems that are often faced during Java application development. While there are also some interesting and useful concepts focused here that will help Java developers to easily face issues and provide their clients with better Java solutions. Clients can choose to outsource Java development services and get it handled right from scratch to the complete process of superior quality of technical solution creation. Outsource Java development includes everything from specification writing, planning, QA, deployment and much more.
Best possible improvement that should be implemented while Java development is to integrate JavaFX 2.0 with Swing and SWT which results into better interoperability. This could be done with some useful tips available online and includes information regarding integration of JavaFX into Swing Applications and SWT Interop. An example of effective class-level Javadoc documentation, the respective JavaFX classes javafx.embed.swing.JFXPanel and javafx.embed.swt.FXCanvasprovides ...
... a simple code sample that helps in using the class for embedding JavaFX into swing or SWT code.
JavaFX Scene is allowed to be set on their instance by both JFXPanel and FXCanvas. The instance of Scene is used in examples in this post are provided by the method shown in the next JavaFX-specific code example.
Method that provides a JavaFX Scene for Integration:
import javafx.application.Platform;
import javafx.embed.swing.JFXPanel;
import javafx.scene.Scene;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;

/**
* Simple class demonstrating interoperability between Swing and JavaFX. This
* class is adapted from the example provided in the Javadoc documentation for
* {@code javafx.embed.swing.JFXPanel}.
*/
public class SwingJavaFxInteroperabilityDemo
{
private static void initAndShowGUI()
{
// This method is invoked on Swing thread
final JFrame frame = new JFrame("JavaFX / Swing Integrated");
final JFXPanel fxPanel = new JFXPanel();
frame.add(fxPanel);
frame.setVisible(true);

Platform.runLater(new Runnable()
{
@Override
public void run()
{
initFX(fxPanel);
}
});
}

private static void initFX(JFXPanel fxPanel)
{
// This method is invoked on JavaFX thread
final Scene scene = TextIntegrationSceneCreator.createTextScene();
fxPanel.setScene(scene);
}

public static void main(String[] arguments)
{
SwingUtilities.invokeLater(new Runnable()
{
@Override
public void run()
{
initAndShowGUI();
}
});
}
}
Now for integrating JavaFX Scene into Swing code via the JavaFX class JFXPanel and its setScene method is used. Please see its demonstration in the next example below which will help particular Scene instance from the method in previous code listing.
JavaFX/Swing Integration with JFXPanel
viewplainprint?
package dustin.examples;

import javafx.application.Platform;
import javafx.embed.swing.JFXPanel;
import javafx.scene.Scene;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;

/**
* Simple class demonstrating interoperability between Swing and JavaFX. This
* class is adapted from the example provided in the Javadoc documentation for
* {@code javafx.embed.swing.JFXPanel}.
*/
public class SwingJavaFxInteroperabilityDemo
{
private static void initAndShowGUI()
{
// This method is invoked on Swing thread
final JFrame frame = new JFrame("JavaFX / Swing Integrated");
final JFXPanel fxPanel = new JFXPanel();
frame.add(fxPanel);
frame.setVisible(true);

Platform.runLater(new Runnable()
{
@Override
public void run()
{
initFX(fxPanel);
}
});
}

private static void initFX(JFXPanel fxPanel)
{
// This method is invoked on JavaFX thread
final Scene scene = TextIntegrationSceneCreator.createTextScene();
fxPanel.setScene(scene);
}

public static void main(String[] arguments)
{
SwingUtilities.invokeLater(new Runnable()
{
@Override
public void run()
{
initAndShowGUI();
}
});
}
}
Now we will see next is the output of running this simple Java Swing application with embedded JavaFX Scene while creating acceptable and successful Java solutions. Integration of SWT with JavaFX is arguably easier as seen below. The main aim is to call FXCanvas's setScene(Scene) method.
JavaFX/SWT Integration with FXCanvas
package dustin.examples;

import javafx.embed.swt.FXCanvas;
import javafx.scene.Scene;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;

/**
* Simple class demonstrating interoperability between SWT and JavaFX. This
* class is based on the example provided in the Javadoc documentation for
* {@code javafx.embed.swt.FXCanvas}.
*
* @author Dustin
*/
public class SwtJavaFxInteroperabilityDemo
{
public static void main(String[] arguments)
{
final Display display = new Display();
final Shell shell = new Shell(display);
shell.setText("JavaFX / SWT Integration");
shell.setLayout(new FillLayout());
final FXCanvas canvas = new FXCanvas(shell, SWT.NONE);
final Scene scene = TextIntegrationSceneCreator.createTextScene();
canvas.setScene(scene);
shell.open();
while (!shell.isDisposed())
{
if (!display.readAndDispatch()) display.sleep();
}
display.dispose();
}
}
These code listings shown above are only little adapted from the Javadoc documentation for the JavaFX classes JFXPanel (Swing) and FXCanvas (SWT).

For More Information:- Java Development | Java Application Development

Total Views: 215Word Count: 572See All articles From Author

Add Comment

Computer Programming Articles

1. Which Are The Best Java Coding Classes In Bhopal?
Author: Shankar Singh

2. Warehouse Management In Zambia: Essential Features To Look For
Author: Doris Rose

3. Ecommerce Web Design And Development In Melbourne With The Merchant Buddy
Author: themerchantbuddy

4. Why Website Maintenance Is Crucial For Business Success
Author: Yogendra Shinde

5. Boost Your Business With Smart Invoice Pos Software In Zambia
Author: Cecilia Robert

6. How Stablecoin Development Ensures Stability And Security?
Author: Michael noah

7. Công Cụ Tính Chiều Cao Chuẩn Từ Minbin Tool: Đo Lường Và Cải Thiện Chiều Cao Hiệu Quả
Author: KenJi123

8. How To Make A Courier App For Courier Delivery And Tracking Service
Author: Deorwine Infotech

9. Reputation Management In The Digital Age: Protecting And Enhancing Your Law Firm’s Image
Author: jamewilliams

10. What Features Should I Look For In Wordpress Ecommerce Plugins?
Author: Rocket Press

11. Staying Updated With The Latest Gaming News
Author: Next Tech Plus

12. Game Development: Evolving Technologies And New Horizons
Author: Rick John

13. Why Svelte Is The Most In-demand Framework For Web Development?
Author: Why Svelte Is The Most In-Demand Framework For Web

14. Maximizing Ebay Sales With Maropost/neto And Pursuit Info Solutions
Author: rachelvander

15. The Importance Of Software Testing: Ensuring Quality In Development
Author: John Mathew

Login To Account
Login Email:
Password:
Forgot Password?
New User?
Sign Up Newsletter
Email Address: