Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8097102

Context Menus overlap in TableView's TextFieldTableCell

    XMLWordPrintable

    Details

      Description

      The below code creates a TableView with two columns. The cells of the second column are editable (uses a TextFieldTableCell).
      The TextFieldTableCells has a context menu defined.

      Double-click on any cell in the second column, the textfield will appear. Right-click inside the textfield, you will get two context menus that overlap each others (one from the textfieldCell and one from the textfield).

      This is a very minor issue, however a more expected behavior would be either to:
      1- See a context menu that combines both actions.
      2- See one of the two menus. In this case, the right-click event should be consumed by one of the components and not reach the other.





      package test;

      import javafx.application.Application;

      import javafx.beans.property.SimpleStringProperty;
      import javafx.beans.property.StringProperty;
      import javafx.beans.value.ObservableValue;


      import javafx.scene.Parent;
      import javafx.scene.Scene;
      import javafx.scene.control.ContextMenu;
      import javafx.scene.control.MenuItem;
      import javafx.scene.control.SelectionMode;
      import javafx.scene.control.TableCell;
      import javafx.scene.control.TableColumn;
      import javafx.scene.control.TableColumn.CellDataFeatures;
      import javafx.scene.control.TableView;
      import javafx.scene.control.cell.TextFieldTableCell;
      import javafx.scene.layout.VBox;

      import javafx.stage.Stage;

      import javafx.util.Callback;

      public class ContextMenuTest extends Application {
      private TableView<File> table;

      public ContextMenuTest() {
      }

      public static void main(String[] args) {
      launch(args);
      }

      @Override
      public void start(Stage stage) {
      try {
      Scene scene = new Scene(createRegion());
      stage.setScene(scene);
      stage.setTitle("Tester App");
      stage.show();

      } catch (Exception e) {
      e.printStackTrace();
      }

      }

      private Parent createRegion() {

      VBox vBox = new VBox();
      table = new TableView<File>();
      table.getSelectionModel().setSelectionMode(SelectionMode.MULTIPLE);


      TableColumn<File, String> firstNameCol = new TableColumn<File, String>(
      "Name");
      firstNameCol
      .setCellValueFactory(new Callback<TableColumn.CellDataFeatures<File, String>, ObservableValue<String>>() {

      @Override
      public ObservableValue<String> call(
      CellDataFeatures<File, String> arg0) {
      return arg0.getValue().nameProperty();
      }
      });

      TableColumn<File, String> valueCol = new TableColumn<File, String>(
      "Value");
      valueCol.setCellValueFactory(new Callback<TableColumn.CellDataFeatures<File, String>, ObservableValue<String>>() {

      @Override
      public ObservableValue<String> call(
      CellDataFeatures<File, String> arg0) {
      return arg0.getValue().valueProperty();
      }
      });
      valueCol.setCellFactory(new Callback<TableColumn<File, String>, TableCell<File, String>>() {

      @Override
      public TableCell<File, String> call(TableColumn<File, String> arg0) {
      TextFieldTableCell<File, String> cell = new TextFieldTableCell<>();

      ContextMenu contextMenu = new ContextMenu();
      cell.setContextMenu(contextMenu);
      contextMenu.getItems().add(new MenuItem("Hello2"));
      contextMenu.getItems().add(new MenuItem("World2"));
      return cell;
      }
      });

      valueCol.setEditable(true);
      table.setEditable(true);

      table.getColumns().setAll(firstNameCol, valueCol);
      table.setColumnResizePolicy(table.CONSTRAINED_RESIZE_POLICY);

      table.getItems().add(new File("test1", "value0", 1.0));
      table.getItems().add(new File("hello", "value1", 3.0));
      table.getItems().add(new File("hello", "value2", 3.0));

      vBox.getChildren().addAll(table);
      return vBox;
      }

      public class File {
      private StringProperty name;
      private StringProperty value;

      public File(String string) {
      nameProperty().set(string);
      }

      public File(String string, String value, Double modified) {
      nameProperty().set(string);
      valueProperty().set(value);
      }

      public void setName(String value) {
      nameProperty().set(value);
      }

      public String getName() {
      return nameProperty().get();
      }

      public StringProperty nameProperty() {
      if (name == null) {
      name = new SimpleStringProperty(this, "name");
      }
      return name;
      }

      public void setValue(String value) {
      valueProperty().set(value);
      }

      public String getValue() {
      return valueProperty().get();
      }

      public StringProperty valueProperty() {
      if (value == null) {
      value = new SimpleStringProperty(this, "value");
      }
      return value;
      }

      }

      }


        Attachments

          Issue Links

            Activity

              People

              Assignee:
              jgiles Jonathan Giles
              Reporter:
              rashajfx rasha (Inactive)
              Votes:
              0 Vote for this issue
              Watchers:
              2 Start watching this issue

                Dates

                Created:
                Updated:
                Resolved:
                Imported: