How to communicate between classes?

Asked

Viewed 716 times

3

I’m starting at Javafx and I’m having doubts.

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 the bdPrincipal to add to 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;
    }
}
  • Your title doesn’t match your question. Actually you want to know how to get the classes to communicate, right? Also, do you have doubts about creating Tableview? For example, if they were in the same class, you would know how to solve the problem?

  • Yes, perhaps my doubt can be dismembered. I will change the title.

  • Okay. I just didn’t understand one thing, you know so create Tableview?

  • Yes, I believe you know, but class communication is that I’m having a hard time.

1 answer

2


Basically you must import the package from the other class and create an object from it, so you will be able to access your public methods.

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

src with Paineis.java package

Add the following import within your Menu class:

Java menu.

import com.pacote.Paineis;

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

Java menu.

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 class Panels:

Paineis.java

public BorderPane getBpPrincipal() {
        return bpPrincipal;
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.