单击鼠标展开表行

时间:2014-01-14 14:32:30

标签: javafx javafx-2 javafx-8

我找到了这些例子:

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

Can a table row expand and close?

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

1 个答案:

答案 0 :(得分:0)

修改

因此,在使用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);
      }
    }
  }


}

Person.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

谁在向表中添加其他行方面做得非常出色。

再次,如果这根本不是你所需要的,请告诉我,我会尽力帮助你。