打印到JavaFX窗口

时间:2016-12-07 06:21:32

标签: java javafx

如何从.txt文件中读取多行文本,选择包含某个字符串的文本,最后将它们作为只读文本添加到JavaFX窗口?

我知道如何选择包含特定字符串的行,但我不知道如何返回多行文本或如何向标签添加可变数量的文本

我需要成品显示如下:

String1a, String2a,...\n
String1b, String2b,...\n
-or-
String1a
String2a
\n
String1b
String2b
.
.
.

它目前显示的方式与第二个选项相同,但它全部是" null" 这是我的代码的一部分:

      bt4.setOnAction(
               new EventHandler<ActionEvent>() {
                  @Override public void handle(ActionEvent e) {
                     VBox vb3 = new VBox();
                     if(dent == true) {

                        a = dentist.selectAppt(id.getText());
                        for(int i=0; i< 4;i++) {
                           System.out.println(a[i]);
                        }
                        Label app = new Label(WORDS);
                        app.setWrapText(true);
                        vb3.getChildren().add(app);
                     }

                     if(dent == false) {
                     //TODO 
                     }
                     Stage stage = new Stage();

                     vb3.setSpacing(10);

                     BorderPane p = new BorderPane();
                     Pane p1= new Pane();
                     p1.getChildren().add(vb3);
                     p.getChildren().add(p1);

                     Scene scene2 = new Scene(p,500,500);
                     stage.setScene(scene2);
                     stage.show();
                  }
               });
   }

   private final String WORDS = 
        a[0]+"\n" +
        a[1]+"\n" +
        a[2]+"\n" +
        a[3]+"\n";



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


}

这是从.txt文件中选择String数组的方法:

   public String[] selectAppt(String s) {

       String[] sa = new String[4];

       try {
           Scanner scan = new Scanner(file2);
           while(scan.hasNextLine()) {
            String line = scan.nextLine();
            if(line.contains(s)) {
               //System.out.println(line);
               StringTokenizer st = new StringTokenizer(line,":");
               pid = st.nextToken();
               appt = st.nextToken();
               did = st.nextToken();
               pcode = st.nextToken();
               sa[0] = pid;
               sa[1] = appt;
               sa[2] = did;
               sa[3] = pcode;

               //System.out.println("\tPatient ID:\t"+pid);
               //System.out.println("\tTime and Date:\t"+appt);
               //System.out.println("\tDentist ID:\t"+did);
               //System.out.println("\tProcedure:\t"+pcode);
               //System.out.println();
            }

      }}
      catch (FileNotFoundException e) {
         System.out.println("error");
      }
    return sa;


   }

目前的方式是,只返回1行文字然后结束。我需要改变这一点,但我不确定如何。

1 个答案:

答案 0 :(得分:0)

FileFilter.java

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.TextArea;
import javafx.stage.Stage;

import java.io.IOException;
import java.nio.file.*;
import java.util.stream.Collectors;

public class FileFilter extends Application {
    public static final String SOURCE_FILE = "/tmp/christmas-eve.txt";
    public static final String SEARCH_STRING = "be";

    @Override
    public void start(Stage stage) throws IOException {
        String filteredText =
            Files.lines(Paths.get(SOURCE_FILE))
                .filter(line -> line.contains(SEARCH_STRING))
                .collect(Collectors.joining("\n"));

        TextArea textArea = new TextArea(filteredText);
        textArea.setEditable(false);

        stage.setScene(new Scene(textArea));
        stage.show();
    }

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

/tmp/christmas-eve.txt

'Twas the night before Christmas, when all through the house
Not a creature was stirring, not even a mouse;
The stockings were hung by the chimney with care
In hopes that St. Nicholas soon would be there;

The children were nestled all snug in their beds,
While visions of sugar-plums danced in their heads;
And mamma in her kerchief, and I in my cap,
Had just settled our brains for a long winter's nap,

When out on the lawn there arose such a clatter,
I sprang from the bed to see what was the matter.
Away to the window I flew like a flash,
Tore open the shutters and threw up the sash.

The moon on the breast of the new-fallen snow
Gave the lustre of mid-day to objects below,
When, what to my wondering eyes should appear,
But a miniature sleigh, and eight tiny reindeer,

With a little old driver, so lively and quick,
I knew in a moment it must be St. Nick.
More rapid than eagles his coursers they came,
And he whistled, and shouted, and called them by name:

"Now, _Dasher!_ now, _Dancer!_ now, _Prancer_ and _Vixen!_
On, _Comet!_ on, _Cupid!_ on, _Donder_ and _Blitzen!_
To the top of the porch! to the top of the wall!
Now dash away! dash away! dash away all!"