Javafx 鼠标单击展开表格行

Javafx 鼠标单击展开表格行,javafx,javafx-2,javafx-8,Javafx,Javafx 2,Javafx 8,我发现这些例子: \ 基本上我想创建一个JavaFX表,我可以扩展它以查看更多数据。有没有用JavaFX编写的类似示例?EDIT 所以,在用tableView的细节重新处理了这个问题之后,我(有点)很快就把这个例子拼凑起来了。请记住,我没有使用原始答案中提到的动画,尽管它很容易适应,而且我根本没有完全复制提供的示例,因为老实说,我没有时间。但这会给人一种基本的手风琴的感觉,你只需要花些时间来摆弄不同区域的不同宽度和高度属性,就可以实现真正的手风琴效果。(在处理程序中,您甚至可能希望插入一行,其

我发现这些例子:

\

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

EDIT

所以,在用tableView的细节重新处理了这个问题之后,我(有点)很快就把这个例子拼凑起来了。请记住,我没有使用原始答案中提到的动画,尽管它很容易适应,而且我根本没有完全复制提供的示例,因为老实说,我没有时间。但这会给人一种基本的手风琴的感觉,你只需要花些时间来摆弄不同区域的不同宽度和高度属性,就可以实现真正的手风琴效果。(在处理程序中,您甚至可能希望插入一行,其中第一列具有巨大的宽度和嵌套的表视图,以实现某种程度上的精确操作)。同样,这是一列,它显示了添加一些扩展信息的基础知识,您可以根据自己的需要:

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


}
再一次,请原谅添加带有命名列的各种按钮,这些按钮不起任何作用,这里非常繁忙,因此我借用了以下主表结构:

他在向表中添加额外行方面做得非常出色


再说一次,如果这根本不是你需要的,请让我知道,我会尽力帮助你

你是否意外地回答了错误的问题?这似乎与tableView和rows没有任何关系……一点也没有,将其应用于tableView并允许行的扩展是非常简单的。这只是一个更高级别的版本,正好解决了阶段的扩展问题。我将使用表格制作一个完整的示例,并将其作为一个编辑器发布。如果您认为此代码可以修改,您可以给我们展示一个快速的示例吗?据我所知,问题是如何在表格行中添加额外的内容,而不是如何设置该内容的显示动画(我想动画是一个不错的功能,但它似乎相对容易…).我对这个问题的解释正确吗?嗯,我想我可能误解了,我认为他遇到的问题是动态地改变表格行的大小,当点击按钮时会出现“手风琴式”的扩展,这就是上面例子的动机所在,因为你会附加那种类型的动画(如果不关心动画,则使用0计时器)发送到单元格中按钮的侦听器,或imageview上的侦听器,从而提供所需的扩展以允许添加其他内容。可能我误解了,如果我这样做,我会很高兴地删除我的答案:)我回答了一个类似的问题。它没有跨度,但在互联网上也有相应的代码。只改变列宽会更容易,但看起来可能没有那么好。
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;
  }
}