Я создаю приложение с богатым пользовательским интерфейсом и использую FXML в FXML, и каждая часть имеет отдельные контроллеры. Я решил следовать этому руководству и использовать свой fxml в качестве компонента. Поэтому я использую класс как контроллер и компонент. Кажется, все работает, но я упираюсь в стену, когда пытаюсь поместить методы в свой класс/контроллер. Я отредактировал свой FXML, чтобы не помещать в него контроллер напрямую. Но используя сам класс. вот где проблемная строка
<fx:root fx:id="profilePane" onMouseClicked="#select" onMouseEntered="#hoverOver" onMouseExited="#hoverOut" type="javafx.scene.layout.Pane" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1">...etc
каждый метод красный, говорящий, что Контроллер не определен в корневом компоненте, я знаю, что удалил эту часть из моего fxml. Но как это будет работать с учебником, которому я следую? Вот мой класс компонентов, с которым я работаю
imports etc..
public class FxmlProfilePanel extends Pane implements Initializable{
@FXML
private ToggleButton toggleButton;
@FXML
private Pane profilePane;
private FadeTransition fadeTransition=new FadeTransition(Duration.millis(500),toggleButton);
/**
* Initializes the controller class.
*/
@Override
public void initialize(URL url, ResourceBundle rb) {
// TODO
System.out.println("Is fx thread?:"+Platform.isFxApplicationThread());
}
public FxmlProfilePanel(){
FXMLLoader fxmlLoader=new FXMLLoader(getClass().getResource("/bitcompile/ecps/fxml/FxmlProfilePanel.fxml"));
fxmlLoader.setRoot(this);
fxmlLoader.setController(this);
try {
fxmlLoader.load();
} catch (IOException ex) {
Logger.getLogger(FxmlProfilePanel.class.getName()).log(Level.SEVERE, null, ex);
}
}
public void hoverOver(MouseEvent event){
Platform.runLater(() -> {
if(!toggleButton.isSelected()){
toggleButton.setText("Select profile");
fadeTransition.setFromValue(0);
fadeTransition.setToValue(1.0);
fadeTransition.play();
}else{
toggleButton.setText("Deselect profile");
fadeTransition.setFromValue(0);
fadeTransition.setToValue(1.0);
fadeTransition.play();
}
});
}
public void hoverOut(MouseEvent event){
Platform.runLater(() -> {
if(toggleButton.isSelected())
fadeTransition.setFromValue(1.0);
fadeTransition.setToValue(0);
fadeTransition.play();
});
}
public void selected(MouseEvent event){
if(!toggleButton.isSelected()){
toggleButton.setSelected(true);
toggleButton.setText("Profile Selected");
}else{
toggleButton.setSelected(false);
toggleButton.setText("Select profile");
}
}
}
Итак, вопрос: Когда я использую fxml/controller как компонент внутри другого fxml, который находится внутри другого fxml и т. д., как мне подключить свои методы из моего класса компонентов? (я знаю, как сделать все это после обычный стиль fxml с отдельным контроллером и т. д., но не таким образом). Я выбрал неправильный способ сделать это? Должен ли я рассмотреть другой подход? Спасибо за любое понимание.
полный fxml-код:
<fx:root fx:id="profilePane" onMouseClicked="#select" onMouseEntered="#hoverOver" onMouseExited="#hoverOut" type="javafx.scene.layout.Pane" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1">
<children><AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="384.0" prefWidth="270.0" style="-fx-border-color: #40464A;">
<children>
<StackPane prefHeight="430.0" prefWidth="270.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
<children>
<Pane prefWidth="270.0">
<children><ImageView fitHeight="288.0" fitWidth="270.0" layoutY="49.0" pickOnBounds="true" preserveRatio="true">
<image>
<Image url="@../resources/images/category/TechnologyTemp.png" />
</image>
<StackPane.margin>
<Insets top="45.0" />
</StackPane.margin>
<viewport>
<Rectangle2D />
</viewport></ImageView>
</children>
</Pane><BorderPane layoutX="79.0" layoutY="100.0" prefHeight="258.0" prefWidth="270.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
<top>
<Pane prefHeight="49.0" prefWidth="268.0" style="-fx-background-color: white;">
<children><VBox prefHeight="143.0" prefWidth="270.0" BorderPane.alignment="CENTER">
<children><Label text="Adobe">
<font>
<Font name="System Bold" size="15.0" />
</font>
<padding>
<Insets left="10.0" top="5.0" />
</padding>
</Label><Label text="Adobe Systems,inc">
<font>
<Font name="System Bold" size="14.0" />
</font>
<padding>
<Insets bottom="3.0" left="10.0" />
</padding>
</Label>
<Pane layoutX="-1.0" opacity="0.82" prefHeight="82.0" prefWidth="220.0">
<children><TextArea cache="true" editable="false" focusTraversable="false" mouseTransparent="true" pickOnBounds="false" prefHeight="82.0" prefWidth="268.0" style="-fx-background-radius: 0;" text="Oracle continually applies good corporate governance principles. The composition and activities of the company's Board of Directors, the approach to public disclosure" wrapText="true">
<VBox.margin>
<Insets top="53.0" />
</VBox.margin></TextArea>
</children>
</Pane>
</children></VBox>
</children>
</Pane>
</top>
<bottom>
<Pane prefHeight="45.0" prefWidth="270.0" style="-fx-background-color: white;">
<children><VBox prefHeight="55.0" prefWidth="270.0" BorderPane.alignment="CENTER">
<children><Label text="Technology">
<font>
<Font name="System Bold" size="14.0" />
</font>
<padding>
<Insets left="10.0" top="4.0" />
</padding>
</Label><Label text="Software services & provider">
<padding>
<Insets left="10.0" />
</padding>
<font>
<Font size="13.0" />
</font></Label>
</children></VBox>
</children>
</Pane>
</bottom></BorderPane>
</children>
</StackPane><Pane prefHeight="382.0" prefWidth="270.0">
<children><ToggleButton fx:id="toggleButton" layoutX="1.0" layoutY="191.0" mnemonicParsing="false" prefHeight="30.0" prefWidth="268.0" style="-fx-background-radius: 0; -fx-background-color: #57A1DE;" text="Select profile" textFill="WHITE">
<font>
<Font name="System Bold" size="14.0" />
</font></ToggleButton>
</children></Pane>
</children></AnchorPane>
</children>
</fx:root>
<fx:root ...>
корневым элементом вашего файла FMXL? Я думаю, что это единственное место, где это действительно. - person James_D   schedule 25.04.2014