JavaFX:使Data类更简洁的技术

时间:2017-03-12 04:17:07

标签: java database javafx

我正在使用JavaFX创建一个简单的数据库应用程序,但是我无法找到一种方法来使Data类更简洁,同时仍然保留Add Button的功能。

有没有人知道简化Data类的好方法,并保留Add Button,以便用户可以输入自己的数据?

import javafx.application.Application;
import javafx.beans.property.SimpleStringProperty;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableColumn.CellEditEvent;
import javafx.scene.control.TableView;
import javafx.scene.control.TextField;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.control.cell.TextFieldTableCell;  
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.scene.text.Font;
import javafx.stage.Stage;

public class DB extends Application {

    private final TableView<Data> table = new TableView<>();
    private final ObservableList<Data> data =
            FXCollections.observableArrayList(
            new Data("one", "one", "one", "one", "one", "one", "one", "one"));
    private TableColumn<Data, String> [] tableCol;
    private TextField [] textField;
    private Button btn;
    private HBox hbox;
    private VBox vbox;

    public static void main(String[] args) {
        launch(args);
    }

    @Override
    public void start(Stage stage) {
        Scene scene = new Scene(new Group());
        stage.setTitle("Custom DB");
        stage.setMaximized(true);
        table.setEditable(true);

        setTableCol();
        setCellValue();
        addToTable();
        setTextField();
        setBtn();

        addToHBox();
        addToVBox();

        ((Group) scene.getRoot()).getChildren().addAll(vbox);

        stage.setScene(scene);
        stage.show();
    }

    private void setTableCol()
    {   
        tableCol = new TableColumn[8];
        for(int i = 0; i<8; i++) {
            tableCol[i] = new TableColumn();
            tableCol[i].setMinWidth(100);
            tableCol[i].setGraphic(new TextField("x"));
        }
    }

    private void setCellValue()
    {   
        tableCol[0].setCellValueFactory( new PropertyValueFactory<Data, String>("one"));
        tableCol[1].setCellValueFactory( new PropertyValueFactory<Data, String>("two"));
        tableCol[2].setCellValueFactory( new PropertyValueFactory<Data, String>("three"));
        tableCol[3].setCellValueFactory( new PropertyValueFactory<Data, String>("four"));
        tableCol[4].setCellValueFactory( new PropertyValueFactory<Data, String>("five"));
        tableCol[5].setCellValueFactory( new PropertyValueFactory<Data, String>("six"));
        tableCol[6].setCellValueFactory( new PropertyValueFactory<Data, String>("seven"));
        tableCol[7].setCellValueFactory( new PropertyValueFactory<Data, String>("eight"));
    }

    private void addToTable()
    {
        table.setItems(data);
        for(int i = 0; i<8; i++)
        table.getColumns().addAll(tableCol[i]); 
    }

    private void setTextField()
    {
        textField = new TextField[8];
        for(int i = 0; i<8; i++) {
            textField[i] = new TextField();
            textField[i].setPromptText("Enter");
            textField[i].setMaxWidth(tableCol[i].getPrefWidth());
        }
    }

    private void setBtn()
    {
        btn = new Button("Add Data");
        btn.setOnAction(new EventHandler<ActionEvent>() {
            @Override
            public void handle(ActionEvent e) {
                data.add(new Data(
                        textField[0].getText(),
                        textField[1].getText(),
                        textField[2].getText(),
                        textField[3].getText(),
                        textField[4].getText(),
                        textField[5].getText(),
                        textField[6].getText(),
                        textField[7].getText()));
            }
        });
    }

    private void addToHBox()
    {
        hbox = new HBox();
        hbox.setSpacing(20);
        for(int i = 0; i<8; i++)
        hbox.getChildren().addAll(textField[i]);
        hbox.getChildren().addAll(btn);
    }

    private void addToVBox()
    {   
        vbox = new VBox();
        vbox.setSpacing(5);
        vbox.setPadding(new Insets(10, 0, 0, 10));
        vbox.getChildren().addAll(table, hbox);
    }

    public static class Data {

        private final SimpleStringProperty one;
        private final SimpleStringProperty two;
        private final SimpleStringProperty three;
        private final SimpleStringProperty four;
        private final SimpleStringProperty five;
        private final SimpleStringProperty six;
        private final SimpleStringProperty seven;
        private final SimpleStringProperty eight;

        private Data(String a, String b, String c, String d, String e, String f, String g, String h)
        {
            this.one = new SimpleStringProperty(a);
            this.two = new SimpleStringProperty(b);
            this.three = new SimpleStringProperty(c);
            this.four = new SimpleStringProperty(d);
            this.five = new SimpleStringProperty(e);
            this.six = new SimpleStringProperty(f);
            this.seven = new SimpleStringProperty(g);
            this.eight = new SimpleStringProperty(h);
        }

        public String getOne()
        {
            return one.get();
        }

        public void setOne(String fName)
        {
            one.set(fName);
        }

        public String getTwo()
        {
            return two.get();
        }

        public void setTwo(String fName) {
            two.set(fName);
        }

        public String getThree()
        {
            return three.get();
        }

        public void setThree(String fName)
        {
            three.set(fName);
        }

        public String getFour()
        {
            return four.get();
        }

        public void setFour(String fName)
        {
            four.set(fName);
        }

        public String getFive()
        {
            return five.get();
        }

        public void setFive(String fName)
        {
            five.set(fName);
        }

        public String getSix()
        {
            return six.get();
        }

        public void setSix(String fName)
        {
            six.set(fName);
        }

        public String getSeven()
        {
            return seven.get();
        }

        public void setSeven(String fName)
        {
            seven.set(fName);
        }

        public String getEight()
        {
            return eight.get();
        }

        public void setEight(String fName)
        {
            eight.set(fName);
        }
    }
}

0 个答案:

没有答案