javafx - 单击鼠标展开表格行

标签 javafx javafx-2 javafx-8

我找到了这些例子:

http://www.jeasyui.com/tutorial/datagrid/datagrid21.php\

Can a table row expand and close?

基本上我想创建一个 JavaFX 表,我可以扩展它以查看更多数据。有没有用JavaFX写的类似的例子?

最佳答案

编辑

所以,在用 tableView 细节重新解决问题之后,我(有点)快速地拼凑了这个例子。请记住,我没有使用原始答案中提到的动画,尽管它很容易适应,而且我根本没有完全复制提供的示例,因为老实说,我没有时间。但这提供了基本的 Accordion 感觉,您只需要花时间摆弄不同字段的各种宽度和高度属性即可获得真正的效果。 (在处理程序中,您甚至可能想要插入一行,其中第一列具有巨大的宽度和一个嵌套的 TableView ,以实现他们正在做的事情)。同样,这是 1 列,它显示了添加一些关于扩展的附加信息的基础知识,您可以根据需要使用它:

fileChooserExample.java: 

package filechooserexample;

import javafx.application.Application;
import javafx.beans.property.*;
import javafx.beans.value.ObservableValue;
import javafx.collections.FXCollections;
import javafx.event.*;
import javafx.geometry.*;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.*;
import javafx.scene.paint.Color;
import javafx.stage.*;
import javafx.util.Callback;

public class FileChooserExample extends Application {
  public static void main(String[] args) { launch(args); }
  @Override public void start(final Stage stage) {
    stage.setTitle("People");
//    stage.getIcons().add(new Image("http://icons.iconarchive.com/icons/icons-land/vista-people/72/Historical-Viking-Female-icon.png"));  // icon license: Linkware (Backlink to http://www.icons-land.com required)

    // create a table.
    final TableView<Person> table = new TableView<>(
      FXCollections.observableArrayList(
        new Person("Jacob", "Smith"),
        new Person("Isabella", "Johnson"),
        new Person("Ethan", "Williams"),
        new Person("Emma", "Jones"),
        new Person("Michael", "Brown")
      )
    );

    // define the table columns.

    TableColumn<Person, Boolean> actionCol = new TableColumn<>("Action");
    actionCol.setSortable(false);

     actionCol.setPrefWidth(1000);
    // define a simple boolean cell value for the action column so that the column will only be shown for non-empty rows.
    actionCol.setCellValueFactory(new Callback<TableColumn.CellDataFeatures<Person, Boolean>, ObservableValue<Boolean>>() {
      @Override public ObservableValue<Boolean> call(TableColumn.CellDataFeatures<Person, Boolean> features) {
        return new SimpleBooleanProperty(features.getValue() != null);
      }
    });

    // create a cell value factory with an add button for each row in the table.
    actionCol.setCellFactory(new Callback<TableColumn<Person, Boolean>, TableCell<Person, Boolean>>() {
      @Override public TableCell<Person, Boolean> call(TableColumn<Person, Boolean> personBooleanTableColumn) {
        return new AddPersonCell(stage, table);
      }
    });

    table.getColumns().setAll(actionCol);
    table.setColumnResizePolicy(TableView.UNCONSTRAINED_RESIZE_POLICY);

    stage.setScene(new Scene(table));
    stage.show();
  }

  /** A table cell containing a button for adding a new person. */
  private class AddPersonCell extends TableCell<Person, Boolean> {
    // a button for adding a new person.
    final Button addButton       = new Button("Add");
    // pads and centers the add button in the cell.
    final VBox paddedButton = new VBox();
    final HBox mainHolder = new HBox();
    // records the y pos of the last button press so that the add person dialog can be shown next to the cell.
    final DoubleProperty buttonY = new SimpleDoubleProperty();

    /**
     * AddPersonCell constructor
     * @param stage the stage in which the table is placed.
     * @param table the table to which a new person can be added.
     */
    AddPersonCell(final Stage stage, final TableView table) {
      paddedButton.setPadding(new Insets(3));
      paddedButton.getChildren().add(addButton);
      mainHolder.getChildren().add(paddedButton);
      addButton.setOnMousePressed(new EventHandler<MouseEvent>() {
        @Override public void handle(MouseEvent mouseEvent) {
          buttonY.set(mouseEvent.getScreenY());
          if (getTableRow().getPrefHeight() == 100){
              getTableRow().setPrefHeight(35);
              paddedButton.getChildren().remove(1);
              getTableRow().autosize();
          }
          else{
            getTableRow().setPrefHeight(100);
            Label myLabel = new Label();
            myLabel.setText("This is new label text!");
            myLabel.setTextFill(Color.BLACK);
            paddedButton.getChildren().add(myLabel);
            getTableRow().autosize();
          }
        }
      });
      addButton.setOnAction(new EventHandler<ActionEvent>() {
        @Override public void handle(ActionEvent actionEvent) {
          table.getSelectionModel().select(getTableRow().getIndex());
        }
      });
    }

    /** places an add button in the row only if the row is not empty. */
    @Override protected void updateItem(Boolean item, boolean empty) {
      super.updateItem(item, empty);
      if (!empty) {
        setContentDisplay(ContentDisplay.GRAPHIC_ONLY);
        setGraphic(paddedButton);
      }
    }
  }


}

人.java:

package filechooserexample;
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.property.StringProperty;
public class Person {
  private StringProperty firstName;
  private StringProperty lastName;

  public Person(String firstName, String lastName) {
    setFirstName(firstName);
    setLastName(lastName);
  }

  public final void setFirstName(String value) { firstNameProperty().set(value); }
  public final void setLastName(String value) { lastNameProperty().set(value); }
  public String getFirstName() { return firstNameProperty().get(); }
  public String getLastName() { return lastNameProperty().get(); }

  public StringProperty firstNameProperty() {
    if (firstName == null) firstName = new SimpleStringProperty(this, "firstName");
    return firstName;
  }
  public StringProperty lastNameProperty() {
    if (lastName == null) lastName = new SimpleStringProperty(this, "lastName");
    return lastName;
  }
}

再一次,请原谅添加带有命名列的各种按钮的看似黑客行为,但这里非常忙,所以我借用了主表结构:

original SO table dynamic row addition question

谁在向表格中添加额外的行方面做得非常出色。

再次强调,如果这根本不是您需要的,请告诉我,我会尽力提供帮助。

关于javafx - 单击鼠标展开表格行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21116129/

相关文章:

java - Gradle JavaFX - java.lang.NullPointerException : Location is required

java - 两个重载的方法,相同的 Action ,导致重复代码

java - 如何缩放和设置MediaView的坐标?

java - 如何在 JAVA FX 2.2 中重新绘制窗口(舞台)

JavaFX,如果有人选中 Tableview 中的复选框,如何触发事件

JavaFX 8 : How to remove the last white pixel from the combobox list

java - 在 javafxports android 项目中查找配置文件时出现问题

java - 如何从javafx中的tableview获取复选框的选定索引

java - 形状的 CSS 不适用于 javafx 中的文本区域

java - 如何修改 JavaFX 中的 ChoiceBox 复选标记?