封装树表控件,Json传入内容,JavaFX方式

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/haoranhaoshi/article/details/82977947

TreeTableViewDataBean.java:

package TreeTableView;

import javafx.beans.property.SimpleStringProperty;
import javafx.beans.property.StringProperty;

public class TreeTableViewDataBean {
	//通用树表对应的一行记录可最多容纳的属性
	 public static final int currentPropertyCount = 15; 
	 private final StringProperty data1;
	 private final StringProperty data2;
	 private final StringProperty data3;
	 private final StringProperty data4;
	 private final StringProperty data5;
	 private final StringProperty data6;
	 private final StringProperty data7;
	 private final StringProperty data8;
	 private final StringProperty data9;
	 private final StringProperty data10;
	 private final StringProperty data11;
	 private final StringProperty data12;
	 private final StringProperty data13;
	 private final StringProperty data14;
	 private final StringProperty data15;
	 
	 public TreeTableViewDataBean(String[] values) {		 
		 this.data1    = new SimpleStringProperty(values[0]);
		 this.data2    = new SimpleStringProperty(values[1]);
		 this.data3    = new SimpleStringProperty(values[2 ]);
		 this.data4    = new SimpleStringProperty(values[3 ]);
		 this.data5    = new SimpleStringProperty(values[4 ]);
		 this.data6    = new SimpleStringProperty(values[5 ]);
		 this.data7    = new SimpleStringProperty(values[6 ]);
		 this.data8    = new SimpleStringProperty(values[7 ]);
		 this.data9    = new SimpleStringProperty(values[8 ]);
		 this.data10   = new SimpleStringProperty(values[9 ]);
		 this.data11   = new SimpleStringProperty(values[10]);
		 this.data12   = new SimpleStringProperty(values[11]);
		 this.data13   = new SimpleStringProperty(values[12]);
		 this.data14   = new SimpleStringProperty(values[13]);
		 this.data15   = new SimpleStringProperty(values[14]);
	 }

	 public StringProperty data1Property() { return data1; }
	 public StringProperty data2Property() { return data2; }
	 public StringProperty data3Property() { return data3; }
	 public StringProperty data4Property() { return data4; }
	 public StringProperty data5Property() { return data5; }
	 public StringProperty data6Property() { return data6; }
	 public StringProperty data7Property() { return data7; }
	 public StringProperty data8Property() { return data8; }
	 public StringProperty data9Property() { return data9; }
	 public StringProperty data10Property() { return data10; }
	 public StringProperty data11Property() { return data11; }
	 public StringProperty data12Property() { return data12; }
	 public StringProperty data13Property() { return data13; }
	 public StringProperty data14Property() { return data14; }
	 public StringProperty data15Property() { return data15; }

}

NewTreeTableViewCreator.java:

package TreeTableView;

import java.util.Iterator;

import javafx.scene.Parent;
import javafx.scene.control.TreeItem;
import javafx.scene.control.TreeTableColumn;
import javafx.scene.control.TreeTableView;
import javafx.scene.control.cell.TreeItemPropertyValueFactory;
import net.sf.json.JSONObject;

@SuppressWarnings("rawtypes")
public class NewTreeTableViewCreator{
	
	public NewTreeTableViewCreator(){
		
	}
	
	@SuppressWarnings({ "unchecked" })
	//递归表行内容
    public TreeItem getNodeData(String jsonStr,int columnLengh,TreeItem fatherItem){  
        JSONObject json = JSONObject.fromObject(jsonStr);
        int currentPropertyCount = TreeTableViewDataBean.currentPropertyCount;
        String[] values = new String[currentPropertyCount];
    	for(int i = 0;i < columnLengh;i++){
    		values[i] = json.getJSONObject("nodeInfo").get("data" + (i + 1)).toString();
    	}
    	for(int i = columnLengh;i < currentPropertyCount;i++){
    		values[i] = "";
    	}
    	
    	TreeTableViewDataBean inventory = new TreeTableViewDataBean(values);
    	TreeItem treeItem = new TreeItem(inventory);
    	
    	if(fatherItem != null){
    		fatherItem.getChildren().add(treeItem);
    	}else{
    		treeItem.setExpanded(true);
    	}
    	
    	Iterator<JSONObject> it = (json.getJSONArray("childNotes")).iterator(); 
    	while(it.hasNext()){  
          JSONObject json2 = it.next();  
          getNodeData(json2.toString(),columnLengh,treeItem) ;
        }  
    	
        return treeItem;  
    }

	@SuppressWarnings({ "unchecked"})
	public Parent createContent(double layoutX, double layoutY, double width, double height, String[] columnName,
			double minColumnWidth,String jsonMessage) {
		final TreeTableView treeTableView = new TreeTableView(getNodeData(jsonMessage,columnName.length,null));
		for (int i = 0; i < columnName.length; i++) {
			TreeTableColumn treeTableColumn = new TreeTableColumn(columnName[i]);
			treeTableColumn.setCellValueFactory(new TreeItemPropertyValueFactory("data" + (i + 1)));
			treeTableColumn.setMinWidth(minColumnWidth);
			treeTableView.getColumns().add(treeTableColumn);
		}

		treeTableView.setEditable(false);
		treeTableView.setLayoutX(layoutX);// X
		treeTableView.setLayoutY(layoutY);// Y
		treeTableView.setPrefSize(width, height);// Width Height

		return treeTableView;
	}
}

NewTreeTableViewTest.java:

package TreeTableView;

import javafx.application.Application;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;

public class NewTreeTableViewTest extends Application {
    @Override
    public void start(Stage primaryStage) throws Exception {
        primaryStage.setTitle("TreeTableViewApp");    
        
        double layoutX = 100;
        double layoutY = 100;
        double width = 400;
        double height = 300;
        String[] columnName = { "Name", "Data", "Notes" };        
        double minColumnWidth = 150;
        String jsonMessage = "{'nodeInfo':{'data1':'Root','data2':'Root data','data3':''},'childNotes':[{'nodeInfo':{'data1':'Child 1','data2':'Child 1 data','data3':'child  1 notes'},'childNotes':[{'nodeInfo':{'data1':'Child 3','data2':'Child 3 data','data3':'Child 3 notes'},'childNotes':[]}]},{'nodeInfo':{'data1':'Child 2','data2':'Child 2 data','data3':'Child 2 notes'},'childNotes':[]}]}";
        //传入树表X坐标、Y坐标、宽、高、表头列名、表头列宽、表体内容
        Parent parent = new NewTreeTableViewCreator().createContent(layoutX, layoutY, width, height, columnName, minColumnWidth,jsonMessage);
        
        primaryStage.setScene(new Scene(parent));
        primaryStage.show();
    }
    
    public static void main(String[] args) {
        launch(args);
    }
}


截图:

截图

猜你喜欢

转载自blog.csdn.net/haoranhaoshi/article/details/82977947
今日推荐