JavaFX:如何获得更多“虚拟/空行”

时间:2016-04-15 12:05:25

标签: java javafx tableview

我有一个Javafx TableView,我可以通过双击添加新行在我的“已填充”/文本字段填充行的末尾单击一个空行。 我的问题是,如果我添加一些行,Java不会给我更多的空行我可以双击添加一些行。 编辑:删除了一些不确定的日志

看看我的意思,这是代码:

import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.property.StringProperty;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.scene.control.ContentDisplay;
import javafx.scene.control.TableCell;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TextArea;
import javafx.util.Callback;

import javafx.application.Application;
import static javafx.application.Application.launch;
import javafx.beans.property.IntegerProperty;
import javafx.beans.property.SimpleIntegerProperty;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TableView;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.input.MouseButton;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.HBox;
import javafx.stage.Stage;


interface inside_table
{
    public void Select_Row_by_Col(int index);
}

public class Supermain extends Application {
    ObservableList<myTextRow> data;

    @Override
    public void start(Stage primaryStage) {

        ArrayList myindizes=new ArrayList();



        final TableView<myTextRow> table = new TableView<>();
        table.setEditable(true);
        table.setStyle("-fx-text-wrap: true;");

        //Table columns
        TableColumn<myTextRow, String> clmID = new TableColumn<>("ID");
        clmID.setMinWidth(160);
        clmID.setCellValueFactory(new PropertyValueFactory<>("ID"));

        TableColumn<myTextRow, String> clmtext = new TableColumn<>("Text");
        clmtext.setMinWidth(160);
        clmtext.setCellValueFactory(new PropertyValueFactory<>("text"));
        clmtext.setCellFactory(new TextFieldCellFactory("text"));

        TableColumn<myTextRow, String> clmtext2 = new TableColumn<>("Text2");
        clmtext2.setMinWidth(160);
        clmtext2.setCellValueFactory(new PropertyValueFactory<>("text2"));
        clmtext2.setCellFactory(new TextFieldCellFactory("text2"));

        //Add data
        data = FXCollections.observableArrayList(
                new myTextRow(5, "Lorem","bla"),
                new myTextRow(2, "Ipsum","bla")
        );

        table.getColumns().addAll(clmID, clmtext,clmtext2);
        table.setItems(data);

        table.setOnMouseClicked(new EventHandler<MouseEvent>() {
            @Override
            public void handle(MouseEvent mouseEvent) {
                if (mouseEvent.getButton().equals(MouseButton.PRIMARY)) {
                    if (mouseEvent.getClickCount() == 2 && mouseEvent.getY()>24) {
                        data.add(new myTextRow(td_get_biggest_ID() + 1,"",""));  
                        table.selectionModelProperty().get().select(data.size()-1);
                    }
                }
            }
        });

        HBox hBox = new HBox();
        hBox.setSpacing(5.0);
        hBox.setPadding(new Insets(5, 5, 5, 5));

        Button btn = new Button();
        btn.setText("Get Data");
        btn.setOnAction(new EventHandler<ActionEvent>() {

            @Override
            public void handle(ActionEvent event) {
                for (myTextRow data1 : data) {
                    System.out.println("data:" + data1.getText2());
                }
            }
        });

        hBox.getChildren().add(btn);

        BorderPane pane = new BorderPane();
        pane.setTop(hBox);
        pane.setCenter(table);
        primaryStage.setScene(new Scene(pane, 640, 480));
        primaryStage.show();

        class I_table implements inside_table{

            @Override
            public void Select_Row_by_Col(int index) {
                table.getSelectionModel().select(index);
            }

        }


    }

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        launch(args);
    }


    public static class TextFieldCellFactory
            implements Callback<TableColumn<myTextRow, String>, TableCell<myTextRow, String>> {

        private String ColumnName;

        public TextFieldCellFactory(String ColumnName){
        this.ColumnName=ColumnName;
        }

        @Override
        public TableCell<myTextRow, String> call(TableColumn<myTextRow, String> param) {
            TextFieldCell textFieldCell = new TextFieldCell(this.ColumnName);
            return textFieldCell;

        }

        public static class TextFieldCell extends TableCell<myTextRow, String> {

            private TextArea textField;
            private StringProperty boundToCurrently = null;
            private String last_text;
            private String ColumnName;


            public TextFieldCell(String cname) {

                textField = new TextArea();
                textField.setWrapText(true);
                textField.setMinWidth(this.getWidth() - this.getGraphicTextGap() * 2);
                last_text="";
                this.ColumnName=cname;



                this.setGraphic(textField);

                textField.focusedProperty().addListener((obs, wasFocused, isNowFocused) -> { 
                    if(this.ColumnName=="text2"){
                        if(isNowFocused){last_text=textField.getText();System.out.println("NOW focus "+last_text);}
                        if (! isNowFocused && ! isValid(textField.getText())) { 
                            textField.setText(last_text);
                            //textField.setText("00:00:00:00");
                            textField.selectAll();
                            System.out.println("blur");

                        }
                    }

                });

            }

            @Override
            protected void updateItem(String item, boolean empty) {
                super.updateItem(item, empty);
                if (!empty) {
                    // Show the Text Field
                    this.setContentDisplay(ContentDisplay.GRAPHIC_ONLY);


                   // myindizes.add(getIndex());

                    // Retrieve the actual String Property that should be bound to the TextField
                    // If the TextField is currently bound to a different StringProperty
                    // Unbind the old property and rebind to the new one
                    ObservableValue<String> ov = getTableColumn().getCellObservableValue(getIndex());
                    SimpleStringProperty sp = (SimpleStringProperty) ov;

                    if (this.boundToCurrently == null) {
                        this.boundToCurrently = sp;
                        this.textField.textProperty().bindBidirectional(sp);
                    } else if (this.boundToCurrently != sp) {
                        this.textField.textProperty().unbindBidirectional(this.boundToCurrently);
                        this.boundToCurrently = sp;
                        this.textField.textProperty().bindBidirectional(this.boundToCurrently);
                    }

                    double height = real_lines_height(textField.getText(), this.getWidth(), 30, 22);
                    textField.setPrefHeight(height);
                    textField.setMaxHeight(height);

                    textField.setMaxHeight(Double.MAX_VALUE);
                    // if height bigger than the biggest height in the row
                    //-> change all heights of the row(textfields ()typeof textarea) to this height
                    // else leave the height as it is


                    //System.out.println("item=" + item + " ObservableValue<String>=" + ov.getValue());
                    //this.textField.setText(item);  // No longer need this!!!
                } else {
                    this.setContentDisplay(ContentDisplay.TEXT_ONLY);
                }
            }//update

            private boolean isValid(String s){

                String splitArray[] = s.split(":");
                if (splitArray.length != 4) {
                    System.out.println("false");
                    return false;
                }

                for (int i = 0; i < splitArray.length; i++) {

                    if (splitArray[i].length() != 2) {
                        System.out.println("false");
                        return false;
                    }
                    if (!splitArray[i].substring(0, 1).matches("[0-9]")) {
                        System.out.println("no number1");
                        return false;
                    }
                    if (!splitArray[i].substring(1, 2).matches("[0-9]")) {
                        System.out.println("no number2");
                        return false;
                    }

                    if (i < 3) {
                        int itest = Integer.parseInt(splitArray[i]);
                        if (itest > 59) {
                            System.out.println(itest + " ist zu groß!");
                            return false;
                        }
                    } else {
                        int itest2 = Integer.parseInt(splitArray[i]);
                        if (itest2 > Math.floor(25)) {
                            System.out.println(itest2 + " ist zu groß!");
                            return false;
                        }
                        //framerate!!!!!
                    }

                    System.out.println("splits: " + splitArray[i]);
                    //if( el.charAt(0).)
                }

                return true;

            }

        }

    }

    public class myTextRow {

        private final SimpleIntegerProperty ID;

        private final SimpleStringProperty text;
        private final SimpleStringProperty text2;

        public myTextRow(int ID, String text,String text2) {

            this.ID = new SimpleIntegerProperty(ID);
            this.text = new SimpleStringProperty(text);
            this.text2 = new SimpleStringProperty(text2);


        }


        //setter
        public void setID(int id) {
            this.ID.set(id);
        }

        public void setText(String text) {
            this.text.set(text);
        }

        public void setText2(String text) {         
            this.text2.set(text);         
        }

       //getter
        public int getID() {
            return ID.get();
        }

        public String getText() {
            return text.get();
        }

        public String getText2() {
            return text2.get();
        }

        //properties
        public StringProperty textProperty() {
        return text;

        }

        public StringProperty text2Property() {
        return text2;

        }

    public IntegerProperty IDProperty() {
        return ID;
    }

    }

    private static double real_lines_height(String s, double width, double heightCorrector, double widthCorrector) {
        HBox h = new HBox();
        Label l = new Label("Text");
        h.getChildren().add(l);
        Scene sc = new Scene(h);
        l.applyCss();
        double line_height = l.prefHeight(-1);

        int new_lines = s.replaceAll("[^\r\n|\r|\n]", "").length();
        //  System.out.println("new lines= "+new_lines);
        String[] lines = s.split("\r\n|\r|\n");
        //  System.out.println("line count func= "+ lines.length);
        int count = 0;
        //double rest=0;
        for (int i = 0; i < lines.length; i++) {
            double text_width = get_text_width(lines[i]);
            double plus_lines = Math.ceil(text_width / (width - widthCorrector));
            if (plus_lines > 1) {
                count += plus_lines;
                //rest+= (text_width / (width-widthCorrector)) - plus_lines;
            } else {
                count += 1;
            }

        }
        //count+=(int) Math.ceil(rest);
        count += new_lines - lines.length;

        return count * line_height + heightCorrector;
    }

    private static double get_text_width(String s) {
        HBox h = new HBox();
        Label l = new Label(s);
        l.setWrapText(false);
        h.getChildren().add(l);
        Scene sc = new Scene(h);
        l.applyCss();
        // System.out.println("FXMLDocumentController.get_text_width(): "+l.prefWidth(-1));
        return l.prefWidth(-1);

    }
    public int td_get_biggest_ID() {
        int biggest = 0;
        for (int i = 0; i < data.size(); i++) {
            if (((myTextRow) data.get(i)).getID() > biggest) {
                biggest = ((myTextRow) data.get(i)).getID();
            }
        }
        return biggest;
    }

}

