javaFx TableView

金昌胤
2023-12-01

基本tableview: 基本、属性绑定两种方式

合并列

加载Map类型的数据

package application.com.tableView;




import java.util.HashMap;

import javafx.application.Application;
import javafx.beans.property.SimpleBooleanProperty;
import javafx.beans.property.SimpleDoubleProperty;
import javafx.beans.property.SimpleIntegerProperty;
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.value.ObservableValue;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.control.TableColumn.CellDataFeatures;
import javafx.scene.layout.AnchorPane;
import javafx.stage.Stage;
import javafx.util.Callback;

public class BasicTableView extends Application{

	public static void main(String[] args) {
		launch(args);
	}
	
	
	
	@Override
	public void start(Stage primaryStage) throws Exception {
	
		ObservableList<Data> list = FXCollections.observableArrayList();
		
		Data d1 = new Data("d1",12,98.0,true);
		Data d2 = new Data("d2",12,98.0,true);
		Data d3 = new Data("d3",12,98.0,true);
		
		list.add(d1);
		list.add(d2);
		list.add(d3);

		//设置表格
		TableView tbView = new TableView<>(list);
		tbView.setPlaceholder(new Label("No Data"));
		tbView.setPrefHeight(200.00);
		tbView.setPrefWidth(200.00);
		
		// 设置列
		TableColumn<Data, String> tc_nameColumn = new TableColumn<Data,String>("Name");
		
		// 添加列
		tbView.getColumns().add(tc_nameColumn);
		
		//设置列值(简写)
		//tc_nameColumn.setCellValueFactory(new PropertyValueFactory<Data,String>("name")); // 前提条件: Data class必须是public修饰的类
		
		
		//设置列值(自定义显示数据)
		tc_nameColumn.setCellValueFactory(new Callback<TableColumn.CellDataFeatures<Data,String>, ObservableValue<String>>() {
			
			@Override
			public ObservableValue<String> call(CellDataFeatures<Data, String> param) {
				// TODO Auto-generated method stub
				SimpleStringProperty simpleStringProperty = new SimpleStringProperty(param.getValue().getName());
				return simpleStringProperty;
			}
		});
		
		
		
		//与Data数据源不同的写法
		ObservableList<Data2> list2 = FXCollections.observableArrayList();
		
		Data2 d12 = new Data2("d12",12,98.0,true);
		Data2 d22 = new Data2("d22",12,98.0,true);
		Data2 d32 = new Data2("d32",12,98.0,true);
		
		list2.add(d12);
		list2.add(d22);
		list2.add(d32);

		TableView tbView2 = new TableView<>(list2);
		tbView2.setPlaceholder(new Label("No Data"));
		tbView2.setPrefHeight(200.00);
		tbView2.setPrefWidth(200.00);
		

		TableColumn<Data2, String> tc_nameColumn2 = new TableColumn<Data2,String>("Name");
		//tbView2.getColumns().add(tc_nameColumn2);// 合并之后需要移除
		//设置值不同
		tc_nameColumn2.setCellValueFactory(new Callback<TableColumn.CellDataFeatures<Data2,String>, ObservableValue<String>>() {

			@Override
			public ObservableValue<String> call(CellDataFeatures<Data2, String> param) {
				// TODO Auto-generated method stub
				return param.getValue().name;
			}
		
		});
		
		TableColumn<Data2, Number> tc_ageColumn2 = new TableColumn<Data2,Number>("Age");
		//tbView2.getColumns().add(tc_ageColumn2);// 合并之后需要移除
		//设置值不同
		tc_ageColumn2.setCellValueFactory(new Callback<TableColumn.CellDataFeatures<Data2,Number>, ObservableValue<Number>>() {

			@Override
			public ObservableValue<Number> call(CellDataFeatures<Data2, Number> param) {
				// TODO Auto-generated method stub
				return param.getValue().age;
			}
		
		});
		
		
		//合并列数据,合并列之后,原先列的添加操作需要移除。
		TableColumn<Data2, String> group = new TableColumn<Data2,String>("group");
		group.getColumns().add(tc_nameColumn2);
		group.getColumns().add(tc_ageColumn2);
		tbView2.getColumns().add(group);
		
		
		
		
		
		// Map数据
		
		
		HashMap<String, SimpleStringProperty> map1 = new HashMap<>();
		map1.put("name", new SimpleStringProperty("B"));
		
		
		HashMap<String, SimpleStringProperty> map2 = new HashMap<>();
		map2.put("age", new SimpleStringProperty("123"));
		
		
		ObservableList<HashMap<String, SimpleStringProperty>> listMap = FXCollections.observableArrayList();
		listMap.add(map1);
		listMap.add(map2);
		
		
		
		TableView<HashMap<String, SimpleStringProperty>> tbView3 = new TableView<HashMap<String, SimpleStringProperty>>(listMap);
		tbView3.setPlaceholder(new Label("No Data"));
		tbView3.setPrefHeight(200.00);
		tbView3.setPrefWidth(200.00);
		

		TableColumn<HashMap<String, SimpleStringProperty>, String> tc_nameColumn3 = new TableColumn<HashMap<String, SimpleStringProperty>,String>("Name");
		
		tc_nameColumn3.setCellValueFactory(new Callback<TableColumn.CellDataFeatures<HashMap<String,SimpleStringProperty>,String>, ObservableValue<String>>() {

			@Override
			public ObservableValue<String> call(CellDataFeatures<HashMap<String, SimpleStringProperty>, String> param) {
				// TODO Auto-generated method stub
				return param.getValue().get("name");
			}
		
		
		});
		tbView3.getColumns().add(tc_nameColumn3);
		
		
		
		
		
		
		Button btn = new Button("UPDATE");
		
		AnchorPane anchorPane = new AnchorPane();
		anchorPane.getChildren().addAll(btn,tbView,tbView2,tbView3);
		
		AnchorPane.setLeftAnchor(btn, 10.0);
		AnchorPane.setBottomAnchor(btn, 10.0);
		
		AnchorPane.setLeftAnchor(tbView, 80.0);
		AnchorPane.setTopAnchor(tbView, 20.0);
		
		AnchorPane.setLeftAnchor(tbView2, 80.0);
		AnchorPane.setTopAnchor(tbView2, 400.0);
		
		AnchorPane.setLeftAnchor(tbView3, 80.0);
		AnchorPane.setTopAnchor(tbView3, 700.0);
		
		
		Scene scene = new Scene(anchorPane);
		
		primaryStage.setScene(scene);
		
		
		primaryStage.show();
	}
	
	
	public class Data{
		private String name;
		private int age;
		private double score;
		private boolean is;
		
