Java Reference
In-Depth Information
@Override
public double minHeight(double width) {
return 200;
}
}
When the two JavaFX buttons are clicked, we invoke code that alters the state of the Swing component
MyRectangle
in a
Runnable
represented as a lambda expression through
EventQueue.invokeLater()
:
view.changeFillButton.setOnAction(actionEvent -> {
EventQueue.invokeLater(() -> {
final java.awt.Color fillColor = model.getFillColor();
if (fillColor.equals(java.awt.Color.LIGHT_GRAY)) {
model.setFillColor(java.awt.Color.GRAY);
} else {
model.setFillColor(java.awt.Color.LIGHT_GRAY);
}
view.canvas.repaint();
});
});
■
the
repaint()
method is actually one of the rare swing ui methods that can be called from any thread, not
just the swing edt. our use of
EventQueue.invokeLater()
is for illustration purposes only.
Note
When the mouse hovers over the
MyRectangle
, we update a JavaFX Label by updating a
StringProperty
named
mouseLocation
in our
Model
class that the
textProperty
of the
Label
binds to:
canvas.addMouseMotionListener(new MouseMotionListener() {
@Override
public void mouseDragged(MouseEvent e) {
}
@Override
public void mouseMoved(MouseEvent e) {
Platform.runLater(() -> {
model.setMouseLocation("(" + e.getX() + ", " + e.getY() + ")");
});
}
});
swingNode.setContent(canvas);
The complete Swing component in JavaFX example application is shown in Listing 7-15.
Listing 7-15.
SwingComponentInJavaFXExample.java
import javafx.application.Application;
import javafx.application.Platform;
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.property.StringProperty;
Search WWH ::
Custom Search