I have tried to use JavaFX in a Swing application using JFXPanel as wrapper for Scene, but I was confronted by the following difficulties: all of the popups that have the ability to scroll over long lists - for example comboboxes - cannot be properly controlled.
My sample code:
import javafx.application.Platform;
import javafx.embed.swing.JFXPanel;
import javafx.scene.Scene;
import javafx.scene.control.ComboBox;
import javafx.scene.layout.HBox;
import javax.swing.*;
import java.awt.*;
public class SwingDialog extends JDialog {
public static void main(String[] args){
final SwingDialog dialog = new SwingDialog();
dialog.setVisible(true);
}
public SwingDialog(){
setDefaultCloseOperation(DISPOSE_ON_CLOSE);
setSize(new Dimension(250, 250));
final JComboBox combo = new JComboBox();
for (int i = 0; i< 101; i++){
combo.addItem("text" + i);
}
final JPanel panel = new JPanel();
panel.setLayout(new FlowLayout());
panel.setPreferredSize(new Dimension(100, 300));
panel.add(combo);
panel.add(getPanel());
final JScrollPane scroll = new JScrollPane(panel);
getContentPane().add(scroll);
}
private JFXPanel getPanel(){
final javafx.embed.swing.JFXPanel panel = new JFXPanel();
Platform.runLater(new Runnable() {
@Override
public void run() {
final HBox hbox = new HBox();
final ComboBox combo = new ComboBox();
for (int i = 0; i< 101; i++){
combo.getItems().add("text" + i);
}
hbox.getChildren().add(combo);
final Scene scene = new Scene(hbox);
panel.setScene(scene);
};
});
return panel;
}
}
There is the JDialog with JPanel inside the JScrollPane with two comboboxes: the first is a JComboBox and the second is a JavaFX ComboBox inside the JFXPanel. The JComboBox works properly, but the JavaFX ComboBox cannot scroll with mouse wheel. It seems that instead of scrolling itself, the mouse wheel scrolls by the JScrollPane with owner JPanel.
The actual version jdk-8-fcs-bin-b129-windows-x64-07_feb_2014 solves only the problem by mouse wheel scrolling inside the JavaFX ComboBox, but You can see again, that its popup (scrollpane) passes the bounds of the dialog and the worst is, that when You open popup of JavaFX ComboBox and scroll in the JScrollPane of the dialog, than the popup of JavaFX ComboBox begins to "fly".
It was my succeed several times (not stable) to close the dialog and the popup of JavaFX ComboBox was staying to "fly" without any dialog.
My sample code:
import javafx.application.Platform;
import javafx.embed.swing.JFXPanel;
import javafx.scene.Scene;
import javafx.scene.control.ComboBox;
import javafx.scene.layout.HBox;
import javax.swing.*;
import java.awt.*;
public class SwingDialog extends JDialog {
public static void main(String[] args){
final SwingDialog dialog = new SwingDialog();
dialog.setVisible(true);
}
public SwingDialog(){
setDefaultCloseOperation(DISPOSE_ON_CLOSE);
setSize(new Dimension(250, 250));
final JComboBox combo = new JComboBox();
for (int i = 0; i< 101; i++){
combo.addItem("text" + i);
}
final JPanel panel = new JPanel();
panel.setLayout(new FlowLayout());
panel.setPreferredSize(new Dimension(100, 300));
panel.add(combo);
panel.add(getPanel());
final JScrollPane scroll = new JScrollPane(panel);
getContentPane().add(scroll);
}
private JFXPanel getPanel(){
final javafx.embed.swing.JFXPanel panel = new JFXPanel();
Platform.runLater(new Runnable() {
@Override
public void run() {
final HBox hbox = new HBox();
final ComboBox combo = new ComboBox();
for (int i = 0; i< 101; i++){
combo.getItems().add("text" + i);
}
hbox.getChildren().add(combo);
final Scene scene = new Scene(hbox);
panel.setScene(scene);
};
});
return panel;
}
}
There is the JDialog with JPanel inside the JScrollPane with two comboboxes: the first is a JComboBox and the second is a JavaFX ComboBox inside the JFXPanel. The JComboBox works properly, but the JavaFX ComboBox cannot scroll with mouse wheel. It seems that instead of scrolling itself, the mouse wheel scrolls by the JScrollPane with owner JPanel.
The actual version jdk-8-fcs-bin-b129-windows-x64-07_feb_2014 solves only the problem by mouse wheel scrolling inside the JavaFX ComboBox, but You can see again, that its popup (scrollpane) passes the bounds of the dialog and the worst is, that when You open popup of JavaFX ComboBox and scroll in the JScrollPane of the dialog, than the popup of JavaFX ComboBox begins to "fly".
It was my succeed several times (not stable) to close the dialog and the popup of JavaFX ComboBox was staying to "fly" without any dialog.
- duplicates
-
JDK-8095046 [ComboBox] ComboBox Popup not maintaining its position wrt control when mouse scrolled
-
- Resolved
-