2013-10-06 74 views
0

我得到一个问题,填充tableview的值。我试过JavaFX TableView dynamic column and data values 的解决方案,但这并不适合我。这是代码。如果有人知道如何解决这个问题,这将是非常好的。JavaFX TableView提供值

public class CSVTableViewer extends Application { 

private String allColumns; 
private FileSource fileSource; 
BufferedReader in; 
private String zeile; 
private String reihe; 
private String[]rowsArray; 
private String a=""; 
private String result2; 

ArrayList<String> c=new ArrayList<String>(); 
List<List<String>> data; 

/** 
* @return the zeile 
*/ 
public String getZeile() { 
    return zeile; 
} 



public void start(Stage stage) throws IOException { 
    TableView tableView = new TableView(); 
    Pattern p = Pattern.compile("[;\\/|\\t]"); 
    Pattern p2 = Pattern.compile("[\\s\\s]"); 
    //DataSourceReader dsr1 = new FileSource("your csv file path"); 
    String input = Dialogs.showInputDialog(stage, "Please enter path to csv:", "Input 
    Dialog", "Input Path"); 
    try{ 
    this.fileSource = new FileSource(input);  
    in = new BufferedReader(new FileReader(input)); 
    allColumns=in.readLine(); 
    }catch(IOException e){ 
     Dialogs.showErrorDialog(stage, "No CSV-File Found"); 
     System.exit(0); 
    } 


// Pattern p = Pattern.compile("[;\\.,/|.\\t]"); 
    String[] columnsArray=p.split(allColumns);// create array of column names you want to 
    display 
    for(int i=0;i<columnsArray.length;i++){ 
// a.add(columnsArray[i]); 

    } 

    CSVDataSource ds1 = new CSVDataSource(this.fileSource,columnsArray); 
    //TableView tableView = new TableView(); 
// tableView.setItems(ds1.getData()); 
    tableView.getColumns().addAll(ds1.getColumns()); 
    tableView.setColumnResizePolicy(TableView.CONSTRAINED_RESIZE_POLICY); 





    try { 
     while ((reihe = in.readLine()) != null) { 

      String[]row=p.split(reihe); 


      // System.out.println(als); 


     for (int i = 0; i < row.length; i++) { 
      String result=row[i].toString(); 
      c.add(result); 

     }} 




     }catch (IOException ex) { 
     Logger.getLogger(CSVTableViewer.class.getName()).log(Level.SEVERE, null, ex); 



     } 

     /* 
    ObservableList<ObservableList> csvData = FXCollections.observableArrayList(); 

    for(List<String> dataList : data) { 
    ObservableList<String> row = FXCollections.observableArrayList(); 
     for(String rowData : dataList) { 
     row.add(rowData); 
     } 
    /add each row to cvsData 
    } 

    tableView.setItems(csvData); 
    */ 
    /* Fills 'columns' and 'data' */ 





// zeile=in.readLine(); 
// System.out.println(zeile); 
// rowsArray=p.split(zeile); 





    //TableView tableView = new TableView(); 
    // tableView.setItems(musics); 

    //ObservableList musics = FXCollections.observableArrayList(); 
    //musics.add(a); 
    //CSVDataSource ds2 = new CSVDataSource(fileSource,rowsArray); 
    //TableView tableView = new TableView(); 
    //tableView.setItems(ds2.g); 



// } catch (IOException ex) { 
    // Logger.getLogger(CSVTableViewer.class.getName()).log(Level.SEVERE, null, ex); 
    // } 

    //tableView.getColumns().addAll(ds2.getData()); 


    //CSVDataSource ds2 = new CSVDataSource(fileSource,rowsArray); 
    //TableView tableView = new TableView(); 
    // tableView.setItems(musics); 
    //tableView.getColumns().addAll(ds1.getColumns()); 


    // ObservableList musics = FXCollections.observableArrayList(); 
    //musics.add(a); 
    // tableView.setItems(musics); 
    ObservableList<ObservableList> csvData = FXCollections.observableArrayList(); 


    ObservableList<String> row = FXCollections.observableArrayList(); 
    for(String rowData : c) { 
    row.add(rowData); 

    // add each row to cvsData 
    } 
    csvData.add(row); 
    tableView.setItems(csvData); 

    /* 
    for(List<String> dataList : data) { 
     ObservableList<String> row = FXCollections.observableArrayList(); 
     ObservableList<String> row = FXCollections.observableArrayList(); 
     for(String rowData : c) { 
      row.add(rowData); 

     } 
     tableView.setItems(row); 

    stage.setScene(SceneBuilder.create().root(tableView).build()); 
    stage.show(); 

    // stage.setScene(SceneBuilder.create().root(tableView).build()); 





    // stage.show(); 

    /* 
     stage.setTitle("Test App"); 
    // Just loading the file... 
     FileSource fs = new FileSource("artikel_demo.csv");  
    // Now creating my datasource 
    CSVDataSource dataSource = new CSVDataSource( 
      fs, "order-id", "order-item-id"); 
    @SuppressWarnings("rawtypes") 
    TableView table1 = new TableView(); 
    TableColumn<?, ?> orderCol = dataSource.getNamedColumn("order-id"); 
    TableColumn<?, ?> itemCol = dataSource.getNamedColumn("order-item-id");  
    table1.getColumns().addAll(orderCol, itemCol); 
    table1.setItems(dataSource.getData()); 
    stage.setScene(SceneBuilder.create().root(table1).build()); 
    stage.show(); 

    */ 
    //stage.setScene(SceneBuilder.create().root(tableView).build()); 
    //stage.show(); 


    // scene.getRoot().getChildren(); 

    stage.setScene(SceneBuilder.create().root(tableView).build()); 
    stage.show(); 
     } 




/** 
* The main() method is ignored in correctly deployed JavaFX application. 
* main() serves only as fallback in case the application can not be 
* launched through deployment artifacts, e.g., in IDEs with limited FX 
* support. NetBeans ignores main(). 
* 
* @param args the command line arguments 
*/ 
public static void main(String[] args) { 

    Application.launch(args); 
} 


} 

回答

1

在yourCarryObject类:

public ObservableList<String> getData() { 
    ObservableList<String> ostr = FXCollections.observableArrayList(); 

    ostr.clear(); 
    for (int i = 0; i < row.length; i++) { 
     String result=row[i].toString(); // if you are importing data from that row array 
     ostr.add(result); 
    } 
    return ostr; 
} 

后来

table.setItems(yourCarryObject.getData()); 
+0

THX一个lot..try这... – pulse

+0

到底什么是CarryObject类? – pulse

+0

该类的一个实例(对象)携带您的数据。 您可以根据需要在该类中编写方法,如:添加更多数据,删除数据,读取文件等。 然后getData()方法通过将数据转换为ObservableList来“准备”填充表的数据表接受。 对不起,迟到answear。 如果你愿意,我可以寄给你我的一些地址簿的完整代码,这些地址簿是我在此之前创建的,并且使用了这个逻辑。 – MarkoSerbia