java - How do I use javax.swing components in an application? -


disclaimer: new java, i've been building .net applications 13 years.

i'm trying build java application basic calculations tutoring. it's not big of program, can't hello, world! state! have requirement that's making difficult:

gui should built using javax.swing components jbutton, jlabel, jtextfield, jtextarea, jframe, , jpanel.

so, downloaded netbeans 7.3 , created javafx in swing application. default code works uses button instead of jbutton:

private void createscene() {     button btn = new button();     btn.settext("say 'hello world'");     btn.setonaction(new eventhandler<actionevent>() {         @override         public void handle(actionevent event) {             system.out.println("hello world!");         }     });     stackpane root = new stackpane();     root.getchildren().add(btn);     fxcontainer.setscene(new scene(root)); } 

so when change use jbutton have change type root built from. while banging head against wall found example here (not directly related) used jrootpane , thought might work in place of stackpane. refactored code this:

private void createscene() {     jbutton btn = new jbutton();     btn.settext("say 'hello world'");     jrootpane root = new jrootpane();     root.getcontentpane().add(btn);     fxcontainer.setscene(new scene(root)); } 

and code fine except fxcontainer.setscene(new scene(root)); because root isn't parent.

fyi, application class implements japplet , has main and init looks this:

public static void main(string[] args) {     swingutilities.invokelater(new runnable() {         @override         public void run() {             try {                 uimanager.setlookandfeel("com.sun.java.swing.plaf.nimbus.nimbuslookandfeel");             } catch (exception e) {             }              jframe frame = new jframe("tutoring calculator");             frame.setdefaultcloseoperation(jframe.exit_on_close);              japplet applet = new tutoringcalculator();             applet.init();              frame.setcontentpane(applet.getcontentpane());              frame.pack();             frame.setlocationrelativeto(null);             frame.setvisible(true);              applet.start();         }     }); }  @override public void init() {     fxcontainer = new jfxpanel();     fxcontainer.setpreferredsize(new dimension(jfxpanel_width_int, jfxpanel_height_int));     add(fxcontainer, borderlayout.center);     // create javafx scene     platform.runlater(new runnable() {         @override         public void run() {             createscene();         }     }); } 

how can fulfill requirement stated above? going whole thing wrong way?

sscce

/*  * change template, choose tools | templates  * , open template in editor.  */ package tutoringcalculator;  import java.awt.borderlayout; import java.awt.dimension; import javafx.application.platform; import javafx.embed.swing.jfxpanel; import javafx.event.actionevent; import javafx.event.eventhandler; import javafx.scene.scene; import javafx.scene.control.button; import javafx.scene.layout.stackpane; import javax.swing.japplet; import javax.swing.jbutton; import javax.swing.jframe; import javax.swing.jrootpane; import javax.swing.swingutilities; import javax.swing.uimanager;  /**  *  * @author owner  */ public class tutoringcalculator extends japplet {      private static final int jfxpanel_width_int = 300;     private static final int jfxpanel_height_int = 250;     private static jfxpanel fxcontainer;      /**      * @param args command line arguments      */     public static void main(string[] args) {         swingutilities.invokelater(new runnable() {             @override             public void run() {                 try {                     uimanager.setlookandfeel("com.sun.java.swing.plaf.nimbus.nimbuslookandfeel");                 } catch (exception e) {                 }                  jframe frame = new jframe("tutoring calculator");                 frame.setdefaultcloseoperation(jframe.exit_on_close);                  japplet applet = new tutoringcalculator();                 applet.init();                  frame.setcontentpane(applet.getcontentpane());                  frame.pack();                 frame.setlocationrelativeto(null);                 frame.setvisible(true);                  applet.start();             }         });     }      @override     public void init() {         fxcontainer = new jfxpanel();         fxcontainer.setpreferredsize(new dimension(jfxpanel_width_int, jfxpanel_height_int));         add(fxcontainer, borderlayout.center);         // create javafx scene         platform.runlater(new runnable() {             @override             public void run() {                 createscene();             }         });     }      private void createscene() {         jbutton btn = new jbutton();         btn.settext("say 'hello world'");         jrootpane root = new jrootpane();         root.getcontentpane().add(btn);         fxcontainer.setscene(new scene(root));     } } 

this code works either applet or desktop app. here:

import java.awt.borderlayout; import java.awt.dimension; import javax.swing.*;  // <applet code=tutoringcalculator width=400 height=400></applet> public class tutoringcalculator extends japplet {      // size of applet set html!     //private static final int jfxpanel_width_int = 300;     //private static final int jfxpanel_height_int = 250;      public static void main(string[] args) {         swingutilities.invokelater(new runnable() {             @override             public void run() {                 jframe frame = new jframe("tutoring calculator");                 frame.setdefaultcloseoperation(jframe.exit_on_close);                  japplet applet = new tutoringcalculator();                 applet.init();                  frame.setcontentpane(applet.getcontentpane());                  frame.pack();                 frame.setlocationrelativeto(null);                 frame.setvisible(true);                  applet.start();             }         });     }      private jpanel swingcontainer;      @override     public void init() {         swingcontainer = new jpanel(new borderlayout());         add(swingcontainer, borderlayout.center);         createscene();     }      private void createscene() {         jbutton btn = new jbutton();         btn.settext("say 'hello world'");         jrootpane root = new jrootpane();         root.getcontentpane().add(btn);         swingcontainer.add(root);     } } 

i not bothered figuring why using rootpane left is.

further tips

  • the spec. mentions jframe not japplet. since applets harder develop, debug , deploy, suggest focus entirely on getting working in frame.
  • for frame positioning, cannot go setlocationbyplatform(true). see this answer demo.

Comments

Popular posts from this blog

jquery - How can I dynamically add a browser tab? -

node.js - Getting the socket id,user id pair of a logged in user(s) -

keyboard - C++ GetAsyncKeyState alternative -