Note that there are some explanatory texts on larger screens.

plurals
  1. POHow do I implement JavaFX in existing WindowBuilder project ?
    text
    copied!<p>I am trying to get my head around the JavaFX stuff... My program is a WindowBuilder based Gui, and I want a JavaFX graph, and a JavaFX live video-feed displayed in my app.</p> <p>How do I implement it in my code? I have tried <a href="http://docs.oracle.com/javafx/2/swing/swing-fx-interoperability.htm#CHDIEEJE" rel="nofollow">this</a>, but I couldn't get it runnning. The data feed isnt the problem. I just need to view it inside my JFrame as small squares...</p> <p><em>Confused now :(</em> </p> <p>Here is my code: (I am sorry that it is a tad long, but I blame the examplecode from JavaFX :p </p> <pre><code>import javax.swing.JFrame; import javax.swing.JTextField; import javax.swing.UIManager; import java.awt.Canvas; import java.awt.SystemColor; import java.io.BufferedReader; import java.io.BufferedWriter; import javax.swing.UIManager.*; /** * @author * */ public class MyClientApp extends JFrame { /** * */ private static final long serialVersionUID = 1L; protected static final String BufferedWriter = null; JFrame frame; private JTextField textFieldUsername; /** * Create the application. */ public MyClientApp(BufferedWriter serverDataOut, BufferedReader serverDataIn) { initialize(); } /** * Initialize the contents of the frame. * * @param serverDataOut * */ private void initialize() { try { for (LookAndFeelInfo info : UIManager.getInstalledLookAndFeels()) { if ("Nimbus".equals(info.getName())) { UIManager.setLookAndFeel(info.getClassName()); break; } } } catch (Exception e) { // Nimbus Theme not avaliable } frame = new JFrame(); frame.setResizable(false); frame.setTitle("*********** My Program ***********"); frame.setBounds(320, 130, 730, 570); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.getContentPane().setLayout(null); Canvas canvasTemp = new Canvas(); canvasTemp.setBackground(SystemColor.window); canvasTemp.setBounds(6, 277, 380, 255); frame.getContentPane().add(canvasTemp); Canvas canvasLevel = new Canvas(); canvasLevel.setBackground(SystemColor.window); canvasLevel.setBounds(6, 10, 380, 255); frame.getContentPane().add(canvasLevel); } public JFrame frame() { return frame; } } </code></pre> <p>And here is the main file to get it running for you guys... : </p> <pre><code>Client.java import java.io.IOException; public class Client { public static void main(String[] args) throws IOException, InterruptedException { MyClientApp window = new MyClientApp(null, null); window.frame.setVisible(true); } } </code></pre> <p>The code I want to implement is: AdvancedLineChartSample.java (from JavaFX)</p> <pre><code>/** * Copyright (c) 2008, 2012 Oracle and/or its affiliates. * All rights reserved. Use is subject to license terms. */ import javafx.application.Application; import javafx.scene.Group; import javafx.scene.Scene; import javafx.stage.Stage; import java.util.ArrayList; import java.util.List; import javafx.animation.KeyFrame; import javafx.animation.Timeline; import javafx.collections.FXCollections; import javafx.collections.ObservableList; import javafx.event.ActionEvent; import javafx.event.EventHandler; import javafx.geometry.Side; import javafx.scene.Node; import javafx.scene.chart.LineChart; import javafx.scene.chart.NumberAxis; import javafx.scene.chart.XYChart; import javafx.scene.chart.XYChart.Series; import javafx.util.Duration; /** * An advanced line chart with a variety of actions and settable properties. * * @see javafx.scene.chart.LineChart * @see javafx.scene.chart.Chart * @see javafx.scene.chart.NumberAxis * @see javafx.scene.chart.XYChart */ public class AdvancedLineChartSample extends Application { private void init(Stage primaryStage) { Group root = new Group(); primaryStage.setScene(new Scene(root)); root.getChildren().add(createChart()); } protected LineChart&lt;Number, Number&gt; createChart() { final NumberAxis xAxis = new NumberAxis(); final NumberAxis yAxis = new NumberAxis(); final LineChart&lt;Number,Number&gt; lc = new LineChart&lt;Number,Number&gt;(xAxis,yAxis); // setup chart lc.setTitle("Temp Chart"); xAxis.setLabel("tid"); yAxis.setLabel("temp"); // add starting data XYChart.Series&lt;Number,Number&gt; series = new XYChart.Series&lt;Number,Number&gt;(); series.setName("Dataset 1"); series.getData().add(new XYChart.Data&lt;Number,Number&gt;(20d, 50d)); series.getData().add(new XYChart.Data&lt;Number,Number&gt;(40d, 80d)); series.getData().add(new XYChart.Data&lt;Number,Number&gt;(50d, 90d)); series.getData().add(new XYChart.Data&lt;Number,Number&gt;(70d, 30d)); series.getData().add(new XYChart.Data&lt;Number,Number&gt;(170d, 122d)); lc.getData().add(series); return lc; } @Override public void start(Stage primaryStage) throws Exception { init(primaryStage); primaryStage.show(); } public static void main(String[] args) { launch(args); } } </code></pre> <p>and the StreamingMediaPlayer.java (from JavaFX):</p> <pre><code>/** * Copyright (c) 2008, 2012 Oracle and/or its affiliates. * All rights reserved. Use is subject to license terms. */ import javafx.application.Application; import javafx.scene.Group; import javafx.scene.Scene; import javafx.stage.Stage; import javafx.application.Platform; import javafx.beans.InvalidationListener; import javafx.beans.Observable; import javafx.event.ActionEvent; import javafx.event.EventHandler; import javafx.geometry.Insets; import javafx.geometry.Pos; import javafx.scene.control.Label; import javafx.scene.control.Slider; import javafx.scene.layout.BorderPane; import javafx.scene.layout.HBox; import javafx.scene.layout.Pane; import javafx.scene.layout.Priority; import javafx.scene.layout.Region; import javafx.scene.media.Media; import javafx.scene.media.MediaPlayer; import javafx.scene.media.MediaPlayer.Status; import javafx.scene.media.MediaView; import javafx.util.Duration; import javafx.beans.value.ChangeListener; import javafx.beans.value.ObservableValue; import javafx.animation.ParallelTransition; import javafx.scene.control.*; import javafx.scene.layout.*; import javafx.scene.paint.Color; /** * A media player with controls for play, pause, stop, seek, and volume. This media player is playing media via HTTP Live Streaming, also known as HLS. * * @see javafx.scene.media.MediaPlayer * @see javafx.scene.media.Media */ public class StreamingMediaPlayer extends Application { private static final String MEDIA_URL = "http://download.oracle.com/otndocs/products/javafx/JavaRap/prog_index.m3u8"; private MediaPlayer mediaPlayer; private void init(Stage primaryStage) { Group root = new Group(); primaryStage.setScene(new Scene(root)); mediaPlayer = new MediaPlayer(new Media(MEDIA_URL)); mediaPlayer.setAutoPlay(true); PlayerPane playerPane = new PlayerPane(mediaPlayer); playerPane.setMinSize(480, 360); playerPane.setPrefSize(480, 360); playerPane.setMaxSize(480, 360); // getStylesheets().add("ensemble/samples/media/OverlayMediaPlayer.css"); root.getChildren().add(playerPane); } public void play() { Status status = mediaPlayer.getStatus(); if (status == Status.UNKNOWN || status == Status.HALTED) { return; } if (status == Status.PAUSED || status == Status.STOPPED || status == Status.READY) { mediaPlayer.play(); } } @Override public void stop() { mediaPlayer.stop(); } static class PlayerPane extends BorderPane { private MediaPlayer mp; private MediaView mediaView; private final boolean repeat = false; private boolean stopRequested = false; private boolean atEndOfMedia = false; private Duration duration; private Slider timeSlider; private Label playTime; private Slider volumeSlider; private HBox mediaTopBar; private HBox mediaBottomBar; private ParallelTransition transition = null; @Override protected void layoutChildren() { if (mediaView != null &amp;&amp; getBottom() != null) { mediaView.setFitWidth(getWidth()); mediaView.setFitHeight(getHeight() - getBottom().prefHeight(-1)); } super.layoutChildren(); if (mediaView != null) { mediaView.setTranslateX((((Pane)getCenter()).getWidth() - mediaView.prefWidth(-1)) / 2); mediaView.setTranslateY((((Pane)getCenter()).getHeight() - mediaView.prefHeight(-1)) / 2); } } @Override protected double computeMinWidth(double height) { return mediaBottomBar.prefWidth(-1); } @Override protected double computeMinHeight(double width) { return 200; } @Override protected double computePrefWidth(double height) { return Math.max(mp.getMedia().getWidth(), mediaBottomBar.prefWidth(height)); } @Override protected double computePrefHeight(double width) { return mp.getMedia().getHeight() + mediaBottomBar.prefHeight(width); } @Override protected double computeMaxWidth(double height) { return Double.MAX_VALUE; } @Override protected double computeMaxHeight(double width) { return Double.MAX_VALUE; } public PlayerPane(final MediaPlayer mp) { this.mp = mp; setId("player-pane"); mediaView = new MediaView(mp); Pane mvPane = new Pane() { }; mvPane.setId("media-pane"); mvPane.getChildren().add(mediaView); setCenter(mvPane); mediaTopBar = HBoxBuilder.create() .padding(new Insets(5, 10, 5, 10)) .alignment(Pos.CENTER) .opacity(1) .build(); BorderPane.setAlignment(mediaTopBar, Pos.CENTER); mediaBottomBar = HBoxBuilder.create() .padding(new Insets(5, 10, 5, 10)) .alignment(Pos.CENTER) .opacity(1) .build(); BorderPane.setAlignment(mediaBottomBar, Pos.CENTER); mp.currentTimeProperty().addListener(new ChangeListener&lt;Duration&gt;() { @Override public void changed(ObservableValue&lt;? extends Duration&gt; observable, Duration oldValue, Duration newValue) { updateValues(); } }); mp.setOnPlaying(new Runnable() { public void run() { if (stopRequested) { mp.pause(); stopRequested = false; } } }); mp.setOnReady(new Runnable() { public void run() { duration = mp.getMedia().getDuration(); updateValues(); } }); mp.setOnEndOfMedia(new Runnable() { public void run() { if (!repeat) { stopRequested = true; atEndOfMedia = true; } } }); mp.setCycleCount(repeat ? MediaPlayer.INDEFINITE : 1); // Time label Label timeLabel = LabelBuilder.create() .text("Time") .minWidth(Control.USE_PREF_SIZE) .textFill(Color.WHITE) .build(); mediaTopBar.getChildren().add(timeLabel); // Time slider timeSlider = SliderBuilder.create() .id("media-slider") .minWidth(240) .maxWidth(Double.MAX_VALUE) .build(); timeSlider.valueProperty().addListener(new InvalidationListener() { public void invalidated(Observable ov) { if (timeSlider.isValueChanging()) { // multiply duration by percentage calculated by slider position if (duration != null) { mp.seek(duration.multiply(timeSlider.getValue() / 100.0)); } updateValues(); } } }); HBox.setHgrow(timeSlider, Priority.ALWAYS); mediaTopBar.getChildren().add(timeSlider); // Play label playTime = LabelBuilder.create() .prefWidth(130) .minWidth(50) .textFill(Color.WHITE) .build(); mediaTopBar.getChildren().add(playTime); // Volume label Label volumeLabel = LabelBuilder.create() .text("Vol") .textFill(Color.WHITE) .minWidth(Control.USE_PREF_SIZE) .build(); mediaTopBar.getChildren().add(volumeLabel); // Volume slider volumeSlider = SliderBuilder.create() .id("media-slider") .prefWidth(120) .maxWidth(Region.USE_PREF_SIZE) .minWidth(30) .build(); volumeSlider.valueProperty().addListener(new InvalidationListener() { public void invalidated(Observable ov) { } }); volumeSlider.valueProperty().addListener(new ChangeListener&lt;Number&gt;() { @Override public void changed(ObservableValue&lt;? extends Number&gt; observable, Number oldValue, Number newValue) { if (volumeSlider.isValueChanging()) { mp.setVolume(volumeSlider.getValue() / 100.0); } } }); mediaTopBar.getChildren().add(volumeSlider); setTop(mediaTopBar); final EventHandler&lt;ActionEvent&gt; backAction = new EventHandler&lt;ActionEvent&gt;() { public void handle(ActionEvent e) { mp.seek(Duration.ZERO); } }; final EventHandler&lt;ActionEvent&gt; stopAction = new EventHandler&lt;ActionEvent&gt;() { public void handle(ActionEvent e) { mp.stop(); } }; final EventHandler&lt;ActionEvent&gt; playAction = new EventHandler&lt;ActionEvent&gt;() { public void handle(ActionEvent e) { mp.play(); } }; final EventHandler&lt;ActionEvent&gt; pauseAction = new EventHandler&lt;ActionEvent&gt;() { public void handle(ActionEvent e) { mp.pause(); } }; final EventHandler&lt;ActionEvent&gt; forwardAction = new EventHandler&lt;ActionEvent&gt;() { public void handle(ActionEvent e) { Duration currentTime = mp.getCurrentTime(); mp.seek(Duration.seconds(currentTime.toSeconds() + 5.0)); } }; mediaBottomBar = HBoxBuilder.create() .id("bottom") .spacing(0) .alignment(Pos.CENTER) .children( ButtonBuilder.create() .id("back-button") .text("Back") .onAction(backAction) .build(), ButtonBuilder.create() .id("stop-button") .text("Stop") .onAction(stopAction) .build(), ButtonBuilder.create() .id("play-button") .text("Play") .onAction(playAction) .build(), ButtonBuilder.create() .id("pause-button") .text("Pause") .onAction(pauseAction) .build(), ButtonBuilder.create() .id("forward-button") .text("Forward") .onAction(forwardAction) .build() ) .build(); setBottom(mediaBottomBar); } protected void updateValues() { if (playTime != null &amp;&amp; timeSlider != null &amp;&amp; volumeSlider != null &amp;&amp; duration != null) { Platform.runLater(new Runnable() { public void run() { Duration currentTime = mp.getCurrentTime(); playTime.setText(formatTime(currentTime, duration)); timeSlider.setDisable(duration.isUnknown()); if (!timeSlider.isDisabled() &amp;&amp; duration. greaterThan(Duration.ZERO) &amp;&amp; !timeSlider.isValueChanging()) { timeSlider.setValue(currentTime.divide(duration).toMillis() * 100.0); } if (!volumeSlider.isValueChanging()) { volumeSlider.setValue((int) Math.round(mp.getVolume() * 100)); } } }); } } private static String formatTime(Duration elapsed, Duration duration) { int intElapsed = (int)Math.floor(elapsed.toSeconds()); int elapsedHours = intElapsed / (60 * 60); if (elapsedHours &gt; 0) { intElapsed -= elapsedHours * 60 * 60; } int elapsedMinutes = intElapsed / 60; int elapsedSeconds = intElapsed - elapsedHours * 60 * 60 - elapsedMinutes * 60; if (duration.greaterThan(Duration.ZERO)) { int intDuration = (int)Math.floor(duration.toSeconds()); int durationHours = intDuration / (60 * 60); if (durationHours &gt; 0) { intDuration -= durationHours * 60 * 60; } int durationMinutes = intDuration / 60; int durationSeconds = intDuration - durationHours * 60 * 60 - durationMinutes * 60; if (durationHours &gt; 0) { return String.format("%d:%02d:%02d", elapsedHours, elapsedMinutes, elapsedSeconds); } else { return String.format("%02d:%02d", elapsedMinutes, elapsedSeconds); } } else { if (elapsedHours &gt; 0) { return String.format("%d:%02d:%02d", elapsedHours, elapsedMinutes, elapsedSeconds); } else { return String.format("%02d:%02d", elapsedMinutes, elapsedSeconds); } } } } @Override public void start(Stage primaryStage) throws Exception { init(primaryStage); primaryStage.show(); play(); } public static void main(String[] args) { launch(args); } } </code></pre> <p>I'm sorry about the long code. Its just the samplecode from JavaFX. You find it <a href="http://docs.oracle.com/javafx/2/charts/line-chart.htm#CIHGBCFI" rel="nofollow">here</a>, and <a href="http://docs.oracle.com/javafx/2/swing/MediaPlayer.java.htm" rel="nofollow">here</a>.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload