Maxwell
Maxwell

Reputation: 85

JavaFX read from text file and display in textarea

I am new to JavaFX and JavaFX Scene Builder and have been researching and trying to figure out for weeks now how to simply read from a text file and display its contents in a textarea. I have a good file reading function in my controller class, but I can't figure out how to display the text file to the text area in the fxml document. I've learned how to click a button and make the file display in the text area, but I want to have the contents in the text area as soon as the GUI loads. If anyone has an idea how to go about doing this, your help will be greatly appreciated!

The last button and text area (towards the end of the FXML document) is the button that prints my text file to netbeans, and the text area in which I would like the text to be dispalyed.

Here is my code:

Controller

import java.io.File;
import java.io.FileNotFoundException;
import java.net.URL;
import java.util.ResourceBundle;
import java.util.Scanner;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;


public class Screen2Controller implements Initializable , ControlledScreen {

    ScreensController myController;

    @Override
    public void initialize(URL url, ResourceBundle rb) {
        // TODO
    }

    public void setScreenParent(ScreensController screenParent){
        myController = screenParent;
    }

    @FXML
    private void goToScreen1(ActionEvent event){
       myController.setScreen(ScreensFramework.screen1ID);
    }

    @FXML
    private void goToProgram(ActionEvent event){
       myController.setScreen(ScreensFramework.programID);
    }

    private void displayText() throws FileNotFoundException {
        Scanner s = new Scanner (new File ("EECS.txt")).useDelimiter("\\s+");
        while (s.hasNext()) {
            if (s.hasNextInt()) { // check if next token is an int
                System.out.print(s.nextInt()+" "); // display the found integer
            }       
            else {
                System.out.print(s.next()+" "); // else read the next token
            }
        } //end while
    } //end main   
} //end screen2controller

FXML

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

<?import javafx.scene.image.*?>
<?import java.lang.*?>
<?import java.util.*?>
<?import javafx.scene.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.text.*?>

<AnchorPane id="AnchorPane" prefHeight="650.0" prefWidth="1350.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="departmentdisplay.Screen2Controller">
  <children>
      <ImageView fitHeight="783.0" fitWidth="1398.0" layoutX="-4.0" layoutY="-80.0" pickOnBounds="true" preserveRatio="true">
         <image>
            <Image url="@nexus_blue.jpg" />
         </image>
      </ImageView>
      <ImageView fitHeight="149.0" fitWidth="1322.0" layoutX="20.0" layoutY="7.0" pickOnBounds="true" preserveRatio="true">
         <image>
            <Image url="@departinfohead.png" />
         </image>
      </ImageView>
  <Button layoutX="49.0" layoutY="26.0" mnemonicParsing="false" onAction="#goToScreen1" prefHeight="111.0" prefWidth="121.0">
         <graphic>
            <ImageView fitHeight="100.0" fitWidth="150.0" pickOnBounds="true" preserveRatio="true">
               <image>
                  <Image url="@backarrow.png" />
               </image>
            </ImageView>
         </graphic></Button>
      <SplitPane dividerPositions="0.20765027322404372" layoutX="928.0" layoutY="168.0" orientation="VERTICAL" prefHeight="185.0" prefWidth="415.0">
         <items>
            <AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="95.0" prefWidth="183.0">
               <children>
                  <Label layoutX="14.0" layoutY="23.0" prefHeight="96.0" prefWidth="158.0" text="                      Map" textAlignment="CENTER" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
                     <font>
                        <Font name="Arial Black" size="24.0" />
                     </font>
                  </Label>
               </children>
            </AnchorPane>
            <AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="176.0" prefWidth="158.0">
               <children>
                  <TextArea prefHeight="151.0" prefWidth="242.0" text="Image" wrapText="true" AnchorPane.bottomAnchor="-7.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">       
                  </TextArea>
               </children>
            </AnchorPane>
         </items>
      </SplitPane>
      <SplitPane dividerPositions="0.08057851239669421" layoutX="20.0" layoutY="168.0" orientation="VERTICAL" prefHeight="480.0" prefWidth="898.0">
         <items>
            <AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="100.0" prefWidth="160.0">
               <children>
                  <Label layoutX="369.0" prefHeight="29.0" prefWidth="1062.0" text="                  Electrical Engineering &amp; Computer Science" textAlignment="CENTER" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
                     <font>
                        <Font name="Arial Black" size="24.0" />
                     </font>
                  </Label>
               </children>
            </AnchorPane>
            <AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="100.0" prefWidth="160.0">
               <children>
                  <TextArea fx:id="dinfoTextArea" layoutY="3.0" prefHeight="453.0" prefWidth="896.0" 
                 text="Text file text goes here" wrapText="true"
                  AnchorPane.bottomAnchor="-14.0" AnchorPane.leftAnchor="0.0"
                   AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="3.0">
                     </TextArea>
               </children>
            </AnchorPane>
         </items>
      </SplitPane>
      <SplitPane dividerPositions="0.13286713286713286" layoutX="930.0" layoutY="365.0" orientation="VERTICAL" prefHeight="282.0" prefWidth="413.0">
         <items>
            <AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="50.0" prefWidth="221.0">
               <children>
                  <Label layoutX="92.0" layoutY="12.0" prefHeight="29.0" prefWidth="263.0" text="                  Programs" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
                     <font>
                        <Font name="Arial Black" size="24.0" />
                     </font>
                  </Label>
              <Button layoutX="25.0" mnemonicParsing="false" onAction="#displayText" prefHeight="34.0" prefWidth="102.0" text="Go To Programs" />
               </children>
            </AnchorPane>
            <AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="176.0" prefWidth="158.0">
               <children>
                  <TextArea layoutX="57.0" layoutY="-6.0" prefHeight="339.0" prefWidth="240.0"
                      text="Lorem ipsum " wrapText="true" AnchorPane.bottomAnchor="0.0"
                      AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" 
                      AnchorPane.topAnchor="0.0"> 
                  </TextArea>
               </children>
            </AnchorPane>
         </items>
      </SplitPane>
  </children>
</AnchorPane>

Upvotes: 2

Views: 29062

Answers (2)

ItachiUchiha
ItachiUchiha

Reputation: 36722

Just put the code you wrote inside the displayTextOnButtonClick inside the initialize(). This is what initialize is meant for.

Initialize is called after processing the root node and it adds extra functionality(if defined) and data to the controls present in the fxml.

Upvotes: 1

Muskan
Muskan

Reputation: 304

      @FXML TextArea YourTextArea; //i think you already know about this

@Override
public void initialize(URL url, ResourceBundle rb) {
    try {
        Scanner s = new Scanner(new File("EECS.txt")).useDelimiter("\\s+");
        while (s.hasNext()) {
            if (s.hasNextInt()) { // check if next token is an int
                YourTextArea.appendText(s.nextInt() + " "); // display the found integer
            } else {
               YourTextArea.appendText(s.next() + " "); // else read the next token
            }
        }
    } catch (FileNotFoundException ex) {
        System.err.println(ex);
    }
}

or you can simply call your button's 'on action' method from initialize(). eg.

    @FXML public void displayTextOnButtonClick(){ //suppose this method gets fired when you click button

}

@FXML public void initialize(URL url, ResourceBundle rb) {
   displayTextOnButtonClick();
} 

Upvotes: 4

Related Questions