Reputation: 1642
I recently found this useful code to create a titled border that contains a component as title by Santhosh Kumar T: ComponentTitledBorder. I successfully employed it using a check box, but when I decided to use a combo box, it only shows the text part of the combo box but not the drop down button:
When I click on the combo box I get the following exception:
Exception in thread "AWT-EventQueue-0" java.awt.IllegalComponentStateException: component must be showing on the screen to determine its location
at java.awt.Component.getLocationOnScreen_NoTreeLock(Unknown Source)
at java.awt.Component.getLocationOnScreen(Unknown Source)
at javax.swing.JPopupMenu.show(Unknown Source)
at javax.swing.plaf.basic.BasicComboPopup.show(Unknown Source)
at javax.swing.plaf.basic.BasicComboPopup.togglePopup(Unknown Source)
at javax.swing.plaf.basic.BasicComboPopup$Handler.mousePressed(Unknown Source)
at java.awt.Component.processMouseEvent(Unknown Source)
at javax.swing.JComponent.processMouseEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at test.ComponentTitledBorder.dispatchEvent(ComponentTitledBorder.java:74)
at test.ComponentTitledBorder.mousePressed(ComponentTitledBorder.java:96)
at java.awt.Component.processMouseEvent(Unknown Source)
at javax.swing.JComponent.processMouseEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$200(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)
I have looked at all google results I could find for this exception, but none of them helped me to understand my specific problem. To illustrate, I provide an sscce:
package test;
import java.awt.BorderLayout;
import java.awt.Dimension;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JComboBox;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
@SuppressWarnings("serial")
public class Problem extends JComponent {
JPanel area = new JPanel();
JComboBox<String> cb;
public Problem() {
area.setPreferredSize(new Dimension(100,100));
String[] options = {"one", "two"};
cb = new JComboBox<String>(new DefaultComboBoxModel<String>(options));
cb.setEditable(false);
cb.setSelectedIndex(0);
this.setLayout(new BorderLayout());
area.setBorder(new ComponentTitledBorder(cb, area, UIManager.getBorder("TitledBorder.border")));
this.add(area, BorderLayout.CENTER);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new Problem());
frame.pack();
frame.setVisible(true);
}
});
}
}
Please help me fix this AND hopefully understand the nature of the error.
Upvotes: 1
Views: 398
Reputation: 57421
Define a JPanel
with BorderLayout
and add the JComboBox
to the panel. Then assign the ComponentTitledBorder
to the panel rather than to JComboBox
directly.
UPDATE:
JPanel p = new JPanel();
p.setLayout(new BorderLayout());
p.add(cb, BorderLayout.CENTER);
area.setBorder(new ComponentTitledBorder(p, area,
UIManager.getBorder("TitledBorder.border")));
Upvotes: 2