2014-01-14 55 views
2

我发现这些例子:展开表行单击

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

Can a table row expand and close?

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

+0

我回答了一个类似的问题[这里](http://stackoverflow.com/a/20198220/2855515)。它没有跨越,但也有在互联网上的代码。只是改变列的宽度会更容易,但看起来不太好。 – brian

回答

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

谁做了一个很棒的工作,将其他行添加到表中。

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

+0

您是否意外回应了错误的问题?这似乎与TableViews和行没有任何关系...... –

+0

完全没有办法,将它适配到tableView并允许扩展行是非常简单的。这只是一个更高级的版本,恰好适用于扩展阶段。我将使用表格完成一个完整的示例并将其张贴为编辑 – WillBD

+0

如果您认为可以对此代码进行修改,您能向我们展示一个快速示例吗? –