		public Data() {
			
		}
		
		public Data(String name, int age, double score, boolean is) {
			super();
			this.name = name;
			this.age = age;
			this.score = score;
			this.is = is;
		}
		public String getName() {
			return name;
		}
		public void setName(String name) {
			this.name = name;
		}
		public int getAge() {
			return age;
		}
		public void setAge(int age) {
			this.age = age;
		}
		public double getScore() {
			return score;
		}
		public void setScore(double score) {
			this.score = score;
		}
		public boolean isIs() {
			return is;
		}
		public void setIs(boolean is) {
			this.is = is;
		}
		
		
		
		
	}
	
 public  class Data2{
	 private SimpleStringProperty name = new SimpleStringProperty();
	 
	 private SimpleIntegerProperty age = new SimpleIntegerProperty();
	 
	 private SimpleDoubleProperty  score = new SimpleDoubleProperty();
	 
	 
	 private SimpleBooleanProperty is = new SimpleBooleanProperty();


	public Data2(String name, int age, double score,
			boolean is) {
		super();
		this.name.set(name);
		this.age.set(age);
		this.score.set(score);
		this.is.set(is);
	}


	public String getName() {
		return name.get();
	}


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


	public int getAge() {
		return age.get();
	}


	public void setAge(int age) {
		this.age.set(age);
	}


	public double getScore() {
		return score.get();
	}


	public void setScore(double score) {
		this.score.set(score);
	}


	public boolean getIs() {
		return is.get();
	}


	public void setIs(boolean is) {
		this.is.set(is);
	}
	 
	
	 
	 
	 
 }
}

其他基本的设置

package application.com.tableView;

import javafx.application.Application;
import javafx.beans.property.SimpleBooleanProperty;
import javafx.beans.property.SimpleIntegerProperty;
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.SelectionMode;
import javafx.scene.control.TableCell;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.TableColumn.CellDataFeatures;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
import javafx.util.Callback;

public class OperationTableView extends Application{

	public static void main(String[] args) {
		launch(args);
	}
	
