美文网首页
2018-04-19 javaFX的TableView 增删

2018-04-19 javaFX的TableView 增删

作者: 黑夜与繁星 | 来源:发表于2018-04-19 20:17 被阅读87次

    首先代码如下,创建工程复制以下代码

    import java.io.BufferedReader;
    import java.io.File;
    import java.io.FileReader;
    import javafx.application.Application;
    import javafx.beans.property.SimpleStringProperty;
    import javafx.collections.FXCollections;
    import javafx.collections.ObservableList;
    import javafx.event.ActionEvent;
    import javafx.geometry.Insets;
    import javafx.scene.Group;
    import javafx.scene.Scene;
    import javafx.scene.control.Button;
    import javafx.scene.control.RadioButton;
    import javafx.scene.control.TableCell;
    import javafx.scene.control.TableColumn;
    import javafx.scene.control.TableView;
    import javafx.scene.control.TextField;
    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 FXtable extends Application {
    
        private final TableView<Person> table = new TableView<>();
        private final ObservableList<Person> data
                = FXCollections.observableArrayList(
    //                    new Person("Jacob", "Smith"),
    //                    new Person("Isabella", "Johnson"),
    //                    new Person("Ethan", "Williams"),
    //                    new Person("Emma", "Jones"),
    //                    new Person("Michael", "Brown")
                );
        
        final HBox hb = new HBox();
        
        public static void main(String[] args) {
            launch(args);
        }
    
        public static class Person {
    
            private final SimpleStringProperty firstName;
            private final SimpleStringProperty lastName;
    
            private Person(String fName, String lName) {
                this.firstName = new SimpleStringProperty(fName);
                this.lastName = new SimpleStringProperty(lName);
            }
    
            public String getFirstName() {
                return firstName.get();
            }
    
            public void setFirstName(String fName) {
                firstName.set(fName);
            }
    
            public String getLastName() {
                return lastName.get();
            }
    
            public void setLastName(String fName) {
                lastName.set(fName);
            }
    
        }
    
        @Override
        public void start(Stage stage) throws Exception {
            //loaddata();
            
            File file = new File("C:\\Users\\micro\\eclipse-workspace\\app\\src\\app\\movie.txt");
            if(file.exists()) {
                FileReader fileReader=null;
                fileReader=new FileReader(file);
                BufferedReader bReader=new BufferedReader(fileReader);
                String line=bReader.readLine();
                String[] txtdata=null;//line.split(" ");
    
                while(line!=null) {
                    txtdata=line.split(" ");
                    //Mo person = new Person(fName, lName)
                    line=bReader.readLine();
                }
                
            }
            
            stage.setWidth(600);
            stage.setHeight(500);
    
            TableColumn firstNameCol = new TableColumn("First Name");
            firstNameCol.setCellValueFactory(new PropertyValueFactory<>("firstName"));
    
            TableColumn lastNameCol = new TableColumn("Last Name");
            lastNameCol.setCellValueFactory(new PropertyValueFactory<>("lastName"));
            
            TableColumn checkboxcol = new TableColumn("Discount Ticket");
            checkboxcol.setCellFactory(new PropertyValueFactory<>("check"));
            
            Callback<TableColumn<Person, String>, TableCell<Person, String>> cellFactory1
            = //
            new Callback<TableColumn<Person, String>, TableCell<Person, String>>() {
                @Override
                public TableCell call(final TableColumn<Person, String> param) {
                    final TableCell<Person, String> cell = new TableCell<Person, String>() {
            
                        final Button rb = new Button("Discount ticket");
                        
            
                        @Override
                        public void updateItem(String item, boolean empty) {
                            super.updateItem(item, empty);
                            if (empty) {
                                setGraphic(null);
                                setText(null);
                            } else {
                                rb.setOnAction(event -> {
                                    Person person = getTableView().getItems().get(getIndex());
                                    System.out.println(person.getFirstName()
                                            + "   " + person.getLastName());
                                });
                                setGraphic(rb);
                                setText(null);
                            }
                        }
                    };
                    return cell;
                }
            };
            
            checkboxcol.setCellFactory(cellFactory1);
    
            TableColumn actionCol = new TableColumn("Standard Ticket");
            actionCol.setCellValueFactory(new PropertyValueFactory<>("DUMMY"));
    
            Callback<TableColumn<Person, String>, TableCell<Person, String>> cellFactory
                    = //
                    new Callback<TableColumn<Person, String>, TableCell<Person, String>>() {
                @Override
                public TableCell call(final TableColumn<Person, String> param) {
                    final TableCell<Person, String> cell = new TableCell<Person, String>() {
    
                        final Button btn = new Button("Buy It");
    
                        @Override
                        public void updateItem(String item, boolean empty) {
                            super.updateItem(item, empty);
                            if (empty) {
                                setGraphic(null);
                                setText(null);
                            } else {
                                btn.setOnAction(event -> {
                                    Person person = getTableView().getItems().get(getIndex());
                                    System.out.println(person.getFirstName()
                                            + "   " + person.getLastName());
                                });
                                setGraphic(btn);
                                setText(null);
                            }
                        }
                    };
                    return cell;
                }
            };
    
            actionCol.setCellFactory(cellFactory);
            TableColumn DelCol = new TableColumn("Del");
            DelCol.setCellValueFactory(new PropertyValueFactory<>("Del"));
    
            Callback<TableColumn<Person, String>, TableCell<Person, String>> DcellFactory
                    = //
                    new Callback<TableColumn<Person, String>, TableCell<Person, String>>() {
                @Override
                public TableCell call(final TableColumn<Person, String> param) {
                    final TableCell<Person, String> cell = new TableCell<Person, String>() {
    
                        final Button btn = new Button("Del");
    
                        @Override
                        public void updateItem(String item, boolean empty) {
                            super.updateItem(item, empty);
                            if (empty) {
                                setGraphic(null);
                                setText(null);
                            } else {
                                btn.setOnAction(event -> {
                                    Person person =getTableView().getItems().get(getIndex());
                                    data.remove(person);
                                });
                                setGraphic(btn);
                                setText(null);
                            }
                        }
                    };
                    return cell;
                }
            };
    
            DelCol.setCellFactory(DcellFactory);
    
            table.setItems(data);
            table.getColumns().addAll(firstNameCol, lastNameCol,checkboxcol, actionCol,DelCol);
            
            final TextField addFirstName = new TextField();
            addFirstName.setPromptText("First Name");
            addFirstName.setMaxWidth(firstNameCol.getPrefWidth());
            final TextField addLastName = new TextField();
            addLastName.setMaxWidth(lastNameCol.getPrefWidth());
            addLastName.setPromptText("Last Name");
          
    
    
            final Button addButton = new Button("Add");
            addButton.setOnAction((ActionEvent e) -> {
                data.add(new Person(
                        addFirstName.getText(),
                        addLastName.getText()
                        ));
                addFirstName.clear();
                addLastName.clear();
    
            });
            
          
    
            hb.getChildren().addAll(addFirstName, addLastName, addButton);
            hb.setSpacing(3);
    
            final VBox vbox = new VBox();
            vbox.setSpacing(5);
            vbox.setPadding(new Insets(10, 0, 0, 10));
            vbox.getChildren().addAll(table, hb);
            
            Scene scene = new Scene(new Group());
    
            ((Group) scene.getRoot()).getChildren().addAll(vbox);
    
            stage.setScene(scene);
            stage.show();
            
        }
    
        private static void loaddata() throws Exception {
            File file = new File("C:\\Users\\micro\\eclipse-workspace\\app\\src\\app\\movie.txt");
            if(file.exists()) {
                FileReader fileReader=null;
                fileReader=new FileReader(file);
                BufferedReader bReader=new BufferedReader(fileReader);
                String line=bReader.readLine();
                String[] txtdata=null;//line.split(" ");
    
                while(line!=null) {
                    txtdata=line.split(" ");
                    
                    line=bReader.readLine();
                }
                
            }
            
        }
    
    

    原始界面:


    image.png

    添加一条数据后


    image.png

    点击Del 删除数据

    相关文章

      网友评论

          本文标题:2018-04-19 javaFX的TableView 增删

          本文链接:https://www.haomeiwen.com/subject/svdgkftx.html