2016-01-10 39 views
0

嗯,我的问题很简单。 我正在用Java Fx构建应用程序,并且我有一个TabPane。 当我打开一个新的标签时,该标签通过一个fxml文件获取它的内容。

tab.setContent((Node)FXMLLoader.load(this.getClass().getResource("/main/textEditor.fxml")))
Java Fx,编辑特定选项卡的文本区域

精细,效果不错,它总是加载所有选项卡上的同一个文件,那么,所有选项卡上所有的textarea有相同的ID。 问题是,用java,我只能得到第一个tab的textarea的信息。

我怎样才能特别编辑一个标签的textarea?


的什么我想要做的一个例子:

主要

import javafx.application.Application; 
import javafx.fxml.FXMLLoader; 
import javafx.geometry.Rectangle2D; 
import javafx.scene.Parent; 
import javafx.scene.Scene; 
import javafx.stage.Screen; 
import javafx.stage.Stage; 


public class Main extends Application{ 


    public static void main(String[] args) { 
     Application.launch(Main.class, args); 
    } 

    @Override 
    public void start(Stage stage) throws Exception { 
     Parent root = FXMLLoader.load(getClass().getResource("/tabPane/test.fxml")); 


     Screen screen = Screen.getPrimary(); 
     Rectangle2D bounds = screen.getVisualBounds(); 

     stage.setScene(new Scene(root)); 



     stage.show(); 
    } 
} 

test.fxml

<?xml version="1.0" encoding="UTF-8"?> 

<?import java.lang.*?> 
<?import java.net.*?> 
<?import javafx.scene.control.*?> 
<?import javafx.scene.layout.*?> 
<?import javafx.scene.text.*?> 
<?import javafx.scene.layout.AnchorPane?> 

<VBox prefHeight="400.0" prefWidth="640.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="tabPane.controller"> 
    <children> 
     <MenuBar VBox.vgrow="NEVER"> 
     <menus> 
      <Menu mnemonicParsing="false" onAction="#test" text="File"> 
       <items> 
        <MenuItem fx:id="insert" mnemonicParsing="false" onAction="#test" text="Insert" /> 
       </items> 
      </Menu> 
     </menus> 
     </MenuBar> 
     <AnchorPane maxHeight="-1.0" maxWidth="-1.0" prefHeight="-1.0" prefWidth="-1.0" VBox.vgrow="ALWAYS"> 
     <children> 
      <Label alignment="CENTER" layoutX="155.0" layoutY="177.0" style="&#10;" text="Drag components from Library here…" textAlignment="CENTER" textFill="#9f9f9f" wrapText="false"> 
       <font> 
        <Font size="18.0" /> 
       </font> 
      </Label> 
      <TabPane fx:id="tabPane" prefHeight="375.0" prefWidth="640.0" tabClosingPolicy="UNAVAILABLE"> 
       <tabs> 
        <Tab text="Untitled Tab 1"> 
        <content> 
         <TextArea fx:id="textarea" prefHeight="200.0" prefWidth="200.0" text="a" /> 
        </content> 
        </Tab> 
        <Tab text="Untitled Tab 2"> 
        <content> 
         <TextArea fx:id="textarea1" prefHeight="200.0" prefWidth="200.0" text="a" /> 
        </content> 
        </Tab> 
       </tabs> 
      </TabPane> 
     </children> 
     </AnchorPane> 
    </children> 
    <stylesheets> 
     <URL value="@../../../../BasicApplicatio11n_css/BasicApplication.css" /> 

controller.java

import java.net.URL; 
import java.util.ResourceBundle; 

import javafx.event.ActionEvent; 
import javafx.fxml.FXML; 
import javafx.fxml.Initializable; 
import javafx.scene.control.TextArea; 

public class controller implements Initializable{ 

    @FXML 
    private TextArea textarea; 

    @Override 
    public void initialize(URL arg0, ResourceBundle arg1) { 


    } 
    public void test(ActionEvent event){ 

     textarea.appendText("Text"); 

    } 

} 

有两个选项卡在这个例子中,按下按钮时,我想补充当前所选选项卡上的文字。

+0

“问题是,用Java,我只能得到第一个选项卡的textarea的信息”。我不确定这是什么意思。你可以张贴一些代码来澄清问题是什么?你的'textEditor。fxml'文件有一个控制器?如果是这样,每个控制器实例将引用对应的UI中的节点,以便这是执行任何逻辑的地方。 –

+0

它有一个控制器,并且该按钮链接到上面的方法,问题是唯一改变的是第一个选项卡的textarea,而不是当前的。 – user3047498

+0

该按钮是textEditor.fxml文件的一部分吗? –

回答

0

