如何从解析的文本文件传递数据并将值插入javafx中的ObservableList? [英] how to pass data from parsed text file and insert the values to ObservableList in javafx?

查看:4597
本文介绍了如何从解析的文本文件传递数据并将值插入javafx中的ObservableList?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

文字档内容

  gr.spinellis.ckjm.ClassVisitor 13 2 0 14 74 34 2 9 

gr.spinellis.ckjm.ClassMetricsContainer 3 1 0 3 18 0 2 2

gr.spinellis.ckjm.MetricsFilter 7 1 0 6 30 11 2 5

gr.spinellis.ckjm.PrintPlainResults 2 1 0 2 8 0 1 2

gr.spinellis.ckjm.MethodVisitor 11 2 0 21 40 0 1 8

gr.spinellis。 ckjm.CkjmOutputHandler 1 1 0 1 1 0 3 1

我解析文本文件,以有组织的方式查看值,



解析运行良好,现在我想把从文本文件中获得的值传递给TableView。



这是我的代码

  
import java.io.BufferedReader;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;
import javafx.application.Application;
import javafx.beans.property.SimpleDoubleProperty;
import javafx.beans.property.SimpleIntegerProperty;
import javafx.beans.property.SimpleStringProperty;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.Event;
import javafx.event.EventHandler;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.chart.PieChart;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.TableView.TableViewSelectionModel;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

public class show_TableView extends应用程序{

public TableView<指标> tableView = new TableView< Metrics>();
private ObservableList<指标> dataList = FXCollections.observableArrayList(

// /做这样的新Metrics(名称,WMC,DIT,NOC,CBO,RFC,LCOM,Ce,NPM),
// i希望添加度量标准ADDED DYNAMICALLY之后的文件被分析

);

ObservableList< PieChart.Data> pieChartData = FXCollections
.observableArrayList(
new PieChart.Data(CBO,m.getCBO()),
new PieChart.Data(Ce,m.getCe
new PieChart.Data(DIT,m.getDIT()),
new PieChart.Data(LCOM,m.getLCOM()),
new PieChart.Data NOC,m.getNOC()),
new PieChart.Data(NPM,m.getNPM()),
new PieChart.Data(WMC,m.getWMC ;
c.setData(pieChartData);







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

@Override
public void start(Stage primaryStage){

primaryStage.setTitle(java-buddy.blogspot.com);
Group root = new Group();

TableColumn name = new TableColumn(name);
name.setCellValueFactory(new PropertyValueFactory< Metrics,String>(
name));

TableColumn WMCCol = new TableColumn(WMC);
WMCCol.setCellValueFactory(new PropertyValueFactory< Metrics,Double>(
WMC));

TableColumn DITCol = new TableColumn(DIT);
DITCol.setCellValueFactory(new PropertyValueFactory< Metrics,Double>(
DIT));

TableColumn NOCCol = new TableColumn(NOC);
NOCCol.setCellValueFactory(new PropertyValueFactory< Metrics,Double>(
NOC));
TableColumn CBOCol = new TableColumn(CBO);
CBOCol.setCellValueFactory(new PropertyValueFactory< Metrics,Double>(
CBO));
TableColumn RFCCol = new TableColumn(RFC);
RFCCol.setCellValueFactory(new PropertyValueFactory< Metrics,Double>(
RFC));
TableColumn LCOMCol = new TableColumn(LCOM);
LCOMCol.setCellValueFactory(new PropertyValueFactory< Metrics,Double>(
LCOM));
TableColumn ceCol = new TableColumn(Ca);
ceCol.setCellValueFactory(new PropertyValueFactory< Metrics,Double>(
ce));
TableColumn NPMCol = new TableColumn(NPM);
NPMCol.setCellValueFactory(new PropertyValueFactory< Metrics,Double>(
NPM));

primaryStage.setScene(new Scene(root,200,200));
primaryStage.show();

List< Metrics> metric = readMetricFromCSV(C:\\Users\\pavi\\Desktop\\11.txt);
//让我们打印从CSV文件读取的所有指标
for(Metrics m:metric){
System.out.println(m);

}

tableView.setItems(FXCollections.observableArrayList(metric));
tableView.getColumns()。addAll(name,WMCCol,DITCol,NOCCol,CBOCol,
RFCCol,LCOMCol,ceCol,NPMCol);

VBox vBox = new VBox();
vBox.setMaxSize(600,600);
vBox.setSpacing(10);
PieChart c = new PieChart();
tableView.setOnMouseClicked(new EventHandler< Event>(){

@Override
public void handle(Event event){
c.setData(FXCollections.emptyObservableList ));
TableViewSelectionModel< Metrics> tableViewSelectionModel = tableView
.selectionModelProperty()。get();
Metrics m = tableViewSelectionModel.getSelectedItem();
if(m == null ){
return;
}
ObservableList< PieChart.Data> pieChartData = FXCollections
.observableArrayList(
new PieChart.Data(CBO,m.getCBO )),
new PieChart.Data(Ce,m.getCe()),
new PieChart.Data(DIT,m.getDIT()),
new PieChart。数据(LCOM,m.getLCOM()),
new PieChart.Data(NOC,m.getNOC()),
new PieChart.Data(NPM,m.getNPM )),
new PieChart.Data(WMC,m.getWMC()));
c.setData(pieChartData);
}
});
vBox.getChildren()。add(tableView);
vBox.getChildren()。add(c);

root.getChildren()。add(vBox);

}

