padman
padman

Reputation: 501

java and javafx integrated programs

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();
    }
}

`

Upvotes: 0

Views: 567

Answers (2)

Anu
Anu

Reputation: 1423

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();
    }
}

Upvotes: 0

Sergey Grinev
Sergey Grinev

Reputation: 34528

You need to use JFXPanel, see http://docs.oracle.com/javafx/2.0/swing/jfxpub-swing.htm

Upvotes: 1

Related Questions