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);
}
}