開發環境
【IntelliJ IDEA 2019.2 + jdk8】
先建一個基本工程
放置界面
先放一個Pane, 這是一個固定步驟,先要有一個容器,才能放TableView等其它控件。
放入TableView
若想增加列數,則加入TableColumn控件即可。
修改列名稱:直接在右邊的Properties --> Text 里輸入新的名程即可。
添加數據
先填入一個 TableView的ID
然后,代碼就可以根據這個ID,初始化變量,如下:
把Main.java里的代碼改一下就行,其它的文件不用改。
Main.java如下:
package sample;
import javafx.application.Application;
import javafx.beans.property.SimpleStringProperty;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.TableCell;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.stage.Stage;
public class Main extends Application {
public javafx.scene.control.TableView m_TableView;
public javafx.scene.control.TableColumn m_TableColumn_0;
public javafx.scene.control.TableColumn m_TableColumn_1;
public javafx.scene.control.TableColumn m_TableColumn_2;
public static void main(String[] args) {
launch(args);
}
@SuppressWarnings({ "rawtypes", "unchecked" })
@Override
public void start(Stage primaryStage) throws Exception{
Parent root = FXMLLoader.load(getClass().getResource("sample.fxml"));
primaryStage.setTitle("Hello World");
primaryStage.setScene(new Scene(root, 500, 500));
primaryStage.show();
m_TableView = (javafx.scene.control.TableView)root.lookup("#Id_TableView_1");
m_TableColumn_0 = (javafx.scene.control.TableColumn) m_TableView.getColumns().get(0);
m_TableColumn_1 = (javafx.scene.control.TableColumn) m_TableView.getColumns().get(1);
m_TableColumn_2 = (javafx.scene.control.TableColumn) m_TableView.getColumns().get(2);
m_TableColumn_0.setCellValueFactory(new PropertyValueFactory<Object, Object>("Name")); // 這里是大小寫不敏感。
m_TableColumn_1.setCellValueFactory(new PropertyValueFactory<Object, Object>("value"));
m_TableColumn_2.setCellValueFactory(new PropertyValueFactory<Object, Object>("Unit"));
// 下面的代碼可以重定義顯示的格式,可以實現多行顯示,等等,功能非常的強大。
// 參考: https://blog.csdn.net/asdinto/article/details/91524452?utm_medium=distribute.pc_relevant_bbs_down.none-task-blog-baidujs-1.nonecase&depth_1-utm_source=distribute.pc_relevant_bbs_down.none-task-blog-baidujs-1.nonecase
m_TableColumn_2.setCellFactory(col->{
TableCell<Object, Object/*Double*/> cell = new TableCell<Object, Object/*Double*/>(){
@Override
public void updateItem(Object/*Double*/ item, boolean empty) {
super.updateItem(item, empty);
this.setText(null);
this.setGraphic(null);
if (!empty) {
int rowIndex = this.getIndex();
//double d = tbView.getItems().get(rowIndex).getWage();
//this.setText(StringUtils.formatNumber(2, d));
String src = item.toString();
String dst = new String();
for (int i=0; i<src.length(); i+=10){
int len = 10;
if (i + len > src.length())len = src.length() - i;
dst += src.substring(i, i+len);
dst += "\n";
}
this.setText(dst);
}
}
};
return cell;
});
// 第一種賦值法:
// m_TableView.getItems().add(new Person("人速", "100", "米"));
// m_TableView.getItems().add(new Person("馬速", "高速度", "秒"));
// 第二種賦值法:
ObservableList<Person> data = FXCollections.observableArrayList(
new Person("1", "張三", "米12345679456877455555aaaabbb"),
new Person("2", "李四", "米"),
new Person("3", "王五", "米")
);
m_TableView.setItems(data);
data.get(0).setName("ooo");
}
// 這是我們數據里的對象--配角的配角
public static class Person {
private SimpleStringProperty Name;// 這里是大小寫不敏感。
private SimpleStringProperty value;
private SimpleStringProperty Unit;
private Person(String name_in, String value_in, String Unit_in) {
this.Name = new SimpleStringProperty(name_in);
this.value = new SimpleStringProperty(value_in);
this.Unit = new SimpleStringProperty(Unit_in);
}
public String getName() {return Name.get();}
public void setName(String name_in) {Name.set(name_in);}
public String getValue() {return value.get();} // 變量在這里,頭個字母必須大寫。
public void setValue(String value_in) {value.set(value_in);}
public String getUnit() {return Unit.get();}
public void setUnit(String Unit_in) {Unit.set(Unit_in);}
}
}
以上代碼參考自:《JavaFX+Jfoenix 學習筆記(三)--TableView數據表格源碼》
程序運行效果如下:
格子自動調整大小
有時候,字符串特別長,放不下,那么,這時,該怎么辦呢?
上面的寫法盡可能地利用fxml來畫圖,很方便,但是有一點不是很好,就是樣式一旦固定后,后面修改的余地不大,也是說,靈活性會缺失一點,下面介紹一種純代碼的寫法。
純代碼寫法