有几种不同的方法可以做到这一点。一种方法是让标签内容的控制器从文本区域暴露textProperty。然后在你的“主控制器”中,创建一个StringProperty字段。创建新选项卡时,只需观察其selected属性并更新字符串属性字段以指向当前控制器中的字段。然后,您可以轻松地将文本加载到“当前”窗格中。

下面是一个简单的例子:

EditorController:

import javafx.beans.property.StringProperty; 
import javafx.fxml.FXML; 
import javafx.scene.control.TextArea; 

public class EditorController { 

    @FXML 
    private TextArea textArea ; 


    public StringProperty textProperty() { 
     return textArea.textProperty() ; 
    } 
} 

与textEditor.fxml:

<?xml version="1.0" encoding="UTF-8"?> 

<?import javafx.scene.layout.BorderPane?> 
<?import javafx.scene.control.TextArea?> 

<BorderPane xmlns:fx="http://javafx.com/fxml/1" fx:controller="EditorController"> 
    <center> 
     <TextArea fx:id="textArea"/> 
    </center> 
</BorderPane> 

然后是MainController可能看起来像:

import java.io.File; 
import java.io.IOException; 
import java.nio.file.Files; 
import java.nio.file.Path; 

import javafx.beans.property.StringProperty; 
import javafx.fxml.FXML; 
import javafx.fxml.FXMLLoader; 
import javafx.scene.control.Alert; 
import javafx.scene.control.Alert.AlertType; 
import javafx.scene.control.Tab; 
import javafx.scene.control.TabPane; 
import javafx.stage.FileChooser; 

public class MainController { 

    @FXML 
    private TabPane tabPane ; 

    private StringProperty currentText ; 

    public void initialize() throws IOException { 
     // load an initial tab: 
     newTab(); 
    } 

    @FXML 
    private void newTab() throws IOException { 
     Tab tab = new Tab("Untitled text"); 
     FXMLLoader loader = new FXMLLoader(getClass().getResource("textEditor.fxml")); 

     tab.setContent(loader.load()); 

     EditorController controller = loader.getController() ; 

     tab.selectedProperty().addListener((obs, wasSelected, isNowSelected) -> { 
      if (isNowSelected) { 
       currentText = controller.textProperty(); 
      } 
     }); 

     tabPane.getTabs().add(tab); 
     tabPane.getSelectionModel().select(tab); 
    } 

    @FXML 
    private void load() { 
     FileChooser chooser = new FileChooser(); 
     File file = chooser.showOpenDialog(tabPane.getScene().getWindow()); 
     if (file != null) { 
      Path path = file.toPath(); 
      try { 
       currentText.set(String.join("\n", Files.readAllLines(path))); 
      } catch (IOException e) { 
       Alert alert = new Alert(AlertType.ERROR); 
       alert.setContentText("Unable to load file "+path); 
       alert.setTitle("Load error"); 
       alert.showAndWait(); 
      } 
      tabPane.getSelectionModel().getSelectedItem().setText(path.getFileName().toString()); 
     } 
    } 
} 

与main.fxml:

<?xml version="1.0" encoding="UTF-8"?> 

<?import javafx.scene.layout.BorderPane?> 
<?import javafx.scene.layout.HBox?> 
<?import javafx.scene.control.TabPane?> 
<?import javafx.scene.control.Button?> 
<?import javafx.geometry.Insets?> 

<BorderPane xmlns:fx="http://javafx.com/fxml/1" fx:controller="MainController"> 
    <center> 
     <TabPane fx:id="tabPane"/> 
    </center> 
    <bottom> 
     <HBox alignment="CENTER" spacing="5"> 
      <padding> 
       <Insets top="5" right="5" left="5" bottom="5" /> 
      </padding> 
      <Button text="Load..." onAction="#load" /> 
      <Button text="New" onAction="#newTab"/> 
     </HBox> 
    </bottom> 
</BorderPane> 

为了完整,简单的应用程序类:

import java.io.IOException; 

import javafx.application.Application; 
import javafx.fxml.FXMLLoader; 
import javafx.scene.Parent; 
import javafx.scene.Scene; 
import javafx.stage.Stage; 

public class Main extends Application { 

    @Override 
    public void start(Stage primaryStage) throws IOException { 
     Parent root = FXMLLoader.load(getClass().getResource("main.fxml")); 
     Scene scene = new Scene(root, 800, 800); 
     primaryStage.setScene(scene); 
     primaryStage.show(); 
    } 

    public static void main(String[] args) { 
     launch(args); 
    } 
} 

还有其它的方法,例如你可以改变按钮的onAction属性当选项卡选择更改等

+0

我改编了一段代码到我的程序中,它的作用就像一个魅力。太好了,谢谢。 – user3047498

相关问题