1 个答案:

答案 0 :(得分:1)

Just click anywhere else on the TableView but make sure it's at least 24 pixels from the top; This will work since you've added the event handler is added to the TableView...

If you only want to use the last row, then use a custom rowFactory and handle the events there.

Add a placeholder item to the TableView items that marks the row that is used for adding new elements (for some reason the selection model doesn't like null):

final myTextRow addPlaceHolder = new myTextRow(Integer.MIN_VALUE, null, null);

...

//Add data
data = FXCollections.observableArrayList(
        new myTextRow(5, "Lorem", "bla"),
        new myTextRow(2, "Ipsum", "bla"),
        addPlaceHolder
);

make sure your TextFieldCells treat null values as empty rows:

@Override
protected void updateItem(String item, boolean empty) {
    super.updateItem(item, empty);
    if (!empty && item != null) {
        // Show the Text Field
        this.setContentDisplay(ContentDisplay.GRAPHIC_ONLY);
...

make sure the first column does not display anything for the placeholder

//Table columns
TableColumn<myTextRow, Number> clmID = new TableColumn<>("ID");
clmID.setMinWidth(160);
clmID.setCellValueFactory(cdf -> {
    myTextRow item = cdf.getValue();
    return item == addPlaceHolder ? Bindings.createObjectBinding(() -> null) : item.IDProperty();
});

and use the following rowFactory to handle adding the items (you don't need the updateItem part unless you need to add a style class to the TableRow; you need not extend TableRow in this case)

table.setRowFactory(tv -> new TableRow<myTextRow>() {
    {
        setOnMouseClicked(mouseEvent -> {
            if (mouseEvent.getButton() == MouseButton.PRIMARY
                    && mouseEvent.getClickCount() == 2
                    && !isEmpty()
                    && getItem() == addPlaceHolder) {
                data.add(data.size() - 1, new myTextRow(td_get_biggest_ID() + 1, "", ""));
                table.selectionModelProperty().get().select(data.size() - 1);
                mouseEvent.consume();
            }
        });
    }

    @Override
    protected void updateItem(myTextRow item, boolean empty) {
        super.updateItem(item, empty);

        // add style class for row containing addPlaceHolder
        List<String> classes = getStyleClass();
        final String clazz = "add-row";

        if (item == addPlaceHolder) {
            if (!classes.contains(clazz)) {
                classes.add(clazz);
            }
        } else {
            classes.remove(clazz);
        }
    }
});