How to communicate between classes?

2

I'm starting in JavaFX and I'm having second thoughts.

The main class creates an HBox with a button. This button has the action of creating a TableView in the Center of bdPrincipal . Only they are in different classes / packages.

What is the best way to search for bdPrincipal to add addTabela() ?

BorderPane bpPrincipal;
public void start(Stage primaryStage) {
        bpPrincipal = new BorderPane();
        bpPrincipal.setLeft(Menu.addMenu());
        scPrincipal = new Scene(bpPrincipal);
        primaryStage.setScene(scPrincipal);
        primaryStage.centerOnScreen(); 
        primaryStage.setHeight(Screen.getPrimary().getVisualBounds().getHeight()); 
        primaryStage.setWidth(Screen.getPrimary().getVisualBounds().getWidth());        
        primaryStage.show();
    }    


public class Menu {
    public static HBox addMenu() {

        HBox hbox = new HBox();
        hbox.setPadding(new Insets(15, 12, 15, 12));
        hbox.setSpacing(20);
        hbox.setStyle("-fx-background-color: #336699;");

        Button btnListar = new Button("Listar");
        btnListar.setPrefSize(100, 20);
        btnListar.setOnAction(new EventHandler<ActionEvent>() {
            public void handle(ActionEvent evento) {
                  GestorFX.bpPrincipal.setCenter(addTabela());
            }
        });
        hbox.getChildren().addAll(btnListar);

        return hbox;
    }
    @SuppressWarnings({ "rawtypes", "unchecked" })
    private static TableView addTabela()
    {
        TableColumn colCodigo = new TableColumn();
        colCodigo.setText("Código");
        colCodigo.setMinWidth(150);
        colCodigo.setCellValueFactory(new PropertyValueFactory("clienteCodigo"));

        TableColumn colFantasia = new TableColumn();
        colFantasia.setText("Nome Fantasia");
        colFantasia.setMinWidth(450);
        colFantasia.setCellValueFactory(new PropertyValueFactory("clienteFantasia"));

        TableColumn colRazao = new TableColumn();
        colRazao.setText("Razão Social");
        colRazao.setMinWidth(450);
        colRazao.setCellValueFactory(new PropertyValueFactory("clienteRazao"));

        TableView tabela = new TableView();
        tabela.setItems(Negocio.Cliente.ListaCompleta());
        tabela.getColumns().addAll(colCodigo, colFantasia, colRazao);

        return tabela;
    }
}
    
asked by anonymous 09.05.2014 / 06:25

1 answer

2

You should basically import the package from the other class and create an object from it, so you can access your public methods.

Assuming your first code that has a BorderPane bpPrincipal; is in the following file:

  

src \ com \ package \ Paineis.java

Add the following import within your Menu class:

Menu.java

import com.pacote.Paineis;

You can already see the class, now create an object of the Panels class, and access the method you want from that class. Example:

Menu.java

Paineis paineis = new Paineis();
BorderPane bdPrincipal = paineis.getBpPrincipal(); //pronto

Now use bdPrincipal as desired, for example by adding it to the table.

Do not forget to create a method that returns the BorderPane of your Panels class:

Paineis.java

public BorderPane getBpPrincipal() {
        return bpPrincipal;
}
    
11.05.2014 / 15:44