2016-12-07 1 views
0

Comment lire plusieurs lignes de texte à partir d'un fichier .txt, sélectionner celles qui contiennent une certaine chaîne et enfin les ajouter à une fenêtre JavaFX en tant que texte en lecture seule?Impression dans une fenêtre JavaFX

Je sais comment sélectionner les lignes qui contiennent une chaîne spécifique, mais je ne sais pas comment retourner plusieurs lignes de texte ou comment ajouter une quantité variable de texte à une étiquette

J'ai besoin du produit fini pour afficher comme ceci:

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

actuellement, il n'affiche comme la deuxième option, mais il est tout « nul » est ici une partie de mon code:

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


} 

Et ceci est la méthode qui sélectionne les tableaux de chaîne à partir d'un fichier txt:

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; 


    } 

La façon dont il est actuellement, il retourne à seulement 1 ligne de texte se termine alors. Je dois changer cela mais je ne sais pas comment.

Répondre

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!" 
+0

fonctionne parfaitement! Merci de votre aide. Probablement la réponse la plus festive que j'ai jamais reçue. – HooperDeuced