How to not show a ListView ContextMenu when right to null

I want only the context menu to be shown if the user is clicking on an actual ListView, not if they click anywhere elsewhere in the listview. I know that this is possible and I found answers to similar problems, but in the end, they did not provide enough information for me to solve my problem. I found this example of how to do it with a list of String objects containing view, but what happens if my list view contains another object, in my case a wrapper class? I have a wrapper class to keep track of the key, but I displays only the variable value in the listview, by the toString method. If the cells still contain a wrapper.

class Wrapper(int key, String value) {
     ..
}

public String getValue() { return value; }
public SimpleStringProperty getValueAsProperty() { return new SimpleStringProperty(value); }
public String toString() { return value; }

I tried binding the context menu for the specific cells like this:

listView.setCellFactory(lv -> {

     ListCell<Wrapper> cell = new ListCell<>();

     cell.textProperty().bind(cell.itemProperty().getValue().getValueAsProperty());

     cell.emptyProperty().addListener((obs, wasEmpty, isNowEmpty) -> {
          if (isNowEmpty) {
               cell.setContextMenu(null);
          } else {
               cell.setContextMenu(contextMenu);
          }
       });
       return cell;
  });

But the line 05 gives me a NullPointerException, why? I do after I defined the elements of the list view by listView.setItems (.).

Link is not really necessary here... or at least not in the way you do.

This should be better:

public class Main extends Application {

    public class Wrapper {

        private final int key;
        private String value;

        public Wrapper(int key, String value) {
            this.key = key;
            this.value = value;
            valueAsProperty.set(value);
        }

        public String getValue() {
            return value;
        }

        private final StringProperty valueAsProperty = new SimpleStringProperty();

        public final StringProperty getValueAsProperty() {
            return valueAsProperty;
        }

        public final String toString() {
            return value;
        }
    }

    public static void main(String... args) {
        Application.launch(args);
    }

    @Override
    public void start(Stage primaryStage) throws Exception {
        final ContextMenu contextMenu = new ContextMenu(new MenuItem("Test"));
        final ListView listView = new ListView();
        listView.getItems().add(new Wrapper(0, "foo"));
        listView.getItems().add(new Wrapper(1, "fuu"));
        listView.getItems().add(new Wrapper(2, "faaa"));
        listView.getItems().add(null);
        listView.getItems().add(new Wrapper(4, "fyy"));
        listView.setCellFactory(lv -> {

            ListCell cell = new ListCell() {

                @Override
                protected void updateItem(Wrapper item, boolean empty) {
                    super.updateItem(item, empty);
                    textProperty().unbind();
                    setContextMenu(null);
                    if (!empty && item != null) {
                        textProperty().bind(item.getValueAsProperty());
                        setContextMenu(contextMenu);
                    }
                }
            };
            return cell;
        });
        final StackPane root = new StackPane(listView);
        final Scene scene = new Scene(root, 200, 400);
        primaryStage.setTitle("Test");
        primaryStage.setScene(scene);
        primaryStage.show();
    }
}

Tags: Java