public List< Metrics> readMetricFromCSV(String fileName){

List< Metrics> metricsss = new ArrayList<>();

路径pathToFile = Paths.get(fileName);
//创建一个BufferedReader的实例
//使用try with resource,Java 7功能来关闭资源
try(BufferedReader br = Files.newBufferedReader(pathToFile,
StandardCharsets.US_ASCII )){
//从文本文件中读取第一行
String line = br.readLine();
while(line!= null&&!line.isEmpty()){//循环直到所有行
//读取
String [] attributes = line.split ); //文件,使用
//逗号作为
//分隔符
Metrics valueOfMetric = createMetric(attributes);
metricsss.add(valueOfMetric); //将度量添加到ArrayList中
//跳过空行
// line.isEmpty()|| line.trim()。equals()||
// line.trim()。equals(\\\
))
br.readLine();
line = br.readLine();
}

} catch(IOException ioe){
ioe.printStackTrace();
}

return metricsss;
}
private Metrics createMetric(String [] metadata){
String name = metadata [0];
int WMC = Integer.parseInt(metadata [1]);
int DIT = Integer.parseInt(metadata [2]);
int NOC = Integer.parseInt(metadata [3]);
int CBO = Integer.parseInt(metadata [4]);
int RFC = Integer.parseInt(metadata [5]);
int LCOM = Integer.parseInt(metadata [6]);
int Ce = Integer.parseInt(metadata [7]);
int NPM = Integer.parseInt(metadata [8]);
return new Metrics(name,WMC,DIT,NOC,CBO,RFC,LCOM,Ce,NPM); //,cc
}


public class指标{

私有字符串名称;
private int WMC;
private int DIT;
private int NOC;
private int CBO;
private int RFC;
private int LCOM;
private int Ce;
private int NPM;

public Metrics(String name,int WMC,int DIT,int NOC,int CBO,int RFC,int LCOM,int Ce,int NPM){

this.name = name;
this.WMC = WMC;
this.DIT = DIT;
this.NOC = NOC;
this.CBO = CBO;
this.RFC = RFC;
this.LCOM = LCOM;
this.Ce = Ce;
this.NPM = NPM;
}

public String getName(){
return name;
}

public void setName(String name){
this.name = name;
}

public int getWMC(){
return WMC;
}

public void setWMC(int WMC){
this.WMC = WMC;
}

public int getDIT(){
return DIT;
}

public void setDIT(int DIT){
this.DIT = DIT;
}

public int getNOC(){
return NOC;
}

public void setNOC(int NOC){
this.NOC = NOC;
}

public int getCBO(){
return CBO;
}

public void setCBO(int CBO){
this.CBO = CBO;
}

public int getRFC(){
return RFC;
}

public void setRFC(int RFC){
this.RFC = RFC;
}

public int getLCOM(){
return LCOM;
}

public void setLCOM(int LCOM){
this.LCOM = LCOM;
}

public int getCe(){
return Ce;
}

public void setCe(int ce){
Ce = ce;
}

public int getNPM(){
return NPM;
}

public void setNPM(int NPM){
this.NPM = NPM;
}


}




}


