这是我的第一个java应用程序,它基于jfoenix组件。
我有一个带有JFXTreeView和JFXDrawer的splitpane。
我正在抽屉中加载课程,我想从这里访问treview。
我得到nullpointerException
。
main.java:
package gui.main;
import clases.avaluos.SIA;
import clases.io;
import com.jfoenix.controls.JFXDrawer;
import com.jfoenix.controls.JFXTreeView;
import datafx.ExtendedAnimatedFlowContainer;
import gui.main.avaluos.informacionGeneral.datosGenerales;
import io.datafx.controller.ViewController;
import io.datafx.controller.flow.Flow;
import io.datafx.controller.flow.FlowHandler;
import io.datafx.controller.flow.context.FXMLViewFlowContext;
import io.datafx.controller.flow.context.ViewFlowContext;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.TreeItem;
import javafx.scene.layout.AnchorPane;
import javafx.util.Duration;
import javax.annotation.PostConstruct;
import java.net.URL;
import java.util.*;
import static io.datafx.controller.flow.container.ContainerAnimations.SWIPE_LEFT;
@ViewController(value = "/fxml/main.fxml")
public class main implements Initializable {
@FXML
public JFXTreeView arbol; //My treeview
@FXMLViewFlowContext
private ViewFlowContext context;
@FXML
private JFXDrawer drawer;
@FXML
private AnchorPane contenido;
@Override
public void initialize(URL url, ResourceBundle rb) {
}
@PostConstruct
public void init() throws Exception {
TreeItem<String> menu = null;
TreeItem<String> rootItem;
TreeItem<String> root;
TreeItem<String> item;
//tree
root = new TreeItem<String>("SIA");
root.setExpanded(true);
rootItem = new TreeItem<String>("root");
root.getChildren().add(rootItem);
//Here I fill my tree
arbol.setRoot(root);
//arbol.setShowRoot(false);
/*arbol.requestFocus();
arbol.getSelectionModel().select(selected);
arbol.scrollTo(arbol.getRow(selected));*/
//CSS
/*arbol.getStyleClass().add("treeview");
arbol.setId("arbol");*/
//Load other class
Flow innerFlow = new Flow(second.class);
context = new ViewFlowContext();
final FlowHandler flowHandler = innerFlow.createHandler(context);
context.register("ContentFlowHandler", flowHandler);
context.register("ContentFlow", innerFlow);
final Duration containerAnimationDuration = Duration.millis(320);
drawer.setContent(flowHandler.start(new ExtendedAnimatedFlowContainer(containerAnimationDuration, SWIPE_LEFT)));
context.register("ContentPane", drawer.getContent().get(0));
}
}
main.fxml
<?xml version="1.0" encoding="UTF-8"?>
<?import com.jfoenix.controls.JFXDrawer?>
<?import com.jfoenix.controls.JFXTreeView?>
<?import javafx.scene.control.SplitPane?>
<?import javafx.scene.layout.*?>
<AnchorPane xmlns:fx="http://javafx.com/fxml/1" prefHeight="600.0" prefWidth="800.0"
xmlns="http://javafx.com/javafx/8.0.121">
<children>
<VBox prefHeight="600.0" prefWidth="800.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0"
AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
<children>
<SplitPane dividerPositions="0.29797979797979796" VBox.vgrow="ALWAYS">
<items>
<AnchorPane minHeight="100.0" minWidth="125.0" prefHeight="598.0" prefWidth="269.0"
SplitPane.resizableWithParent="false">
<children>
<JFXTreeView fx:id="arbol" prefHeight="598.0" prefWidth="300.0"
AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0"
AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0"/>
</children>
</AnchorPane>
<AnchorPane fx:id="contenido" minHeight="0.0" minWidth="0.0" prefHeight="160.0" prefWidth="100.0">
<children>
<JFXDrawer fx:id="drawer" direction="LEFT" prefHeight="598.0" prefWidth="556.0"
AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0"
AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0"/>
</children>
</AnchorPane>
</items>
</SplitPane>
</children>
</VBox>
</children>
</AnchorPane>
second.java:
package gui.main;
import clases.avaluos.SIA;
import com.jfoenix.controls.*;
import com.jfoenix.effects.JFXDepthManager;
import gui.main.verAvaluo;
import io.datafx.controller.ViewController;
import io.datafx.controller.flow.context.FXMLViewFlowContext;
import io.datafx.controller.flow.context.ViewFlowContext;
import javafx.application.Platform;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.Node;
import javafx.scene.control.Label;
import javafx.scene.control.ScrollPane;
import javafx.scene.layout.Priority;
import javafx.scene.layout.StackPane;
import javafx.scene.layout.VBox;
import javax.annotation.PostConstruct;
import java.io.IOException;
import java.net.URL;
import java.util.ArrayList;
import java.util.ResourceBundle;
@ViewController(value = "/fxml/second.fxml")
public class second implements Initializable {
@FXML
private JFXTreeView arbol;
@FXML
private main main;
@FXML
private ScrollPane scrollPane;
@FXMLViewFlowContext
private ViewFlowContext context;
@FXML
private JFXDrawer drawer;
@Override
public void initialize(URL url, ResourceBundle rb) {
//How to access to main treeview to control it.
}
/**
* init fxml when loaded.
*/
@PostConstruct
public void init() throws IOException {
//my componenets
}
}
多天后,我无法访问父级视图。
URL location = getClass().getResource("/fxml/main.fxml");
FXMLLoader fxmlLoader = new FXMLLoader(location);
AnchorPane root = null;
try {
root = (AnchorPane) fxmlLoader.load();
} catch (IOException ex) {
//
}
main = (main) fxmlLoader.getController();
main.arbol.visibleProperty().setValue(false);
这给了我nullpointerException
。
即使我把这段代码放在主类中:
public void getArbol(){
arbol.visibleProperty().setValue(false);
}
从第二个来电:
main.getArbol();
给我nullpointerexception。
就这样,让我访问。
main.java:
Flow innerFlow = new Flow(datosGenerales.class);
context = new ViewFlowContext();
context.register("arbol", arbol); //Adding this
final FlowHandler flowHandler = innerFlow.createHandler(context);
context.register("ContentFlowHandler", flowHandler);
context.register("ContentFlow", innerFlow);
final Duration containerAnimationDuration = Duration.millis(320);
drawer.setContent(flowHandler.start(new ExtendedAnimatedFlowContainer(containerAnimationDuration, SWIPE_LEFT)));
context.register("ContentPane", drawer.getContent().get(0));
和 second.java:
JFXTreeView arbol = (JFXTreeView) context.getRegisteredObject("arbol");
arbol.setVisible(false);
但是,如果我需要在主窗口或其他我没有加载类需要访问控制权的地方访问控件,会发生什么?
如何在我的所有窗口中访问组件?