	@Override
	public void start(Stage primaryStage) throws Exception {
		// TODO Auto-generated method stub
		ObservableList<Student> list = FXCollections.observableArrayList();
		
		Student s1 = new Student("limei", 99, false);
		Student s2 = new Student("hanmeimei", 99, true);
	
		list.add(s2);
		list.add(s1);
		
		TableView<Student> tbView = new TableView<>(list);
		tbView.setPlaceholder(new Label("NO Data"));
		TableColumn<Student, String> tbNameCell = new TableColumn<Student,String>("NAME");
		tbNameCell.setCellValueFactory(new PropertyValueFactory<>("name"));
		
		TableColumn<Student, Integer> tbScoreColumn = new TableColumn<>("SCORE");
		//设置显示列
		tbScoreColumn.setCellValueFactory(new PropertyValueFactory<>("score"));
		
		//自定义cell UI
		tbScoreColumn.setCellFactory(new Callback<TableColumn<Student,Integer>, TableCell<Student,Integer>>() {

			@Override
			public TableCell<Student, Integer> call(TableColumn<Student, Integer> param) {
				
				// 自定义cell内容UI
				TableCell<Student, Integer> cell = new TableCell<Student, Integer>() {
					@Override
					protected void updateItem(Integer item, boolean empty) {
						// TODO Auto-generated method stub
						super.updateItem(item, empty);
						if(empty) {return;}
						HBox hBox = new HBox();
						hBox.setStyle("-fx-background-color:#FFFF55");
						hBox.setAlignment(Pos.CENTER);
						Button label = new Button(item.toString());
						label.setOnAction((e)->{
							System.out.println("ingo update");
							hBox.setStyle("-fx-background-color:#AABBCC");
						});
						hBox.getChildren().add(label);
						this.setGraphic(hBox);
					}
				};
				return cell;
			}
		
		});
		
		TableColumn<Student, String> tbIsCheckColumn = new TableColumn<Student, String>("CHECK");
		tbIsCheckColumn.setCellValueFactory(new Callback<TableColumn.CellDataFeatures<Student,String>, ObservableValue<String>>() {
			
			@Override
			public ObservableValue<String> call(CellDataFeatures<Student, String> param) {
				// TODO Auto-generated method stub
				SimpleStringProperty simpleStringProperty = new SimpleStringProperty(param.getValue().isCheck() ? "是" :"否");
				return simpleStringProperty;
			}
		});
	
		
		
		
		
		tbView.getColumns().addAll(tbNameCell,tbScoreColumn,tbIsCheckColumn);
		
		tbView.setTableMenuButtonVisible(true); //设置显示列
		tbView.scrollTo(0); // 滚动到指定索引行
		tbView.scrollToColumnIndex(0);// 滚动到指定列
		tbView.setFixedCellSize(60.0); // 设置单元格大小
		tbView.setEditable(false);// 设置是否可编辑,如果设置为true,则需要实现指定可编辑的列CellFactory方法
		
		
		tbView.getSelectionModel().setSelectionMode(SelectionMode.SINGLE); // 设置选择模式
		/**
		 * 设置选择监听
		 * tbView.getSelectionModel().selectedItemProperty().addListener(new ChangeListener<Student>() {

			@Override
			public void changed(ObservableValue<? extends Student> observable, Student oldValue, Student newValue) {
				// TODO Auto-generated method stub
				
			}
		
		});
		*/
		//和上面的是一致的
		tbView.getSelectionModel().selectedItemProperty().addListener((o,ovalue,nvalue)->{
			System.out.println(nvalue);
		});
		
		
		
		
		
		VBox vbox = new VBox();
		
		vbox.getChildren().add(tbView);		
		Scene scene = new Scene(vbox);
		primaryStage.setScene(scene);
		primaryStage.show();
	}
	
	
	
	
	public  class Student {
		private SimpleStringProperty name = new SimpleStringProperty();
		private SimpleIntegerProperty score = new SimpleIntegerProperty();
		private SimpleBooleanProperty isCheck = new SimpleBooleanProperty();
		public Student(String name, int score, boolean isCheck) {
			super();
			this.name.set(name);
			this.score.set(score);
			this.isCheck.set(isCheck);
		}
		
		public Student() {
			
			
		}

		public String getName() {
			return name.get();
		}

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

		public int getScore() {
			return score.get();
		}

		public void setScore(int score) {
			this.score.set(score);
		}

		public boolean isCheck() {
			return isCheck.get();
		}

		public void setCheck(boolean isCheck) {
			this.isCheck.set(isCheck);
		}
	}
}

 类似资料:

相关阅读

相关文章

相关问答