解决方案

  tableView.setItems(FXCollections.observableArrayList(metric)); // added 

我添加了上面的代码行以使用列表创建observaleList

  br.readLine(); //这里读取extraline 
String line1 = null;
//从文本文件中读取第一行
String line = br.readLine();
while(line!= null){

上面的条件是worng如果行为空?我也改变了这一点(对不起,我不知道我不知道这么多英语)

  @覆盖
public void start(Stage primaryStage){

primaryStage.setTitle(java-buddy.blogspot.com);
Group root = new Group();

TableColumn name = new TableColumn(name);
name.setCellValueFactory(new PropertyValueFactory< Metrics,String>(
name));

TableColumn WMCCol = new TableColumn(WMC);
WMCCol.setCellValueFactory(new PropertyValueFactory< Metrics,Double>(
WMC));

TableColumn DITCol = new TableColumn(DIT);
DITCol.setCellValueFactory(new PropertyValueFactory< Metrics,Double>(
DIT));

TableColumn NOCCol = new TableColumn(NOC);
NOCCol.setCellValueFactory(new PropertyValueFactory< Metrics,Double>(
NOC));
TableColumn CBOCol = new TableColumn(CBO);
CBOCol.setCellValueFactory(new PropertyValueFactory< Metrics,Double>(
CBO));
TableColumn RFCCol = new TableColumn(RFC);
RFCCol.setCellValueFactory(new PropertyValueFactory< Metrics,Double>(
RFC));
TableColumn LCOMCol = new TableColumn(LCOM);
LCOMCol.setCellValueFactory(new PropertyValueFactory< Metrics,Double>(
LCOM));
TableColumn ceCol = new TableColumn(Ca);
ceCol.setCellValueFactory(new PropertyValueFactory< Metrics,Double>(
ce));
TableColumn NPMCol = new TableColumn(NPM);
NPMCol.setCellValueFactory(new PropertyValueFactory< Metrics,Double>(
NPM));

primaryStage.setScene(new Scene(root,200,200));
primaryStage.show();

List< Metrics> metric = readMetricFromCSV(C:\\Users\\pavi\\Desktop\\11.txt);
//让我们打印从CSV文件读取的所有指标
for(Metrics m:metric){
System.out.println(m);

}

tableView.setItems(FXCollections.observableArrayList(metric));
tableView.getColumns()。addAll(name,WMCCol,DITCol,NOCCol,CBOCol,
RFCCol,LCOMCol,ceCol,NPMCol);

VBox vBox = new VBox();
vBox.setMaxSize(600,600);
vBox.setSpacing(10);
PieChart c = new PieChart();
tableView.setOnMouseClicked(new EventHandler< Event>(){

@Override
public void handle(Event event){
c.setData(FXCollections.emptyObservableList ));
TableViewSelectionModel< Metrics> tableViewSelectionModel = tableView
.selectionModelProperty()。get();
Metrics m = tableViewSelectionModel.getSelectedItem();
if(m == null ){
return;
}
ObservableList< PieChart.Data> pieChartData = FXCollections
.observableArrayList(
new PieChart.Data(CBO,m.getCBO )),
new PieChart.Data(Ce,m.getCe()),
new PieChart.Data(DIT,m.getDIT()),
new PieChart。数据(LCOM,m.getLCOM()),
new PieChart.Data(NOC,m.getNOC()),
new PieChart.Data(NPM,m.getNPM )),
new PieChart.Data(WMC,m.getWMC()));
c.setData(pieChartData);
}
});
vBox.getChildren()。add(tableView);
vBox.getChildren()。add(c);

root.getChildren()。add(vBox);

}

