JavaFX:如何获得更多 "dummy/empty Rows"

标签 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;
    }

}

最佳答案

只需单击 TableView 上的任何其他位置,但确保距顶部至少 24 像素;这将起作用,因为您已将事件处理程序添加到 TableView...

如果您只想使用最后一行,则使用自定义 rowFactory 并处理其中的事件。

TableView 项添加一个占位符项,该占位符项标记用于添加新元素的行(由于某种原因,选择模型不喜欢 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
);

确保您的 TextFieldCellnull 值视为空行:

@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);
...

确保第一列不显示占位符的任何内容

//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();
});

并使用以下rowFactory来处理添加项目(除非您需要向TableRow添加样式类,否则不需要updateItem部分;在这种情况下,您不需要扩展 TableRow)

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);
        }
    }
});

关于JavaFX:如何获得更多 "dummy/empty Rows",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36646676/

相关文章:

java - 运行从 springboot 项目打包的 jar 时无法获取 bean

java - JUnit 测试器类在没有 main 方法的情况下运行。这是如何完成/可能的?

java - 如何在 IntelliJ IDEA 中创建 JavaFX Maven 项目?

java - 如何在 ComboBox ListView 中显示动态变化的文本

java - 为什么将 TextField 绑定(bind)到正在另一个线程上更新的属性最终会导致应用程序抛出错误?

javascript - 单击钛合金中的 tableviewrow 项目时更新标签文本

java - 如何在linux中用C++代码调用java -classpath

java - 无法使用 Oracle 数据库连接在 Spring 中加载配置类

xcode - 滚动时在 TableViewCell 内的 Stepper 中重复值

ios - 如何在 tableView 上反射(reflect) childChanges - IOS Swift Firebase