Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8164537
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T19:20:20+00:00 2026-06-06T19:20:20+00:00

I want to add a context menu to the tableview, more specifically I want

  • 0

I want to add a context menu to the tableview, more specifically I want to display context menu on table rows on a right click.

I tried with this code

 final EventHandler click = new EventHandler() {
           public void handle(MouseEvent t) {

           }

                @Override
                public void handle(Event arg0) {
                }
     };
            final Context menu = new ContextMenu();
     MenuItem item = new MenuItem("Add Image");
     item.setOnAction(new EventHandler() {
          public void handle(ActionEvent t) 
          {
           }
                @Override
                public void handle(Event arg0)
                {
                  //some code here   
          }});
     menu.getItems().addAll(item);
     EditingCell cellFactory = new EditingCell(click,menu); 
      TableColumn col = new TableColumn("column1");
     col.setCellFactory(cellFactory);

Above code is working correctly in JAVAFX 2.0, and I am getting a context menu on right click, How ever when I run my code with JAVAFX 2.1 Context menu doesnt work.

I tried to get context menu’s showing property using showingProperty() method and it is returning false in JAVAFX 2.1 and true in JAVAFX 2.0

Further I also tried making my custom cellfactory class and here is my code (I took it from official JAVAFX TreeView context menu example)

import javafx.event.Event;
import javafx.event.EventHandler;
import javafx.scene.control.*;
import javafx.scene.input.KeyCode;
import javafx.scene.input.KeyEvent;

class TextFieldTreeCellImpl extends TableCell<String,String> {

    private TextField textField;
    private ContextMenu addMenu = new ContextMenu();

    public TextFieldTreeCellImpl() {
        MenuItem addMenuItem = new MenuItem("Add Employee");
        addMenu.getItems().add(addMenuItem);
        addMenuItem.setOnAction(new EventHandler() {
            public void handle(Event t) {

            }
        });
        setContextMenu(addMenu);
    }

    @Override
    public void startEdit() {
        super.startEdit();

        if (textField == null) {
            createTextField();
        }
        setText(null);
        setGraphic(textField);
        textField.selectAll();
    }

    @Override
    public void cancelEdit() {
        super.cancelEdit();

        setText((String) getItem());
        //setGraphic(getTableCell().getGraphic());
    }

    @Override
    public void updateItem(String item, boolean empty) {
        super.updateItem(item, empty);

        if (empty) {
            setText(null);
            setGraphic(null);
        } else {
            if (isEditing()) {
                if (textField != null) {
                    textField.setText(getString());
                }
                setText(null);
                setGraphic(textField);
            } else {
                setText(getString());
                //setGraphic(getTreeItem().getGraphic());



            }
        }
    }

    private void createTextField() {
        textField = new TextField(getString());
        textField.setOnKeyReleased(new EventHandler<KeyEvent>() {

            @Override
            public void handle(KeyEvent t) {
                if (t.getCode() == KeyCode.ENTER) {
                    commitEdit(textField.getText());
                } else if (t.getCode() == KeyCode.ESCAPE) {
                    cancelEdit();
                }
            }
        });  

    }

    private String getString() {
        return getItem() == null ? "" : getItem().toString();
    }
}

and I added this custom cellfactory as:

 final Callback<TableColumn, TableCell> cellFactory = 
new Callback<TableColumn, TableCell>() {
    public TableCell call(TableColumn p) {
        return new TextFieldTreeCellImpl();
}

};
col.setCellFactory(cellFactory);

Still it is working fine in JAVAFX 2.0 but not in JAVAFX 2.1

Please help me, I am having a hard time …

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T19:20:21+00:00Added an answer on June 6, 2026 at 7:20 pm

    Both official TreeView and TableView tutorials are working fine with JavaFX 2.1. The problem is, you are mixing these code examples. You cannot return the TreeCell when the cell factory of the TableColumn needs TableCell. Simply follow the TableView tutorial alone only and replace the EditingCell with the following one that has contextMenu:

    import javafx.event.Event;
    import javafx.event.EventHandler;
    import javafx.scene.control.ContextMenu;
    import javafx.scene.control.MenuItem;
    import javafx.scene.control.TableCell;
    import javafx.scene.control.TextField;
    import javafx.scene.input.KeyCode;
    import javafx.scene.input.KeyEvent;
    
    class EditingCell extends TableCell<Person, String> {
    
        private TextField textField;
        private ContextMenu addMenu = new ContextMenu();
    
        public EditingCell() {
            MenuItem addMenuItem = new MenuItem("Print Me");
            addMenu.getItems().add(addMenuItem);
            addMenuItem.setOnAction(new EventHandler() {
    
                public void handle(Event t) {
                    // Do something with current row
                    Person curr = getTableView().getItems().get(getIndex());
                    System.out.println(curr.getFirstName() + " " + curr.getLastName());
                }
            });
        }
    
        @Override
        public void startEdit() {
            super.startEdit();
    
            if (textField == null) {
                createTextField();
            }
            setText(null);
            setGraphic(textField);
            textField.selectAll();
        }
    
        @Override
        public void cancelEdit() {
            super.cancelEdit();
            setText((String) getItem());
            setGraphic(null);
        }
    
        @Override
        public void updateItem(String item, boolean empty) {
            super.updateItem(item, empty);
    
            if (empty) {
                setText(null);
                setGraphic(null);
            } else {
                if (isEditing()) {
                    if (textField != null) {
                        textField.setText(getString());
                    }
                    setText(null);
                    setGraphic(textField);
                } else {
                    setText(getString());
                    setGraphic(null);
                    setContextMenu(addMenu);
                }
            }
        }
    
        private void createTextField() {
    
            textField = new TextField(getString());
            textField.setMinWidth(this.getWidth() - this.getGraphicTextGap() * 2);
            textField.setOnKeyReleased(new EventHandler<KeyEvent>() {
    
                @Override
                public void handle(KeyEvent t) {
                    if (t.getCode() == KeyCode.ENTER) {
                        commitEdit(textField.getText());
                    } else if (t.getCode() == KeyCode.ESCAPE) {
                        cancelEdit();
                    }
                }
            });
        }
    
        private String getString() {
            return getItem() == null ? "" : getItem().toString();
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

this is the class. i want to add context menu to it on longpress
I want to add a bookmarklet in the context menu. This is my bookmarklet:
I want add label to every row in tableview at right side of the
I want add my custom sidebar next right column all page. Please check this
I've already figured out how to add a menu item to the right click
In my soundboard app I have created a context menu using this code. public
I want to add a submenu in my context menu which is created like
I Want to Add or Change the context menu of treeview of wpf in
i want to extend contact context menu in windows mobile. when a user right
I'm developing a flex application and I want to add it a context menu.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.