Hatena::ブログ(Diary)

tomoTakaの日記

2013-02-07

NetBeans plugin for JavaFX Version

I wanted to make NetBeans plugin showing JavaFX version(like Figure1) by using JavaFX.
But when I read the article NetBeans NetBeans IDE 7.2 Plugin Quick Start I decided to use Swing with JavaFX(like Figure2)

  • Figure 1(This window is created by JavaFX)

f:id:tomoTaka:20130204074350p:image:w360

  • Figure 2(This window is created by Swing JFrame and JFXPanel)

The content that is surrounded with the square of the orange is the JFXPanel.
(FYR:http://docs.oracle.com/javafx/2/swing/swing-fx-interoperability.htm#CHDIEEJE )
f:id:tomoTaka:20130203205631p:image:w360

At first it was not difficult to develop the plugin thanks to the above article.
After the plugin runs, you see the icon on the toolbar(like Figure3)
You can use any icons when you create a plugin action.

  • Figure 3

f:id:tomoTaka:20130203205700p:image

The problem is when the I click the icon I see the window without the JFXPanel

  • Figure 4

f:id:tomoTaka:20130203205728p:image:w360

I am pretty sure I am doing something wrong.
I hope someone gives me any advice.The whole code is here(tomoTaka01/JavaFXVersionInfo ? GitHub)
I am looking forward to creating the toolbar by JavaFX!!!

  • code1(the code for the icon Action)
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import org.openide.awt.ActionID;
import org.openide.awt.ActionReference;
import org.openide.awt.ActionRegistration;
import org.openide.util.NbBundle.Messages;

@ActionID(
    category = "File",
id = "org.myorg.javafxversioninfo.VersionListener")
@ActionRegistration(
    iconBase = "org/myorg/javafxversioninfo/version.png",
displayName = "#CTL_VersionListener")
@ActionReference(path = "Toolbars/File", position = 0)
@Messages("CTL_VersionListener=JavaFXVersion")
public final class VersionListener implements ActionListener {
    @Override
    public void actionPerformed(ActionEvent e) {
        SwingVersion swingVersion = new SwingVersion();
        swingVersion.main(null);
//        Application.launch(JavaFXVersionDisplay.class);  // this code does not work???
    }
}
  • code2(SwingVersion class)
import com.sun.javafx.runtime.VersionInfo;
import java.awt.BorderLayout;
import java.awt.Container;
import javafx.application.Platform;
import javafx.embed.swing.JFXPanel;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.effect.InnerShadow;
import javafx.scene.layout.ColumnConstraints;
import javafx.scene.layout.GridPane;
import javafx.scene.paint.Color;
import javafx.scene.text.Font;
import javafx.scene.text.FontWeight;
import javafx.scene.text.Text;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.SwingUtilities;

public class SwingVersion {
    private static void initAndShowGui() {
        JFrame jFrame = new JFrame("test");
        final JFXPanel jfxPanel = new JFXPanel();
        jFrame.add(jfxPanel);
        jFrame.setSize(500, 200);
        // JavaFX version by Swing
        String javaFxVer = VersionInfo.getRuntimeVersion();
        JLabel jLabel = new JLabel("JavaFX version(by Swing) is " + javaFxVer);
        Container contentPane = jFrame.getContentPane();
        contentPane.add(jLabel, BorderLayout.PAGE_START);
        jFrame.setVisible(true);
        jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        // this code does not work???
        Platform.runLater(new Runnable() {
            @Override
            public void run() {
                initFX(jfxPanel);
            }
        });
    }
    private static void initFX(JFXPanel fxpanel){
        Scene scene = createScene();
        fxpanel.setScene(scene);
    }
    private static Scene createScene() {
        GridPane grid = new GridPane();
        grid.setAlignment(Pos.CENTER);
        grid.setHgap(10);
        grid.setVgap(10);
        grid.setPadding(new Insets(25));
        // *** for row size
        ColumnConstraints col1 = new ColumnConstraints(250);
        grid.getColumnConstraints().add(col1);
        // Title label
        InnerShadow is = new InnerShadow();
        is.setOffsetX(0.1f);
        is.setOffsetY(0.1f);
        Text scenetitle = new Text("JavaFX Version Display");        
        scenetitle.setEffect(is);
        scenetitle.setFill(Color.AQUA);
        scenetitle.setFont(Font.font("Tahoma", FontWeight.BOLD, 36));
        grid.add(scenetitle, 0, 0, 2, 1);
        // JavaFX version info
        Label l1 = new Label("Version from VersionInfo is ");
        l1.setFont(Font.font(null, FontWeight.NORMAL, 18));
        grid.add(l1, 0, 1);
        final Text t1 = new Text(VersionInfo.getRuntimeVersion());
        t1.setFont(Font.font(null, FontWeight.BOLD, 18));
        grid.add(t1, 1, 1);
        // JavaFX version info
        Label l2 = new Label("Version from property is ");
        l2.setFont(Font.font(null, FontWeight.NORMAL, 18));
        grid.add(l2, 0, 2);
        final Text t2 = new Text(System.getProperty("javafx.runtime.version"));
        t2.setFont(Font.font(null, FontWeight.BOLD, 18));
        grid.add(t2, 1, 2);
        // Java version info
        Label l3 = new Label("Java Version is ");
        l3.setFont(Font.font(null, FontWeight.NORMAL, 18));
        grid.add(l3, 0, 3);
        final Text t3 = new Text(System.getProperty("java.version"));
        t3.setFont(Font.font(null, FontWeight.BOLD, 18));
        grid.add(t3, 1, 3);
        return new Scene(grid, Color.ALICEBLUE);
        
    }
    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                initAndShowGui();
            }
        });
    }
}

keep coding...

欽ちゃん1号欽ちゃん1号 2013/02/07 22:38 表示できない主な理由はJFrameにaddした時にはまだjfxPanelが準備できていないからですね。
こんな感じでいけます。
public class SwingVersion {

public SwingVersion(){
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
initAndShowGui();
}
});
}

private static void initAndShowGui() {
JFrame jFrame = new JFrame("test");
final JFXPanel jfxPanel = new JFXPanel();
jfxPanel.setPreferredSize(new java.awt.Dimension(500, 180));
jFrame.setLayout(new BorderLayout());

jFrame.setSize(500, 200);
// JavaFX version by Swing
String javaFxVer = VersionInfo.getRuntimeVersion();
JLabel jLabel = new JLabel("JavaFX version(by Swing) is " + javaFxVer);

jFrame.add(jLabel, BorderLayout.NORTH);

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

jFrame.add(jfxPanel, BorderLayout.CENTER);
jFrame.pack();

jFrame.setVisible(true);
jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

}
private static void initFX(JFXPanel fxpanel){
Scene scene = createScene();
fxpanel.setScene(scene);
}
private static Scene createScene() {
GridPane grid = new GridPane();
grid.setAlignment(Pos.CENTER);
grid.setHgap(10);
grid.setVgap(10);
grid.setPadding(new Insets(25));
// *** for row size
ColumnConstraints col1 = new ColumnConstraints(250);
grid.getColumnConstraints().add(col1);
// Title label
InnerShadow is = new InnerShadow();
is.setOffsetX(0.1f);
is.setOffsetY(0.1f);
Text scenetitle = new Text("JavaFX Version Display");
scenetitle.setEffect(is);
scenetitle.setFill(Color.AQUA);
scenetitle.setFont(Font.font("Tahoma", FontWeight.BOLD, 36));
grid.add(scenetitle, 0, 0, 2, 1);
// JavaFX version info
Label l1 = new Label("Version from VersionInfo is ");
l1.setFont(Font.font(null, FontWeight.NORMAL, 18));
grid.add(l1, 0, 1);
final Text t1 = new Text(VersionInfo.getRuntimeVersion());
t1.setFont(Font.font(null, FontWeight.BOLD, 18));
grid.add(t1, 1, 1);
// JavaFX version info
Label l2 = new Label("Version from property is ");
l2.setFont(Font.font(null, FontWeight.NORMAL, 18));
grid.add(l2, 0, 2);
final Text t2 = new Text(System.getProperty("javafx.runtime.version"));
t2.setFont(Font.font(null, FontWeight.BOLD, 18));
grid.add(t2, 1, 2);
// Java version info
Label l3 = new Label("Java Version is ");
l3.setFont(Font.font(null, FontWeight.NORMAL, 18));
grid.add(l3, 0, 3);
final Text t3 = new Text(System.getProperty("java.version"));
t3.setFont(Font.font(null, FontWeight.BOLD, 18));
grid.add(t3, 1, 3);
return new Scene(grid, Color.ALICEBLUE);

}

public static void main(String[] args) {
SwingVersion sv = new SwingVersion();

}
}

tomoTakatomoTaka 2013/02/08 08:10 ありがとうございます!Swingはまったくの初心者で、、、レイアウトの指定も勉強になりました。
修正して動作確認できました。修正したコードはgithubにもアップしました。

Thank you for your kind help!
The code works due to your advice. I am very glad to see the result.

The fixed code is here(https://github.com/tomoTaka01/JavaFXVersionInfo)

スパム対策のためのダミーです。もし見えても何も入力しないでください
ゲスト


画像認証

トラックバック - http://d.hatena.ne.jp/tomoTaka/20130207/1360191634
リンク元