2014-12-01 4 views
1

저는 JavaFX 및 JavaFX Scene Builder를 처음 사용하고 있으며 텍스트 파일에서 텍스트 파일을 읽고 텍스트 영역에 내용을 표시하는 방법을 연구하고 몇 주 동안 알아 내려고 노력했습니다. 컨트롤러 클래스에서 좋은 파일 읽기 기능이 있지만 fxml 문서의 텍스트 영역에 텍스트 파일을 표시하는 방법을 알 수 없습니다. 단추를 클릭하고 텍스트 영역에 파일을 표시하는 방법을 배웠지 만 GUI가로드되는 즉시 텍스트 영역에 내용을 표시하려고합니다. 누구든지이 일을하는 법을 알고 있다면 당신의 도움을 크게 받으실 수 있습니다!JavaFX에서 텍스트 파일을 읽고 텍스트 영역에 표시

마지막 버튼과 텍스트 영역 (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> 

답변

2
 @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); 
    } 
} 

또는 단순히 initialize()에서 버튼의 'on action'메서드를 호출 할 수 있습니다. 예.

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

} 

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

@Muskan에 감사드립니다. 실제로 myTextArea 옆에 배치 한 주석에 대해서는 생각하지 않았습니다. 그 변경 사항을 구현하고 텍스트 영역은 여전히 ​​내 텍스트 파일을 표시하지 않았다. 그래서 컨트롤러와 fxml 문서가 올바르게 통신하고 작동하는지 확인하기 위해 "hello world"를 추가했습니다. 그래서 파일 읽기 기능에 문제가 있습니까? 파일을 찾을 수 없다는 등의 오류는 발생하지 않습니다. – Maxwell

+1

파일 읽기 기능이 잘 작동합니다. .txt 파일에 유니 코드 형식의 문자가 포함되어 있고이 파일을 ANSI로 인코딩 된 텍스트 파일로 저장 했습니까? – Muskan

+1

아뇨, 유니 코드 였고 ANSI로 바뀌었고 정상적으로 작동했습니다. 고맙습니다! – Maxwell

1

그냥 당신이 initialize() 내부의 displayTextOnButtonClick 내부에 쓴 코드를 삽입

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 :

여기 내 코드입니다. 이것은 초기화가 의미하는 것입니다.

Initialize는 루트 노드를 처리 한 후 호출되며 추가 기능 (정의 된 경우)과 데이터를 fxml에있는 컨트롤에 추가합니다.

+1

감사합니다. @ ItachiUchiha는 매력처럼 작동했습니다! rtf 파일을 읽고 올바른 형식으로 내용을 표시하는 방법을 알고 있습니까? 자세한 내용은 여기 : http://stackoverflow.com/questions/28573275/how-to-read-rtf-file-and-display-in-javafx – Maxwell

관련 문제