The example in this article describes the JavaFX simple implementation of the "My Computer Explorer" effect. Share to everyone for your reference. Specifically as follows:
1. Java Code:
Package TTT;
Import Java.io.File;
Import javafx.application.Application;
Import Javafx.beans.value.ChangeListener;
Import Javafx.beans.value.ObservableValue;
Import javafx.collections.FXCollections;
Import javafx.collections.ObservableList;
Import Javafx.scene.Scene;
Import Javafx.scene.control.TableColumn;
Import Javafx.scene.control.TableView;
Import Javafx.scene.control.TreeItem;
Import Javafx.scene.layout.BorderPane;
Import Javafx.scene.layout.HBox;
Import javafx.scene.layout.Priority;
Import Javafx.stage.Stage;
Import Javafx.scene.control.TreeView;
Import Javafx.scene.control.cell.PropertyValueFactory; public class TreeViews extends application {public static observablelist<filedetail> data = Fxcollections.observa
Blearraylist ();
public static void Main (string[] args) {launch (args);
@Override public void Start (Stage primarystage) {Primarystage.settitle ("Javafx implementation \" My Computer \ "explorer"); treeitem<file> Rootitem = new treeitem<> (New File (SYSTEM.GEtenv ("COMPUTERNAME"));
for (file File:File.listRoots ()) {Filetreeitem Rootsitem = new Filetreeitem (file);
Rootitem.getchildren (). Add (Rootsitem);
treeview<file> tree = new treeview<file> (Rootitem);
Hbox root = new Hbox ();
tableview<filedetail> TableView = new tableview<> (data);
Tablecolumn<filedetail, string> firstcolumn = new tablecolumn<> ("file");
Firstcolumn.setcellvaluefactory (New Propertyvaluefactory<filedetail, string> ("FileName"));
Firstcolumn.setprefwidth (120);
Tablecolumn<filedetail, string> secondcolumn = new tablecolumn<> ("type");
Secondcolumn.setcellvaluefactory (New Propertyvaluefactory<filedetail, string> ("type"));
Secondcolumn.setprefwidth (120);
Tablecolumn<filedetail, string> thirdcolumn = new Tablecolumn<> ("Last Modified");
Thirdcolumn.setcellvaluefactory (New Propertyvaluefactory<filedetail, string> ("LastModified")); Thirdcolumn.setpRefwidth (200);
Tableview.getcolumns (). SetAll (Firstcolumn, Secondcolumn, Thirdcolumn);
Hbox.sethgrow (tree, priority.always);
Hbox.sethgrow (TableView, priority.always);
Root.getchildren (). AddAll (Tree,tableview); Tree.getselectionmodel (). Selecteditemproperty (). AddListener (New changelistener<treeitem<file>> () {@ Override public void changed (OBSERVABLEVALUE<? extends treeitem<file>> observable, treeitem<file> OldValue, treeitem<file> newvalue) {observablelist<treeitem<file>> treelist = NewValu
E.getchildren ();
observablelist<filedetail> tablelist = Fxcollections.observablearraylist ();
for (treeitem<file> item:treelist) {filedetail filedetail = new Filedetail (Item.getvalue ());
Tablelist.add (Filedetail);
} data.setall (Tablelist);
}
});
Primarystage.setscene (new Scene (root));
Primarystage.setheight (600); PrimaRystage.show ();
}
}
2. Java code:
Package TTT;
Import Java.io.File;
Import Java.text.SimpleDateFormat;
Import Java.util.Date;
public class Filedetail {private String FileName;
Private String lastmodified;
Private Boolean isfile;
Private Boolean Isfolder;
private Boolean exists;
Private String type;
private long length;
Private SimpleDateFormat FMT;
Public filedetail (file file) {isfile = File.isfile ();
Isfolder = File.isdirectory ();
exists = file.exists (); if (exists) {this.
FileName = File.getname ();
FMT = new SimpleDateFormat ("Yyyy-mm-dd hh:mm");
Date date = new Date (file.lastmodified ()); This.
LastModified = Fmt.format (date);
This.length = File.length ();
if (isfolder) {this.type = "folder";
else This.type = string.valueof (This.length/(long) 1024) + "KB";
} public String GetFileName () {return FileName;
public void Setfilename (String filename) {filename = filename; } public String GetlastmodiFied () {return lastmodified;
} public void Setlastmodified (String lastmodified) {lastmodified = LastModified;
Public String GetType () {return type;
public void SetType (String type) {this.type = type;
Public long GetLength () {return length;
public void SetLength (long length) {this.length = length; }
}
3. Java code:
Package TTT;
Import Java.io.File;
Import javafx.collections.FXCollections;
Import javafx.collections.ObservableList;
Import Javafx.scene.control.TreeItem;
public class Filetreeitem extends Treeitem<file> {private Boolean isleaf;
Private Boolean Isfirsttimechildren = true;
Private Boolean isfirsttimeleaf = true;
Public filetreeitem (file file) {super (file); @Override public observablelist<treeitem<file>> GetChildren () {if (Isfirsttimechildren) {is
Firsttimechildren = false;
Super.getchildren (). SetAll (Buildchildren (this));
return Super.getchildren ();
@Override public boolean isleaf () {if (isfirsttimeleaf) {isfirsttimeleaf = false;
File F = (file) getValue ();
IsLeaf = F.isfile ();
return isleaf; Private observablelist<treeitem<file>> Buildchildren (treeitem<file> TreeItem) {File F = TreeItem.g
Etvalue ();
if (f!= null && f.isdirectory ()) { file[] files = f.listfiles ();
if (Files!= null) {observablelist<treeitem<file>> children = fxcollections.observablearraylist ();
for (File childfile:files) {children.add (new Filetreeitem (Childfile));
return children;
} return Fxcollections.emptyobservablelist ();
}
}
4. Operation Effect Screenshot:
I hope this article will help you with your Java programming.