Gabriel Luque
Gabriel Luque

Reputation: 117

Add a panel to a fxml pane

I am new to JavaFX and I am trying to do an app that will show several panels from the same class: The class PacienteGUI creates a panel, and I want to show 5 of this PacienteGUI panels in my main FXML, which has a panel itself. I´ve tried to add it through the controller by

@FXML Pane principal;

@Override
public void initialize(URL url, ResourceBundle rb) 
{
    PacienteGUI paciente = new PacienteGUI(1);
    principal.getChildren().add(paciente);
} 

Part of the PacienteGUI:

public class PacienteGUI extends javax.swing.JPanel {

public PacienteGUI(int num) {
    chairNum = num;
    initComponents();
}

private void initComponents() {
..
..
..Creates JPanel with all its components
..
}

The problem is that it says that PacientesGUI cannot be converted to node. How can I solve this??

Thanks

Upvotes: 0

Views: 984

Answers (1)

James_D
James_D

Reputation: 209674

Your Paciente class is a Swing JPanel, which cannot be placed in a JavaFX Pane directly.

You either need to make Paciente a subclass of a JavaFX Pane, or you need to wrap the Paciente instance in a SwingNode. The latter (SwingNode) is tricky, because you will need to use two different threads to create the different components: swing components need to be created and accessed on the AWT event dispatching thread, and JavaFX components need to be created on the FX Application Thread. I strongly recommend not mixing JavaFX and Swing if you can do so.

Upvotes: 1

Related Questions