Similar Questions

  • After effects is not show in creative cloud when I go to download

    After effects is not show in creative cloud when I go to download. @@@

    You MUST have a 64-bit computer and operating system... What is your computer?

    -Programs are supposed TO not view or download if they are not installed on your computer

    MINIMUM for After Effects and Premiere Pro http://helpx.adobe.com/premiere-pro/system-requirements.html

  • How to not show the pages in the Site map

    This is probably the simplest question of them all... but...

    How do you not show pages in the sitemap?

    It seems that by default, all pages are added to the sitemap.

    Is this a case of unchecking 'enabled' and 'exclude search results' under 'Other Options' control on the page itself?

    Or another way?

    Hi David,

    The "exclude from the search results ' will remove the 'yoursite.com/sitemap.xml' sitemap.xml and major to be picked up search engines.  If you check this option, please allow a few hours to have the xml code to reflect the changes.

    ""If you select this option, then this web page will be excluded from search site integrated to this system, as well as the sitemap.xml file submitted to the major search engines such as Google, Yahoo and Live.".

    Sitemap under site settings module is not related to the xml file, so you will need to manually remove and insert on your page if you use this function.

    - http://kb.worldsecuresystems.com/kb/add-sitemap.html

    Hope that clarifies.

    -Sidney

  • How to get rid of brush popup when right click on - pen Wacom of CS5 Windows 7

    I had this annoying problem with unwanted pop-up brush on click (mouse and pen) right before and found an answer, but can not this time.

    No, it not there nothing inside Photoshop to change this behavior - because this is the expected behavior.

    If you see the brush picker when you do not expect, then you are probably hitting the button on the stylus when you don't realize. You can change the assignment of the keys in the controls of Wacom, so it won't right click and so will not show the brush picker.

  • Is there a way to set the default to NOT show the menu on the right side?

    Every time I opened a PDF it is crushed because the right menu is open, and I have to reduce manually each time so you can see the PDF file. It wouldn't be a problem if it isn't my work didn't need to look 100 PDFs every week. Does anyone know how to change this default does not show the menu aside everytime I open a PDF file? Any advice would be greatly appreciated! Merci !

    Hiangiesaurous,

    First close the Tools pane manually. Then navigate to the Edit-> Preferences-> Documents menu-> uncheck "Open tools for each document pane.

    Click OK to confirm the changes.

    Restart the CD player to test.

    Kind regards

    Nicos

  • Devices drivers not showing no not after its installation. How can I show different pilot locations when looking at a workstation?

    I installed a hard drive of 250 GB and once he showed me that it is installed and that the device is administrable via Control Panel, it cannot be opened or read when you browse the file directory. How he finds out, open it and browse to the location of the devce driver if she does not?

    Hi Anderson5903,

    1. What is the brand and model of the hard drive?
    2. How is the 250 GB drive connected, it's via the USB port or internal drive?
    3. it worked on other computers?
          
    See the links below for help on Device Manager.
            
    Open the Device Manager
    http://Windows.Microsoft.com/en-us/Windows7/open-Device-Manager
               
    What is Device Manager?
    http://Windows.Microsoft.com/en-us/Windows7/what-is-Device-Manager 
                  
    I also suggest you to check in the disk management for the hard drive and also check if it has a drive letter assigned, if the drive letter is not assigned, then you can assign a drive letter and check the issue.
              
    To change drive letter assignments in Windows 7, follow these steps:
                  
    a. log in the computer as an administrator.
    b. Insert a disk in the removable drive.
    c. Click Start, right-click computer and then click on manage.
    d. click on disk management.
    e. right-click the partition, logical drive or volume that you want to change and then click on change drive letter and paths.
    f. click on the removable drive, click it, click on change, click the drive letter that you want to use, and then click OK.
            
    For more information, visit this link below:
              
    Change, add, or remove a drive letter
    http://Windows.Microsoft.com/en-us/Windows7/change-add-or-remove-a-drive-letter

    Thank you, and in what concerns:
    I. Suuresh Kumar - Microsoft technical support.

    Visit our Microsoft answers feedback Forum and let us know what you think.

  • Under the authority: How do I not show the column "else" when you edit a formula

    Hello everyone

    I'm creating a new analysis (reporting) and I have a problem when you edit a formula.

    I have an item called 'age' of the RPD. In my analysis (such as a table), I do not want to display each value 'age', I think you'll understand why. So, I'm to change the formula, and that's what I wrote:

    CASE

    WHEN 'âge' < = 18, THEN "minor".

    WHEN 'The age' (19, 20, 21, 22, 23, 24, 25) THEN ' < 26'

    END

    When I try this formula, I had 3 columns, a "minor", a "< 26' and an other empty but with numbers in it."

    It's I don't want to display a column for those who are more than 26 years.

    I tried to add another line before "END":

    NOTHING ELSE , but I read on other forums that if I write this line or not, OBIEE seems to do the same.

    I know I can hide the empty column, but I like, the analysis of 300 that include these data and it would take me some time if I had to hide the column for each analysis... In addition, I have a column selector and he won't do it more easily.

    If anyone has an idea how to solve this problem, I would be very grateful.

    I hate to make mistakes, I'm not a native English speaker

    Thanks for your help

    Clemency

    Good then you're basically transposing the rows into columns...

    Well how about setting a filter or selection stage to remove all NULL values for the column Age

  • Do not show my privacy options when I'm in the preferences. How to fix this?

    When I open preferences and click the option for the protection of personal information, instead of the drop-down list, I get nothing! What's wrong?

    Start Firefox in Firefox to solve the issues in Safe Mode to check if one of the extensions or if hardware acceleration is the cause of the problem (switch to the DEFAULT theme: Firefox/tools > Modules > appearance/themes).

  • How to NOT send some form data when the form is submitted

    Hello

    I created a PDF form with radio buttons in the Group and the boxes. When I the form of data, such as values using the submit by e-mail button to send form data as XML or merge data into a spreadsheet files, I'll see the data values for radio buttons and checkboxes.

    What I want here is to don't get the data values for some controls such as the boxes option, but no other controls like check boxes, in the XML or Excel worksheet after submission of the form. I know how to hide the controls in the form, but I have to the visible and active boxes on the form.

    Is there a way to hide or do not send form data when the form data is submitted?

    Thank you

    Sam

    Hi Kyle,.

    That's exactly what I want! Once I have not selected any data binding, the field is in shape and active, but when submitted as XML or it merge with a spreadsheet, the field is gone.

    Forgive me if I didn't my self clear in my question. Answer you is straight to the point.

    Thank you very much!

    Sam

  • How can I show more top sites when I open a new tab? Currently it only shows four.

    I opened a new tab and I see that first four sites, I used to get eight. How to change this back?

    If you 'zoom back"holding the key Ctrl and tapping - one or twice, this is get the page for a good place?

    If Firefox does not remember your zoom level of the page the next time that you open, you may need to disable prefetching of the page using this parameter:

    (1) in a new tab, type or paste Subject: config in the address bar and press enter/return. Click on the button promising to be careful.

    (2) in the search above the list box, type or paste newtab and make a pause so that the list is filtered

    (3) double-click the preference browser.newtab.preload to change its value from true to false

    After the change, if you have any 'new tab' pages open, close them and open a new one change, the zoom setting and now we must remember.

    If you do not want to reduce the zoom level, there are some add-ons to change the page. What follows is often referred to:

    https://addons.Mozilla.org/firefox/addon/new-tab-tools/

  • How to not show the windows in safe mode (pop-up), start in safe mode

    Firefox Safe mode

                 Safe Mode is a special mode of Firefox that can be used to troubleshoot issues.
                 Your add-ons and custom settings will be temporarily disabled. You can also Reset Firefox if you want to start fresh.
                                                                             Start in Safe Mode           Reset Firefox
    

    This window I want to cancel whenever I want to use the command line

    "C:\Program Files (x86)\Mozilla Firefox\firefox.exe" -safe-mode  How to do it? Thanks
    

    There is a pref extensions.enabledAddons you can find on the topic: config page.

    One possibility would be to have two versions of a user.js file to initialize this pref with extensions installed and enabled and copy one you need by launching Firefox via a cmd file.
    If you are that tight on memory, then you only need to run a version at the time.

    It is Pref plugin.state to disable plugins in case you want to do the same, but plugins are used when you visit pages that use them and affecting all the plugins for "Ask to activate" should suffice.

  • MSN hotmail does not show 'attachment' option bar when on firefox, but works on safari

    When I try to send an attachment on my msn hotmail account, the range of options that contains the option 'Attachment' briefly appears, then disappears so I can do an attachment. It is normally always there when I do a 'new' email. I have written to windows hotmail MSN and they said to optimise my browser, but I don't know how to do this. It works on my other computer and also works on Safari on my Mac. Just can't on my usual Mac with Firefox. So, what to do? Thank you.
    DK

    You can check this isssue with Firefox 10?

  • I have several symbols, how can we showed above the others when you click it?

    I have an animation that is a City/Beach/sea, and different parts of the city are symbols that can be clicked on, for example a boat in the sea could be clicked that would then lead the boat. My problem is that the boat could grow to fill the screen, then the animation playing, but as there are many other symbols, then another symbol could be on top of the animation clicked on / reading. Ideally when each animation is clicked, it would move to the top of the list of layers, but I guess that's not possible, so I was wondering if anyone had a better way to do this?

    You need to change the z-index when you click on a higher level and return it to its original level when it ends, or when the user clicks on another symbol.

  • Pages not showing in preview or when the downloaded - HELP

    In the two days that several pages in a Web site failed to load then uploaded - when I try to preview them I just get a black page.

    What have I done?

    The only change I did was to add some html < head > in the metadata would be the cause of the problem or is - it something else?

    Help

    Please try to remove all code customized page and then check, in addition, please check if the layers are removed.

    Thank you

    Sanjit

  • Desktop icons and desktop do not work. (Nothing happens when right clicking Google desktop for office properties, or by clicking on an icon on the desktop).

    My office does not work.  When I right click on the desktop to launch desktop properties, nothing happens.  In addition, trash and my other desktop files are also unresponsive.  However, I see the desktop correctly.

    I did a virus/malware scan using Norton Security Suite and Malwarebytes, and no viruses/malware were found.  I also have several reboots, run the check disk and deleted the cookies/cache.  How can I solve this?

    Before being able to solve problems, I ended up receiving an error message from Windows Explorer.  I checked the possibility of a solution and restart the Explorer.  Fortunately, this seemed to solve the problem.  I'll wait to see if this is a permanent or temporary solution.  Either by the way, thank you very much for your help!

