I'm new in the javafx and until now is going good. But I don't find any form to send a object from a screen to another. I have some dificult to understand funciont od annotations @FXML
and the method initialize
from Initializable
interface
Class that calls another
public class FluxoCaixaController extends ParametrosTelas implements iTelaPrincipalFX {
/*Atributos locais*/
private ObservableList<String> opcoes = FXCollections.observableArrayList("Receita", "Despesa");
private Object parent;
private AberturaDeTelasFX formaAbertura;
private ToggleGroup modalGroup = new ToggleGroup();
private Categoria categoria;
private CategoriaTreeViewController root = new CategoriaTreeViewController();
@Override
public void showScreen() {
formaAbertura = new AberturaDialogFX();
formaAbertura.loadFXML(bundle, icone, bundle.getString("screnn.fluxo.title"), new AnchorPane(), "FluxoCaixa.fxml");
}
@Override // This method is called by the FXMLLoader when initialization is complete
public void initialize(URL fxmlFileLocation, ResourceBundle resources) {
tipoField.getItems().setAll(opcoes);
root.setParentController(getInstance());
//idColumn.setCellValueFactory(new PropertyValueFactory<>("id"));
//descricaoColumn.setCellValueFactory(new PropertyValueFactory<>("descricao"));
//atualizaTabela();
}
@FXML
private void btnAddCetegoria() {
root.showScreen();
categoriaSubcategoriaField.setText(categoria.getDescricao());
}
private Object getInstance(){
return this;
}
Class called
public class CategoriaTreeViewController extends ParametrosTelas implements iTelaNormalFX {
private AberturaDeTelasFX formaAbertura;
private Object parent;
private CategoriaService categoriaService = new CategoriaService();
@FXML
private TreeView<Categoria> treeView;
private Categoria EmptyCategoria;
private TreeItem<Categoria> rootItem;
private EventHandler<MouseEvent> mouseEventHandle;
@Override
public void showScreen() {
formaAbertura = new AberturaDialogFX();
formaAbertura.loadFXML(bundle, icone, bundle.getString("screnn.subcategory.title"), new AnchorPane(), "CategoriaTreeView.fxml");
}
@Override // This method is called by the FXMLLoader when initialization is complete
public void initialize(URL fxmlFileLocation, ResourceBundle resources) {
initialiazeTree();
mouseEventHandle = (MouseEvent event) -> {
handleMouseClicked(event);
};
treeView.addEventHandler(MouseEvent.MOUSE_CLICKED, mouseEventHandle);
treeView.setRoot(rootItem);
}
private void initialiazeTree() {
EmptyCategoria = new Categoria();
EmptyCategoria.setDescricao("Categorias");
rootItem = new TreeItem<>(EmptyCategoria);
// private TreeItem<SubCategoria> itens = new TreeItem<>();
for (Categoria g : categoriaService.listaCategorias()) {
List<Categoria> subLst = categoriaService.listaSubCategoriasByCategoria(g.getId());
TreeItem<Categoria> itens = new TreeItem<>(g);
//ObservableList<Categoria> subData = FXCollections.observableArrayList(subLst);
for (Categoria s : subLst) {
s.setCategoria(g);
TreeItem<Categoria> subItem = new TreeItem<>(s);
//subItem.addEventHandler(MouseEvent, new EventHandler<MouseEvent>() {
itens.getChildren().add(subItem);
}
//itens.getChildren().add(itens);
rootItem.getChildren().add(itens);
}
}
private void handleMouseClicked(MouseEvent event) {
if (treeView.getSelectionModel().getSelectedItem() != null) {
Categoria name = (Categoria) ((TreeItem) treeView.getSelectionModel().getSelectedItem()).getValue();
FluxoCaixaController fluxo = (FluxoCaixaController) getParentController();
fluxo.setCategoria(name);
System.out.println("Node click: " + name.getDescricao());
formaAbertura.getStage().hide();
}
/*Node node = event.getPickResult().getIntersectedNode();
// Accept clicks only on node cells, and not on empty spaces of the TreeView
if (node instanceof Text || (node instanceof TreeCell && ((TreeCell) node).getText() != null)) {
String name = (String) ((TreeItem)treeView.getSelectionModel().getSelectedItem()).getValue();
System.out.println("Node click: " + name);
}*/
}
}
Must of the interfaces just point to on or two methods. The AberturaDeTelasFX interface has a Implementation that says how the screen should open.
AberturaNormalFX
public class AberturaNormalFX implements AberturaDeTelasFX {
private Stage stage;
@Override
public void loadFXML(ResourceBundle bundle, Image icon, String title, Node node, String fxmlPath) {
try {
// Carrega o root layout do arquivo fxml.
FXMLLoader loader = new FXMLLoader();
loader.setLocation(getClass()
.getResource(fxmlPath));
loader.setResources(bundle);
if (node instanceof BorderPane) {
BorderPane rootLayout = (BorderPane) loader.load();
showLayout(icon, title, rootLayout);
} else if (node instanceof AnchorPane) {
AnchorPane rootLayout = (AnchorPane) loader.load();
showLayout(icon, title, rootLayout);
}
} catch (IOException e) {
e.printStackTrace();
}
}
private void showLayout(Image icone, String title, Parent node) {
stage = new Stage();
Scene scene = new Scene(node);
stage.setTitle(title);
stage.getIcons().add(icone);
stage.setScene(scene);
stage.show();
}
@Override
public void loadFXML(String title, Node node, String fxmlPath) {
throw new UnsupportedOperationException("Not supported yet."); //To chan
ge body of generated methods, choose Tools | Templates. }
/**
* @return the stage
*/
@Override
public Stage getStage() {
return stage;
}
}