javafx 2 的表格 绑定到 map
程序员文章站
2022-05-30 23:20:18
...
当 一个tabview 要不同时候展现不同的数据表时,往往需要tabview的数据绑定到一个动态集合上。 下面的例子就展示了这种功能。
其中Person类,可以是共用,随便取的一个名字,不要以为仅仅是表示人名才能用
本例是 jdk 1.8 所写。 用了lamda达表达式
界面是这样的
/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package basetech.hello; import java.util.ArrayList; import java.util.LinkedHashMap; import java.util.List; import java.util.function.Predicate; import javafx.application.Application; import javafx.beans.property.SimpleStringProperty; import javafx.beans.property.StringProperty; import javafx.beans.value.ChangeListener; import javafx.beans.value.ObservableValue; import javafx.collections.FXCollections; import javafx.collections.ObservableList; import javafx.geometry.HPos; import javafx.geometry.Insets; import javafx.geometry.Orientation; import javafx.geometry.Pos; import javafx.scene.Parent; import javafx.scene.Scene; import javafx.scene.control.Alert; import javafx.scene.control.Button; import javafx.scene.control.Label; import javafx.scene.control.Separator; import javafx.scene.control.TableColumn; import javafx.scene.control.TablePosition; import javafx.scene.control.TableRow; import javafx.scene.control.TableView; import javafx.scene.control.TextField; import javafx.scene.control.ToolBar; import javafx.scene.control.cell.PropertyValueFactory; import static javafx.scene.input.KeyCode.S; import javafx.scene.input.MouseButton; import javafx.scene.layout.BorderPane; import javafx.scene.layout.ColumnConstraints; import javafx.scene.layout.GridPane; import javafx.stage.Stage; import javafx.util.Callback; /** * * @author cloud */ public class TableWithMapDemo extends Application{ private Button addBT= new Button("新增"); private Button editBT= new Button("修改"); private Button cancelBT= new Button("取消"); private Button saveBT= new Button("保存"); private Button deleteBT= new Button("删除"); private boolean isAdd=false; private TableView tableView = new TableView(); private TextField firstnameTF= new TextField(); private TextField lastnameTF= new TextField(); private TextField emailTF= new TextField(); private Alert alert=new Alert(Alert.AlertType.INFORMATION); private Person person=Person.create("first","second","email"); final ObservableList<Person> data = FXCollections.observableArrayList ( person.putNewRow("张","三","zhangsan@email.com"), person.putNewRow("李","四","lisi@email.com"), person.putNewRow("王","二","wanger@email.com"), person.putNewRow("刘","七","liuqi@email.com") ); private Parent createToolbar(){ for(int i=0;i<2000;i++){ String id=Integer.toString(i); data.add(person.putNewRow("姓"+id,"名"+id,"email"+id+"@mail.com")); } ToolBar toolbar = new ToolBar(); Button cellBT=new Button("操作指定单元格"); cellBT.setOnAction( e->{ //对指定单元格操作 Person person=(Person)tableView.getItems().get(2); alert.setContentText(person.getColData(0)); alert.show(); // 得到选中行 列 int selectRow=tableView.getSelectionModel().getFocusedIndex(); int selectCol=((TablePosition)tableView.getSelectionModel().getSelectedCells().get(0)).getColumn(); Object row=tableView.getSelectionModel().getSelectedItem(); }); toolbar.getItems().addAll(addBT,editBT,saveBT,cancelBT,deleteBT,cellBT); tableView.setStyle(" .column-header {" + " -fx-size: 300;" + "}"); return toolbar; } private Parent createEditPane(){ GridPane grid=new GridPane(); grid.setAlignment(Pos.CENTER); grid.setHgap(3); grid.setVgap(20); grid.setPadding(new Insets(25,25,25,25)); grid.setGridLinesVisible(false); Label lb=new Label("姓"); grid.add(lb, 0, 0); grid.add(firstnameTF, 1, 0); Separator sp=new Separator(Orientation.VERTICAL); sp.setOpacity(0); grid.add(sp, 2, 0); lb=new Label("名"); grid.add(lb, 3, 0); grid.add(lastnameTF, 4, 0); sp=new Separator(Orientation.VERTICAL); sp.setOpacity(0); grid.add(sp, 5, 0); lb=new Label("Email"); grid.add(lb, 6, 0); grid.add(emailTF, 7, 0); sp=new Separator(Orientation.VERTICAL); sp.setOpacity(0); grid.add(sp, 8, 0); for (int i=0;i<9;i++){ ColumnConstraints column1 = new ColumnConstraints(); if (i%3==0){ column1.setPrefWidth(60); column1.setHalignment(HPos.RIGHT);//水平居中 } else if (i%3==2){ column1.setPrefWidth(10); column1.setHalignment(HPos.LEFT);//水平居中 } else { column1.setPercentWidth(20); column1.setHalignment(HPos.LEFT);//水平居中 } grid.getColumnConstraints().add(column1); } return grid; } private Parent createTop(){ GridPane grid=new GridPane(); grid.add(createToolbar(), 0, 0,2,1); grid.add(createEditPane(), 1, 1,1,1); ColumnConstraints column1 = new ColumnConstraints(); column1.setPercentWidth(5); grid.getColumnConstraints().add(column1); ColumnConstraints column2 = new ColumnConstraints(); column2.setPercentWidth(95); column2.setHalignment(HPos.RIGHT);//水平居中 grid.getColumnConstraints().add(column2); return grid; } private Parent createContent() { TableColumn firstNameCol = new TableColumn(); firstNameCol.setText("First"); firstNameCol.setId(person.getColId(0)); firstNameCol.setCellValueFactory(person.getMapCellValueFactory()); firstNameCol.prefWidthProperty().bind(tableView.widthProperty().divide(3)); // w * 1/3 TableColumn lastNameCol = new TableColumn(); lastNameCol.setId(person.getColId(1)); lastNameCol.setText("Last"); lastNameCol.setCellValueFactory(person.getMapCellValueFactory()); lastNameCol.prefWidthProperty().bind(tableView.widthProperty().divide(3)); // w * 1/3 TableColumn emailCol = new TableColumn(); emailCol.setId(person.getColId(2)); emailCol.setText("Email"); emailCol.setMinWidth(200); emailCol.setCellValueFactory(person.getMapCellValueFactory()); emailCol.prefWidthProperty().bind(tableView.widthProperty().divide(3)); // w * 1/3 tableView.setItems(data); tableView.getColumns().addAll(firstNameCol, lastNameCol, emailCol); BorderPane borderPane=new BorderPane(); borderPane.setTop(createTop()); borderPane.setCenter(tableView); return borderPane; } private void toolbarAction(){ saveBT.setDisable(true); cancelBT.setDisable(true); alert.getDialogPane().setHeaderText(null); saveBT.setOnAction(e->{ alert.setContentText("按下了保存按钮"); alert.show(); saveBT.setDisable(true); cancelBT.setDisable(true); addBT.setDisable(false); editBT.setDisable(false); deleteBT.setDisable(false); if(isAdd){ Person p= person.putNewRow(firstnameTF.getText(),lastnameTF.getText(),emailTF.getText()); data.add(p); } else { Person person =(Person)tableView.getSelectionModel().getSelectedItem(); person.editRow(firstnameTF.getText(),lastnameTF.getText(),emailTF.getText()); } isAdd=false; }); cancelBT.setOnAction(e->{ alert.setContentText("按下了取消按钮"); alert.show(); saveBT.setDisable(true); cancelBT.setDisable(true); addBT.setDisable(false); editBT.setDisable(false); deleteBT.setDisable(false); isAdd=false; }); deleteBT.setOnAction(e->{ data.remove(tableView.getSelectionModel().getFocusedIndex()); }); addBT.setOnAction(e->{ saveBT.setDisable(false); cancelBT.setDisable(false); addBT.setDisable(true); editBT.setDisable(true); deleteBT.setDisable(true); isAdd=true; }); editBT.setOnAction(e->{ saveBT.setDisable(false); cancelBT.setDisable(false); addBT.setDisable(true); editBT.setDisable(true); deleteBT.setDisable(true); isAdd=false; }); tableView.setOnMouseClicked(e->{ if(e.getButton().equals(MouseButton.SECONDARY)){ //展示 选中行列。注意:TablePosition还有表的 选中游标 TablePosition pos=(TablePosition)tableView.getSelectionModel().getSelectedCells().get(0); String str="ROW:"+Integer.toString(pos.getRow())+",COL:"+Integer.toString(pos.getColumn()); alert.setContentText(str); alert.show(); } else { // //显示选中的单行序号 // alert.setContentText(Integer.toString(tableView.getSelectionModel().getSelectedIndex())); // alert.show(); Person ps=(Person)tableView.getSelectionModel().getSelectedItem(); this.firstnameTF.setText(ps.getColData(0)); this.lastnameTF.setText(ps.getColData(1)); this.emailTF.setText(ps.getColData(2)); } }); tableView.selectionModelProperty().addListener( new ChangeListener(){ @Override public void changed(ObservableValue observable, Object oldValue, Object newValue) { alert.setContentText("选中的行是:"+tableView.getSelectionModel().getFocusedIndex()); alert.show(); } }); } @Override public void start(Stage primaryStage) throws Exception { toolbarAction(); primaryStage.setScene(new Scene(createContent(),400,300)); primaryStage.show(); } public static void main(String[] args){ launch(args); } public static class Person { private final LinkedHashMap<String,SimpleStringProperty> row=new LinkedHashMap(); protected List<String> colIds=null; private Callback< TableColumn.CellDataFeatures<Person,String>,ObservableValue<String>> mapCellValueFactory=new Callback< TableColumn.CellDataFeatures<Person,String>,ObservableValue<String>> (){ @Override public ObservableValue<String> call(TableColumn.CellDataFeatures<Person, String> param) { SimpleStringProperty rtn= param.getValue().getCol(param.getTableColumn().getId()); return rtn; } }; private Person(){}; public static Person create(String... ids){ Person p=new Person(); p.colIds=new ArrayList(); for(String id:ids){ p.colIds.add(id); } return p; } public Person copyStuct(){ Person p=new Person(); for (String id:this.colIds){ p.colIds.add(id); } return p; } public Person putNewRow(String... datas){ Person p=new Person(); p.colIds=this.colIds; for(int i=0;i<datas.length;i++){ p.row.put(colIds.get(i), new SimpleStringProperty(datas[i])); } return p; } public Person editRow(String... datas){ for(int i=0;i<datas.length;i++){ SimpleStringProperty p=row.get(colIds.get(i)); p.setValue(datas[i]); } return this; } public Person putColData(String col,String data){ row.put(col, new SimpleStringProperty(data)); return this; } public String getColData(int i){ String id=colIds.get(i); return row.get(id).getValue(); } public String getColId(int i){ String id=colIds.get(i); return id; } public String getColData(String colId){ return row.get(colId).getValue(); } public SimpleStringProperty getCol(String colId){ return row.get(colId); } public LinkedHashMap<String, SimpleStringProperty> getRow() { return row; } public Callback<TableColumn.CellDataFeatures<Person, String>, ObservableValue<String>> getMapCellValueFactory() { return mapCellValueFactory; } public List<String> getColIds() { return colIds; } } }
上一篇: asp.net导出EXCEL的功能代码
下一篇: java 之异步套接字编程实例(AIO)
推荐阅读
-
php怎样把数据库数据循环绑定到一个八行四列的表格里面去呢,知道的老师请说一下思路,谢谢
-
【JAVA】表格数据的存储(2)每一行使用javabean对象存储,多行使用放到map或者list中
-
javafx 2 的表格 绑定到 map
-
javafx 2 的表格 绑定到 map
-
php怎样把数据库数据循环绑定到一个八行四列的表格里面去呢,知道的老师请说一下思路,多谢
-
php怎样把数据库数据循环绑定到一个八行四列的表格里面去呢,知道的老师请说一下思路,谢谢
-
Json2Template.js 基于jquery的插件 绑定JavaScript对象到Html模板中_jquery
-
Json2Template.js 基于jquery的插件 绑定JavaScript对象到Html模板中_jquery