public List< Metrics> readMetricFromCSV(String fileName){

List< Metrics> metricsss = new ArrayList<>();

路径pathToFile = Paths.get(fileName);
//创建一个BufferedReader的实例
//使用try with resource,Java 7功能来关闭资源
try(BufferedReader br = Files.newBufferedReader(pathToFile,
StandardCharsets.US_ASCII )){
//从文本文件中读取第一行
String line = br.readLine();
while(line!= null&&!line.isEmpty()){//循环直到所有行
//读取
String [] attributes = line.split ); //文件,使用
//逗号作为
//分隔符
Metrics valueOfMetric = createMetric(attributes);
metricsss.add(valueOfMetric); //将度量添加到ArrayList中
//跳过空行
// line.isEmpty()|| line.trim()。equals()||
// line.trim()。equals(\\\
))
br.readLine();
line = br.readLine();
}

} catch(IOException ioe){
ioe.printStackTrace();
}

return metricsss;
}

对于图表,我们需要将数据设置为pieChart.setData(pieChartData);

  ObservableList< PieChart.Data> pieChartData = FXCollections 
.observableArrayList(
new PieChart.Data(CBO,m.getCBO()),
new PieChart.Data(Ce,m.getCe
new PieChart.Data(DIT,m.getDIT()),
new PieChart.Data(LCOM,m.getLCOM()),
new PieChart.Data NOC,m.getNOC()),
new PieChart.Data(NPM,m.getNPM()),
new PieChart.Data(WMC,m.getWMC ;
c.setData(pieChartData);


content of text file

    gr.spinellis.ckjm.ClassVisitor 13 2 0 14 74 34 2 9

    gr.spinellis.ckjm.ClassMetricsContainer 3 1 0 3 18 0 2 2

    gr.spinellis.ckjm.MetricsFilter 7 1 0 6 30 11 2 5

    gr.spinellis.ckjm.PrintPlainResults 2 1 0 2 8 0 1 2

    gr.spinellis.ckjm.MethodVisitor 11 2 0 21 40 0 1 8

    gr.spinellis.ckjm.CkjmOutputHandler 1 1 0 1 1 0 3 1

i am parsing text file and then i want to view the values in organized way ,

the parsing works well , now i want to put the values i get from text file to TableView .

this is my code

package show;
   import java.io.BufferedReader;
   import java.io.IOException;
   import java.nio.charset.StandardCharsets;
   import java.nio.file.Files;
   import java.nio.file.Path;
   import java.nio.file.Paths;
   import java.util.ArrayList;
   import java.util.List;
   import javafx.application.Application;
   import javafx.beans.property.SimpleDoubleProperty;
   import javafx.beans.property.SimpleIntegerProperty;
   import javafx.beans.property.SimpleStringProperty;
   import javafx.collections.FXCollections;
   import javafx.collections.ObservableList;
   import javafx.event.Event;
   import javafx.event.EventHandler;
   import javafx.scene.Group;
   import javafx.scene.Scene;
   import javafx.scene.chart.PieChart;
   import javafx.scene.control.TableColumn;
   import javafx.scene.control.TableView;
   import javafx.scene.control.TableView.TableViewSelectionModel;
   import javafx.scene.control.cell.PropertyValueFactory;
   import javafx.scene.layout.VBox;
   import javafx.stage.Stage;

       public class show_TableView extends Application {

        public TableView < Metrics > tableView = new TableView<Metrics>();
       private ObservableList< Metrics > dataList =   FXCollections.observableArrayList(

       // /do something like this  new Metrics(name,WMC,DIT,NOC ,CBO,RFC,LCOM , Ce, NPM),
       // i want to add Metrics ADDED DYNAMICALLY after file is parsed

       );

         ObservableList<PieChart.Data> pieChartData = FXCollections
                       .observableArrayList(
                               new PieChart.Data("CBO", m.getCBO()),
                               new PieChart.Data("Ce", m.getCe()),
                               new PieChart.Data("DIT", m.getDIT()),
                               new PieChart.Data("LCOM", m.getLCOM()),
                               new PieChart.Data("NOC", m.getNOC()),
                               new PieChart.Data("NPM", m.getNPM()),
                               new PieChart.Data("WMC", m.getWMC()));
               c.setData(pieChartData);







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

    @Override
   public void start(Stage primaryStage) {

       primaryStage.setTitle("java-buddy.blogspot.com");
       Group root = new Group();

       TableColumn name = new TableColumn("name");
       name.setCellValueFactory(new PropertyValueFactory<Metrics, String>(
               "name"));

       TableColumn WMCCol = new TableColumn("WMC");
       WMCCol.setCellValueFactory(new PropertyValueFactory<Metrics, Double>(
               "WMC"));

       TableColumn DITCol = new TableColumn("DIT");
       DITCol.setCellValueFactory(new PropertyValueFactory<Metrics, Double>(
               "DIT"));

       TableColumn NOCCol = new TableColumn("NOC");
       NOCCol.setCellValueFactory(new PropertyValueFactory<Metrics, Double>(
               "NOC"));
       TableColumn CBOCol = new TableColumn("CBO");
       CBOCol.setCellValueFactory(new PropertyValueFactory<Metrics, Double>(
               "CBO"));
       TableColumn RFCCol = new TableColumn("RFC");
       RFCCol.setCellValueFactory(new PropertyValueFactory<Metrics, Double>(
               "RFC"));
       TableColumn LCOMCol = new TableColumn("LCOM");
       LCOMCol.setCellValueFactory(new PropertyValueFactory<Metrics, Double>(
               "LCOM"));
       TableColumn ceCol = new TableColumn("Ca");
       ceCol.setCellValueFactory(new PropertyValueFactory<Metrics, Double>(
               "ce"));
       TableColumn NPMCol = new TableColumn("NPM");
       NPMCol.setCellValueFactory(new PropertyValueFactory<Metrics, Double>(
               "NPM"));

       primaryStage.setScene(new Scene(root, 200, 200));
       primaryStage.show();

       List<Metrics> metric = readMetricFromCSV("C:\\Users\\pavi\\Desktop\\11.txt");
       // let's print all the metric read from CSV file
       for (Metrics m : metric) {
           System.out.println(m);

       }

       tableView.setItems(FXCollections.observableArrayList(metric));
       tableView.getColumns().addAll(name, WMCCol, DITCol, NOCCol, CBOCol,
               RFCCol, LCOMCol, ceCol, NPMCol);

       VBox vBox = new VBox();
       vBox.setMaxSize(600, 600);
       vBox.setSpacing(10);
       PieChart c = new PieChart();
       tableView.setOnMouseClicked(new EventHandler<Event>() {

           @Override
           public void handle(Event event) {
               c.setData(FXCollections.emptyObservableList());
               TableViewSelectionModel<Metrics> tableViewSelectionModel = tableView
                       .selectionModelProperty().get();
               Metrics m = tableViewSelectionModel.getSelectedItem();
               if(m==null){
                   return;
               }
               ObservableList<PieChart.Data> pieChartData = FXCollections
                       .observableArrayList(
                               new PieChart.Data("CBO", m.getCBO()),
                               new PieChart.Data("Ce", m.getCe()),
                               new PieChart.Data("DIT", m.getDIT()),
                               new PieChart.Data("LCOM", m.getLCOM()),
                               new PieChart.Data("NOC", m.getNOC()),
                               new PieChart.Data("NPM", m.getNPM()),
                               new PieChart.Data("WMC", m.getWMC()));
               c.setData(pieChartData);
           }
       });
       vBox.getChildren().add(tableView);
       vBox.getChildren().add(c);

       root.getChildren().add(vBox);

   }

   public List<Metrics> readMetricFromCSV(String fileName) {

       List<Metrics> metricsss = new ArrayList<>();

       Path pathToFile = Paths.get(fileName);
       // create an instance of BufferedReader
       // using try with resource, Java 7 feature to close resources
       try (BufferedReader br = Files.newBufferedReader(pathToFile,
               StandardCharsets.US_ASCII)) {
           // read the first line from the text file
           String line = br.readLine();
           while (line != null && !line.isEmpty()) { // loop until all lines
                                                       // are read
               String[] attributes = line.split(" "); // the file, using a
                                                       // comma as the
                                                       // delimiter
               Metrics valueOfMetric = createMetric(attributes);
               metricsss.add(valueOfMetric); // adding metric into ArrayList
               // skip empty line
               // line.isEmpty() || line.trim().equals("") ||
               // line.trim().equals("\n"))
               br.readLine();
               line = br.readLine();
           }

       } catch (IOException ioe) {
           ioe.printStackTrace();
       }

       return metricsss;
   }
         private   Metrics createMetric(String[] metadata) {
           String name = metadata[0];
           int WMC = Integer.parseInt(metadata[1]);
           int DIT = Integer.parseInt(metadata[2]);
           int NOC = Integer.parseInt(metadata[3]);
           int CBO = Integer.parseInt(metadata[4]);
           int RFC = Integer.parseInt(metadata[5]);
           int LCOM= Integer.parseInt(metadata[6]);
           int Ce  = Integer.parseInt(metadata[7]);
           int NPM = Integer.parseInt(metadata[8]);
           return new Metrics(name,WMC,DIT,NOC,CBO,RFC,LCOM,Ce,NPM);//,cc
       }


         public class Metrics {

           private String name;
           private int WMC;
           private int DIT;
           private int NOC;
           private int CBO;
           private int RFC;
           private int LCOM;
           private int Ce;
           private int NPM;

           public Metrics( String name,int WMC,int DIT,int NOC,int CBO,int RFC,int LCOM, int Ce, int NPM) {

               this.name = name;
               this.WMC = WMC;
               this.DIT = DIT;
               this.NOC = NOC;
               this.CBO = CBO;
               this.RFC = RFC;
               this.LCOM = LCOM;
               this.Ce = Ce;
               this.NPM = NPM;
           }

           public String getName() {
               return name;
           }

           public void setName(String name) {
               this.name = name;
           }

           public int getWMC() {
               return WMC;
           }

           public void setWMC(int WMC) {
               this.WMC = WMC;
           }

           public int getDIT() {
               return DIT;
           }

           public void setDIT(int DIT) {
               this.DIT = DIT;
           }

           public int getNOC() {
               return NOC;
           }

           public void setNOC(int NOC) {
               this.NOC = NOC;
           }

           public int getCBO() {
               return CBO;
           }

           public void setCBO(int CBO) {
               this.CBO = CBO;
           }

           public int getRFC() {
               return RFC;
           }

           public void setRFC(int RFC) {
               this.RFC = RFC;
           }

           public int getLCOM() {
               return LCOM;
           }

           public void setLCOM(int LCOM) {
               this.LCOM = LCOM;
           }

           public int getCe() {
               return Ce;
           }

           public void setCe(int ce) {
               Ce = ce;
           }

           public int getNPM() {
               return NPM;
           }

           public void setNPM(int NPM) {
               this.NPM = NPM;
           }


       }




       }

解决方案

Pie Chart java example

tableView.setItems(FXCollections.observableArrayList(metric));//added

I have added above line of code to create observaleList using list

 br.readLine();//removed here this read extraline 
String line1=null;
// read the first line from the text file
String line = br.readLine();                 
while (line != null) { 

above condition is worng what if line is empty ? I have changed this too.(sorry for not explaing I dont know that much english)

    @Override
public void start(Stage primaryStage) {

    primaryStage.setTitle("java-buddy.blogspot.com");
    Group root = new Group();

    TableColumn name = new TableColumn("name");
    name.setCellValueFactory(new PropertyValueFactory<Metrics, String>(
            "name"));

    TableColumn WMCCol = new TableColumn("WMC");
    WMCCol.setCellValueFactory(new PropertyValueFactory<Metrics, Double>(
            "WMC"));

    TableColumn DITCol = new TableColumn("DIT");
    DITCol.setCellValueFactory(new PropertyValueFactory<Metrics, Double>(
            "DIT"));

    TableColumn NOCCol = new TableColumn("NOC");
    NOCCol.setCellValueFactory(new PropertyValueFactory<Metrics, Double>(
            "NOC"));
    TableColumn CBOCol = new TableColumn("CBO");
    CBOCol.setCellValueFactory(new PropertyValueFactory<Metrics, Double>(
            "CBO"));
    TableColumn RFCCol = new TableColumn("RFC");
    RFCCol.setCellValueFactory(new PropertyValueFactory<Metrics, Double>(
            "RFC"));
    TableColumn LCOMCol = new TableColumn("LCOM");
    LCOMCol.setCellValueFactory(new PropertyValueFactory<Metrics, Double>(
            "LCOM"));
    TableColumn ceCol = new TableColumn("Ca");
    ceCol.setCellValueFactory(new PropertyValueFactory<Metrics, Double>(
            "ce"));
    TableColumn NPMCol = new TableColumn("NPM");
    NPMCol.setCellValueFactory(new PropertyValueFactory<Metrics, Double>(
            "NPM"));

    primaryStage.setScene(new Scene(root, 200, 200));
    primaryStage.show();

    List<Metrics> metric = readMetricFromCSV("C:\\Users\\pavi\\Desktop\\11.txt");
    // let's print all the metric read from CSV file
    for (Metrics m : metric) {
        System.out.println(m);

    }

    tableView.setItems(FXCollections.observableArrayList(metric));
    tableView.getColumns().addAll(name, WMCCol, DITCol, NOCCol, CBOCol,
            RFCCol, LCOMCol, ceCol, NPMCol);

    VBox vBox = new VBox();
    vBox.setMaxSize(600, 600);
    vBox.setSpacing(10);
    PieChart c = new PieChart();
    tableView.setOnMouseClicked(new EventHandler<Event>() {

        @Override
        public void handle(Event event) {
            c.setData(FXCollections.emptyObservableList());
            TableViewSelectionModel<Metrics> tableViewSelectionModel = tableView
                    .selectionModelProperty().get();
            Metrics m = tableViewSelectionModel.getSelectedItem();
            if(m==null){
                return;
            }
            ObservableList<PieChart.Data> pieChartData = FXCollections
                    .observableArrayList(
                            new PieChart.Data("CBO", m.getCBO()),
                            new PieChart.Data("Ce", m.getCe()),
                            new PieChart.Data("DIT", m.getDIT()),
                            new PieChart.Data("LCOM", m.getLCOM()),
                            new PieChart.Data("NOC", m.getNOC()),
                            new PieChart.Data("NPM", m.getNPM()),
                            new PieChart.Data("WMC", m.getWMC()));
            c.setData(pieChartData);
        }
    });
    vBox.getChildren().add(tableView);
    vBox.getChildren().add(c);

    root.getChildren().add(vBox);

}

public List<Metrics> readMetricFromCSV(String fileName) {

    List<Metrics> metricsss = new ArrayList<>();

    Path pathToFile = Paths.get(fileName);
    // create an instance of BufferedReader
    // using try with resource, Java 7 feature to close resources
    try (BufferedReader br = Files.newBufferedReader(pathToFile,
            StandardCharsets.US_ASCII)) {
        // read the first line from the text file
        String line = br.readLine();
        while (line != null && !line.isEmpty()) { // loop until all lines
                                                    // are read
            String[] attributes = line.split(" "); // the file, using a
                                                    // comma as the
                                                    // delimiter
            Metrics valueOfMetric = createMetric(attributes);
            metricsss.add(valueOfMetric); // adding metric into ArrayList
            // skip empty line
            // line.isEmpty() || line.trim().equals("") ||
            // line.trim().equals("\n"))
            br.readLine();
            line = br.readLine();
        }

    } catch (IOException ioe) {
        ioe.printStackTrace();
    }

    return metricsss;
}

for charts we need create data set to pieChart.setData(pieChartData); Reff the like bellow image you ll understand

ObservableList<PieChart.Data> pieChartData = FXCollections
                    .observableArrayList(
                            new PieChart.Data("CBO", m.getCBO()),
                            new PieChart.Data("Ce", m.getCe()),
                            new PieChart.Data("DIT", m.getDIT()),
                            new PieChart.Data("LCOM", m.getLCOM()),
                            new PieChart.Data("NOC", m.getNOC()),
                            new PieChart.Data("NPM", m.getNPM()),
                            new PieChart.Data("WMC", m.getWMC()));
            c.setData(pieChartData);

这篇关于如何从解析的文本文件传递数据并将值插入javafx中的ObservableList?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