【问题标题】:JavaFX: how to recognise current row in TableView?JavaFX:如何识别 TableView 中的当前行?
【发布时间】:2016-02-16 18:41:47
【问题描述】:

People Table - TableView Example

我在 JavaFX 中构建了一个 TableView,如图所示(人物表),其中包含一个名称列表。每次单击“详细信息”按钮时,如何将名字和姓氏打印到相应行的控制台?

(作为参考,我的最终目标和原始未回答的问题是使用详细信息按钮打开模板场景并使用该特定人员的数据填充它)。

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.control.cell.PropertyValueFactory;
import javafx.scene.image.Image;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.*;
import javafx.stage.*;
import javafx.util.Callback;

public class cutExample extends Application {
  public static void main(String[] args) { launch(args); }


  @Override public void start(final Stage stage) {
    stage.setTitle("People");

    // 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, String> firstNameCol = new TableColumn<>("First Name");
    firstNameCol.setCellValueFactory(new PropertyValueFactory("firstName"));
    TableColumn<Person, String> lastNameCol = new TableColumn<>("Last Name");
    lastNameCol.setCellValueFactory(new PropertyValueFactory("lastName"));
    TableColumn<Person, Boolean> actionCol = new TableColumn<>("Action");
    actionCol.setSortable(false);


    // create a cell value factory with a details 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(firstNameCol, lastNameCol, actionCol);
    table.setColumnResizePolicy(TableView.CONSTRAINED_RESIZE_POLICY);

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

  /** A table cell containing a button for details on each person. */
  private class AddPersonCell extends TableCell<Person, Boolean> {
    // a button for a specific person's details
    final Button details       = new Button("Details");
    // pads and centers the button in the cell.
    final StackPane paddedButton = new StackPane();


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


    }

    /** places an details 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);
      } else {
        setGraphic(null);
      }
    }
  }


}

【问题讨论】:

    标签: java user-interface javafx


    【解决方案1】:

    你可以使用下面sn-p的代码:

    actionCol.setCellFactory(column -> new TableCell<Person, Void>() {
    
            @Override
            protected void updateItem(Void item, boolean empty) {
                super.updateItem(item, empty);
                if (!empty) {
                    Button details = new Button("details");
                    details.setOnAction(e ->{Person person= (Person)getTableRow().getItem(); System.out.println(person.getFirstName()+", "+person.getLastName());});
                    setContentDisplay(ContentDisplay.GRAPHIC_ONLY);
                    setGraphic(details);
                } else {
                    setGraphic(null);
                }
    
            }
    
        });
    

    编辑:为避免强制转换,您可以使用:

     details.setOnAction(e ->{int  index = getTableRow().getIndex();
     Person person= getTableView().getItems().get(index);
    System.out.println(person.getFirstName()+", "+person.getLastName());});
    

    【讨论】:

      【解决方案2】:

      Person somePerson=table.getSelectionModel().getSelectedItem();

      【讨论】:

        猜你喜欢
        • 2015-03-26
        • 2015-07-12
        • 2011-03-17
        • 1970-01-01
        • 2012-12-28
        • 2014-04-29
        • 1970-01-01
        • 2014-12-23
        • 1970-01-01
        相关资源
        最近更新 更多