Thursday, December 11, 2008

JavaFX with Your Dad's Java (a.k.a. Swing)

JavaFX deliberately left out a lot of Swing components because they think it is targeted for RIA and mobile platform.  I am not sure if that logic make sense.  Anyway, here is a minimum example of how to reuse existing Swing component in JavaFX.  In the example below I use a JEditorPane as a JavaFX component.

SimpleSwingEditor.fx

package com.aspectsolution.javafx.example;

import javafx.ext.swing.SwingComponent;
import javax.swing.JEditorPane;
import java.awt.Dimension;

public class SimpleSwingEditor extends SwingComponent {

public override function createJComponent() {
var wrappedEditor: JEditorPane;
wrappedEditor = new JEditorPane();
wrappedEditor.setPreferredSize(new Dimension(width, height));
return new JEditorPane();
}
}


Main.fx

/*
* Main.fx
*
* Created on Dec 10, 2008, 11:23:38 PM
*/

package com.aspectsolution.javafx.example;

import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.Group;
import javafx.scene.text.Text;
import javafx.scene.text.Font;
import javafx.scene.shape.Rectangle;
import javafx.scene.shape.Circle;
import javafx.scene.paint.Color;

Stage {
title: "Simple Swing in FX"
width: 400
height: 300
scene: Scene {
content:
Group {
content: [
Rectangle {
width: 400
height: 300
fill: Color.BEIGE
},
Circle {
centerX:200
centerY:130
radius: 120
fill: Color.LIGHTBLUE
},
Text {
font: Font {
size: 24
}
x: 10, y: 30
content: "A Simple Editor"
},
SimpleSwingEditor {
width: 400, height: 270
translateY: 60
opacity: 0.8
}
]
}

}
}



No comments: