I have demo application in which I am adding JAVAFX button in JPanel using JFXPanel and add scene to JFXPanel. but when I remove and add again JPanel in which my JFXPanel is implemented the scene is removed or hide inside JFXPanel please help.
I have tried to repain() and revalidate() JFXPanel and JPanel also but nothing happens.
Here is the whole code of the application please help.
public class MainForm extends JFrame {
JButton btnTest;
JButton button;
JLabel lblMessage;
JPanel mainPanel;
JFXPanel jfxPanel;
JPanel panel1;
JPanel panel2;
public MainForm() {
initialize();
}
private void initialize() {
setTitle("Touch Screen Test");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setExtendedState(JFrame.MAXIMIZED_BOTH);
mainPanel = new JPanel();
mainPanel.setLayout(new MigLayout("insets 10 10 10 10"));
panel1 = new JPanel();
panel1.setLayout(new MigLayout("insets 10 10 10 10"));
panel2 = new JPanel();
panel2.setLayout(new MigLayout("insets 10 10 10 10"));
MouseListener listener = new TestButtonListener();
btnTest = new JButton("Test");
btnTest.addMouseListener(listener);
jfxPanel = new JFXPanel();
jfxPanel.setToolTipText("JFX Panel");
jfxPanel.setScene(createScene());
jfxPanel.setPreferredSize(new Dimension(300, 50));
lblMessage = new JLabel("Your message Here.");
lblMessage.setFont(new Font("Arial", Font.PLAIN, 80));
panel1.add(btnTest, "h 50, w 300, push, c, wrap");
panel1.add(jfxPanel, "h 50, w 300, push, c, wrap");
panel1.add(lblMessage, "pushx, c");
button = new JButton("change");
button.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
if (button.getText().equals("change")) {
mainPanel.remove(panel1);
mainPanel.repaint();
mainPanel.revalidate();
button.setText("changed");
} else {
mainPanel.add(panel1, "push, grow, wrap, cell 0 0");
mainPanel.repaint();
mainPanel.revalidate();
button.setText("change");
}
}
});
panel2.add(button, "push, align c");
mainPanel.add(panel1, "push, grow, wrap, cell 0 0");
mainPanel.add(panel2, "pushx, growx, cell 0 1");
add(mainPanel);
setVisible(true);
setLocationRelativeTo(null);
}
class TestButtonListener implements MouseListener {
@Override
public void mouseClicked(MouseEvent arg0) {
}
@Override
public void mouseEntered(MouseEvent arg0) {
}
@Override
public void mouseExited(MouseEvent arg0) {
}
@Override
public void mousePressed(MouseEvent arg0) {
lblMessage.setText("Button pressed");
}
@Override
public void mouseReleased(MouseEvent arg0) {
lblMessage.setText("Button released");
}
}
public static void main(String[] args) {
new MainForm();
}
@SuppressWarnings("static-access")
private Scene createScene() {
VBox vBox = new VBox();
vBox.setAlignment(Pos.CENTER);
Scene scene = new Scene(vBox);
scene.getStylesheets().add(getClass().getResource("res/style.css").toString());
Button button = new Button("JAVAFX Button");
button.setPrefSize(300, 50);
button.setOnTouchPressed(new EventHandler<Event>() {
@Override
public void handle(Event event) {
lblMessage.setText("Button pressed");
}
});
button.setOnTouchReleased(new EventHandler<Event>() {
@Override
public void handle(Event event) {
lblMessage.setText("Button released");
}
});
vBox.getChildren().add(button);
return scene;
}
}
Finally I found the answer of this issue solution is available on this post
Prevent JavaFX thread from dying with JFXPanel Swing interop?