I have developed a desktop database application. But the interface is not rich. So I want integrate javafx interface in my code. But I am new to javafx, please give examples or tutorials for java and javafx integrated programs. I search in javafx site and some other books but I didn't get the java and javafx combined code.
Give me a code that replace Jbutton by javafx button.
import javax.swing.*;
class Javabutton extends JFrame
{
JButton b;
Container con;
Javabutton()
{
b=JButton("hello");
setLayout(new FlowLayout());
setSize(400,500);
con=getContentPane();
con.add(b);
setVisible(true);
}
public static void main(String args[])
{
new Javabutton();
}
}
`
You need to use JFXPanel, see http://docs.oracle.com/javafx/2.0/swing/jfxpub-swing.htm
It's better to write pure javaFX code other than using JFX with swing.
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.stage.Stage;
public class Hello extends Application
{
public static void main(String[] args)
{
Application.launch(args);
}
#Override
public void start(Stage primaryStage)
{
primaryStage.setTitle("Button");
Group root = new Group();
Scene scene = new Scene(root, 300, 250);
Button btn = new Button();
btn.setLayoutX(100);
btn.setLayoutY(80);
btn.setText("OK");
btn.setOnAction(new EventHandler<ActionEvent>()
{
public void handle(ActionEvent event)
{
System.out.println("Hello World");
}
});
root.getChildren().add(btn);
primaryStage.setScene(scene);
primaryStage.show();
}
}
Related
I am learning Javafx and wondering why this calling launch(args) in this code:
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.stage.Stage;
import javafx.scene.control.Button;
public class Gui extends Application{
#Override
public void start(Stage primaryStage){
Button btn = new Button("OK");
Scene scene = new Scene(btn, 200, 250);
primaryStage.setTitle("My First GUI");
primaryStage.setScene(scene);
primaryStage.show();
primaryStage.setResizable(true);
}
public static void main(String[] args) {
Application.launch(args);
}
}
is equivalent when we call
launch(args);
I've searched and found this answer "the JavaFX main class is a subtype of Application." but I can't understand it.
It's because you extended your gui class with javafx Application class. In other words, you inherited all of its methods including static void launch.
I am having some difficulty understanding how to set up a pop up window that displays a web page in my Swing application. I have seen this code across multiple tutorials but what I am struggling with is how to include this in my own code.
Every tutorial uses the main method with this code, but I want to call this from one of my other classes and I am not sure how.
This is the code in question:
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.layout.StackPane;
import javafx.scene.web.WebEngine;
import javafx.scene.web.WebView;
import javafx.stage.Stage;
public class WebViewMain extends Application {
public static void main(String[] args) {
Application.launch(args);
}
#Override
public void start(Stage primaryStage) throws Exception {
primaryStage.setTitle("WebView test");
WebView browser = new WebView();
WebEngine engine = browser.getEngine();
String url = "http://zoranpavlovic.blogspot.com/";
engine.load(url);
StackPane sp = new StackPane();
sp.getChildren().add(browser);
Scene root = new Scene(sp);
primaryStage.setScene(root);
primaryStage.show();
}
}
I want to be able to call the WebView from an ActionListener in one of my classes like this:
JButton mapExpandBtn = new JButton();
mapExpandBtn.setText("Expand");
mapExpandBtn.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent event) {
new WebViewMain();
}
});
This doesn't work obviously. How can I adapt my own code or the WebViewMain code to get it to launch in my application?
I was testing the MonologFX from JFXtras (v8.0-r5), but I got stuck with it!
Can anyone tell me how to check what was the button in the dialog that was pressed by the user? I tried in many ways, but no luck at all.
package javafx_jfxtras_monologfx;
import javafx.application.Application;
import javafx.scene.control.Button;
import javafx.scene.layout.StackPane;
import javafx.scene.Scene;
import javafx.stage.Stage;
import javafx.event.EventHandler;
import javafx.event.ActionEvent;
import jfxtras.labs.dialogs.MonologFX;
import jfxtras.labs.dialogs.MonologFXButton;
import jfxtras.labs.dialogs.MonologFX.Type;
public class JavaFX_JFXtras_MonologFX extends Application
{
#Override
public void start(Stage stage)
{
MonologFX m = new MonologFX();
m.setModal(true);
m.setType(Type.QUESTION);
m.setTitleText("JFXtras MonologFX");
m.setMessage("Do you want to continue?");
m.setPos(698, 450);
MonologFXButton mb1 = new MonologFXButton();
mb1.setType(MonologFXButton.Type.YES);
mb1.setLabel("Continue");
m.addButton(mb1);
MonologFXButton mb2 = new MonologFXButton();
mb2.setType(MonologFXButton.Type.NO);
mb2.setLabel("Exit");
m.addButton(mb2);
Button btn = new Button();
btn.setText("Click the Button");
btn.setOnAction(new EventHandler<ActionEvent>()
{
#Override
public void handle(ActionEvent event)
{
System.out.println("Hello :)");
}
});
StackPane root = new StackPane();
root.getChildren().add(btn);
Scene scene = new Scene(root, 300, 250);
stage.setTitle("JavaFX - JFXtras MonologFX");
stage.setScene(scene);
stage.show();
m.show();
}
public static void main(String[] args)
{
launch(args);
}
}
But the controls in labs are experimental and Mark has not worked on this one for a long time. We don't take them out because someone may use them, but as of version 8u40 JavaFX has a dialog itself. https://docs.oracle.com/javase/8/javafx/api/javafx/scene/control/Dialog.html|
I'm currently developing a JavaFX desktop application that will take the place of an existing browser based application.
This new application have the option of launching external applications made in swing.
I'm now wondering if there's any chance of launching these external applications inside the existing JavaFX application?
In other words, is there a way to embed external applications in JavaFX?
Do you mean something like this?
JavaFX:
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.stage.Stage;
public class Main extends Application {
public static void main(String[] args) {
launch(args);
}
#Override
public void start(Stage stage) {
Button fxButton = new Button("javafx button");
fxButton.setOnAction(event -> new SwingTest());
Scene scene = new Scene(fxButton, 600, 400);
stage.setScene(scene);
stage.show();
}
}
Swing:
import javax.swing.JFrame;
import javax.swing.WindowConstants;
import java.awt.Button;
public class SwingTest extends JFrame {
SwingTest() {
Button swingButton = new Button("swing button");
getContentPane().add(swingButton);
setSize(600, 600);
setVisible(true);
}
}
So I am trying to learn Java now that I know JavaScript and PHP. I am working in Netbeans with JavaFX and I am trying to create a program that creates 5 buttons. (This is modifying the code that comes with Netbeans when creating a new JavaFX Application.) If I change the y-argument of the scene to be less than the y of all of the buttons, it will not display the remaining buttons and it will not be able scroll down. This is what I have so far. How do I enable it to scroll down so all buttons can be seen? I know that I can just change the scene back to its old height but I want to learn about scrolling with JavaFX.
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.GridPane;
import javafx.stage.Stage;
import javax.swing.JScrollPane;
public class JavaFXApplication1 extends Application {
#Override
public void start(Stage primaryStage) {
GridPane root = new GridPane();
Button[] btn=new Button[5];
for(int i=0;i<5;i++){
btn[i] = new Button();
btn[i].setText(i+"");
GridPane.setRowIndex(btn[i],i);
root.getChildren().addAll(btn[i]);
btn[i].setOnAction(new EventHandler<ActionEvent>() {
#Override
public void handle(ActionEvent event) {
System.out.println("This is a button");
}
});
}
Scene scene = new Scene(root, 300, 50);
primaryStage.setTitle("Hello World!");
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
Using ScrollPane to set root as:
ScrollPane sp = new ScrollPane();
sp.setContent(root);
Scene scene = new Scene(sp, 300, 50);