0

I have a created a class of ComboBox popup menu listener to change the width of popup menu from the actual width of combobox.

protected void customizePopup(BasicComboPopup popup)
{
    scrollPane = getScrollPane(popup);

    if (popupWider)
        popupWider( popup );

    checkHorizontalScrollBar( popup );

    //  For some reason in JDK7 the popup will not display at its preferred
    //  width unless its location has been changed from its default
    //  (ie. for normal "pop down" shift the popup and reset)

    Component comboBox = popup.getInvoker();
    Point location = comboBox.getLocationOnScreen();

    if (popupAbove)
    {
        int height = popup.getPreferredSize().height;
        popup.setLocation(location.x, location.y - height);
    }
    else
    {
        int height = comboBox.getPreferredSize().height;
        popup.setLocation(location.x, location.y + height - 1);
        popup.setLocation(location.x, location.y + height);
    }
}

And this method is called from

public void popupMenuWillBecomeVisible(PopupMenuEvent e)
{
    JComboBox comboBox = (JComboBox)e.getSource();

    if (comboBox.getItemCount() == 0) return;

    final Object child = comboBox.getAccessibleContext().getAccessibleChild(0);

    if (child instanceof BasicComboPopup)
    {
        SwingUtilities.invokeLater(new Runnable()
        {
            public void run() {
                customizePopup((BasicComboPopup)child);
            }
        });
    }
}

calling method CustomizePopup() outside EDT causes NullPointerException.

Can anyone tell me what is the reason??

Why do we need another EDT to process CustomizePopup()?

public void popupMenuWillBecomeVisible(PopupMenuEvent e)
{
    JComboBox comboBox = (JComboBox)e.getSource();

    if (comboBox.getItemCount() == 0) return;

    final Object child = comboBox.getAccessibleContext().getAccessibleChild(0);

    if (child instanceof BasicComboPopup)
    {
        customizePopup((BasicComboPopup)child);
    }
}
John Kugelman
  • 349,597
  • 67
  • 533
  • 578
M.H
  • 3
  • 5
  • 1
    Possible duplicate of [What is a NullPointerException, and how do I fix it?](http://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it) – bradimus Oct 28 '16 at 17:08
  • @bradimus NO!!!. this question has nothing to do with event dispatch thread – M.H Oct 28 '16 at 17:26
  • 1
    @M.H, Yes it does have to do with the EDT. The code is overriding the default processing of the combo box popup. So in this cause it need to make sure the custom code is added to the end of the EDT so it executes after the default popup code is executed. Sometimes the internals use invokeLater() to add processing code to the end of the EDT, so this this code also needs to make sure the custom code is added to the end of the EDT. – camickr Oct 28 '16 at 17:42
  • There is **only one EDT** – Antoniossss Oct 28 '16 at 17:47
  • @camickr But it should be added by EDT which is in main method .why are we using another EDT. – M.H Oct 28 '16 at 17:47
  • @Antoniossss one EDT is in the main method. which set JFrame visible – M.H Oct 28 '16 at 17:49
  • You are not using another EDT. You are adding code to the "END" of the existing EDT. This makes sure that the default processing of the popup is executed before this custom changes are executed. – camickr Oct 28 '16 at 17:53
  • But we are making new instance of EDT here ? and my question is when EDT calls setPopupMenuListener() from main method so why we need to add this code again in EDT – M.H Oct 28 '16 at 17:55
  • @M.H there is **only one EDT** so your argument is **INVALID** https://docs.oracle.com/javase/tutorial/uiswing/concurrency/dispatch.html – Antoniossss Oct 28 '16 at 17:59
  • Now i understand we are just adding code in same EDT... but add something to my knowledge why it causes NullPointerException – M.H Oct 28 '16 at 18:01
  • >>calling method CustomizePopup() outside EDT causes NullPointerException. - and how are you calling it ffrom "outside" of EDT? – Antoniossss Oct 28 '16 at 18:05
  • By not calling SwingUtilites.invokeLater(new Runnable()) method in the popupMenuWillBecomeVisible() method – M.H Oct 28 '16 at 18:07
  • @M.H please append the code that causes NPE insteed of describing it - show me the code. – Antoniossss Oct 28 '16 at 18:09
  • i am editing .wait – M.H Oct 28 '16 at 18:10
  • @Antoniossss i have edited. – M.H Oct 28 '16 at 18:14
  • 1
    The original code is found in [Combo Box Popup](https://tips4java.wordpress.com/2010/11/28/combo-box-popup/). The NPE is generated because the combo box popup is in an undefined state (for whatever reason) when the custom code is executed. The invokeLater() is used so that the custom code can be executed AFTER the popup is now in a complete state. Sometimes the internal workings of the components get complicated so you use the invokeLater() so the code is executed after the default processing of the original code is finished. It is a solution based on experience once a problem is encountered. – camickr Oct 28 '16 at 18:47

0 Answers0