Maybe you are looking for

  • The colors for my Google calendar disappeared. I can see with Safari and Chrome, but not with Firefox.

    I use Windows 7. I tried to delete the cookies and cache. Nothing has changed. There is no similar problem with all other browsers: Safari, Chrome or Internet Explorer. I restarted my computer. I had to send this chrome because Firefox does not load

  • Update hard drive on my Qosmio

    Hi I have a G20-126 with 2 HDDs 100gig, dose anyone know if I can get more great or am I stuck with these Thank youBarry

  • deterioration of the power of the purse strings

    Anyone else had problems with power cords deteriorate just for no reason? It happened to me several times.  I had cable usb for iphones, are still near the place where the plug is inserted into the iphone. The most recent is my macbook pro power cabl

  • Pavilion 17 f004dx: need to boot from DVD to load Windows 7 Pro

    I'm downgrading my OS Windows 7 Pro 64 bit because I convert this computer to a computer game. I placed my Windows 7 Pro 64 bit DVD in the DVD drve.  Previous upgrades of older and other brands of laptops, you just tell the BIOS to boot from the CD/D

  • Graphics card for tiny m72e?

    Is it possible to get a different graphics for the tiny M72e card? I checked the manual online and it does not show something that I can use to install a graphics card, but I just want to be sure...