Preventing the popup of a JComboBox from displaying
Hi
Again i come seeking advice. What I'm trying to achieve is a JComboBox which looks enabled i.e. you can read the text of the currently selected Item but which is disabled in terms of functionality i.e. does not show a popup when clicked.
Having searched both google and the forum I've written a custom combo box which, in theory, allows the toggling of whether the combo box popup is displayed or not. A boolean flag indicates whether the popup should be displayed, its checked inside the firePopupMenuWillBecomeVisible()
method and if the popup should not be displayed then hidePopup()
is called.
However, rather than having the desired effect of hiding the popup I seem to have some how caused the popup to becomestuck. If you move the mouse over it it still acts as thought its enabled, but if you click outside of the popup it remains on screen, and if you drag the frame it remains in stationary on screen. While this is a cool effect that I couldnt achieve if I wanted to, it doesn't help me with my current situation. COuld anyone give me some advice?
I've include the code for my custom combobox:
import javax.swing.JComboBox;
publicclass HideableComboBoxextends JComboBox{
privateboolean showPopup =true;
publicvoid setShowPopup(boolean showPopup)
{
this.showPopup = showPopup;
}
@Override
publicvoid firePopupMenuWillBecomeVisible()
{
super.firePopupMenuWillBecomeVisible();
if (!showPopup)
{
this.hidePopup();
}
}
}
and a small runnable example to illustrate the problem i'm having:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.*;
/**
* @author User #1
*/
publicclass HideableComboExampleextends JFrame{
public HideableComboExample(){
initComponents();
}
publicstaticvoid main(String[] args)
{
HideableComboExample example =new HideableComboExample();
example.pack();
example.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
example.setVisible(true);
}
privatevoid enablePopupButtonActionPerformed(){
hideableComboBox1.setShowPopup(true);
enablePopupButton.setEnabled(false);
disablePopupButton.setEnabled(true);
this.repaint();
}
privatevoid disablePopupButtonActionPerformed(){
hideableComboBox1.setShowPopup(false);
enablePopupButton.setEnabled(true);
disablePopupButton.setEnabled(false);
this.repaint();
}
privatevoid closeButtonActionPerformed(){
System.exit(0);
}
privatevoid initComponents(){
dialogPane =new JPanel();
contentPanel =new JPanel();
panel1 =new JPanel();
hideableComboBox1 =new HideableComboBox();
enablePopupButton =new JButton();
disablePopupButton =new JButton();
buttonBar =new JPanel();
closeButton =new JButton();
Container contentPane = getContentPane();
contentPane.setLayout(new BorderLayout());
dialogPane.setBorder(new EmptyBorder(12, 12, 12, 12));
dialogPane.setLayout(new BorderLayout());
contentPanel.setLayout(new BorderLayout());
panel1.setLayout(new FlowLayout());
hideableComboBox1.setModel(new DefaultComboBoxModel(new String[]{
"Test",
"1",
"2",
"3",
"4",
"5",
"6"
}));
panel1.add(hideableComboBox1);
enablePopupButton.setText("Enable Popup");
enablePopupButton.setEnabled(false);
enablePopupButton.addActionListener(new ActionListener(){
publicvoid actionPerformed(ActionEvent e){
enablePopupButtonActionPerformed();
}
});
panel1.add(enablePopupButton);
disablePopupButton.setText("Disable Popup");
disablePopupButton.addActionListener(new ActionListener(){
publicvoid actionPerformed(ActionEvent e){
disablePopupButtonActionPerformed();
}
});
panel1.add(disablePopupButton);
contentPanel.add(panel1, BorderLayout.CENTER);
dialogPane.add(contentPanel, BorderLayout.CENTER);
buttonBar.setBorder(new EmptyBorder(12, 0, 0, 0));
buttonBar.setLayout(new GridBagLayout());
((GridBagLayout)buttonBar.getLayout()).columnWidths =newint[]{0, 80};
((GridBagLayout)buttonBar.getLayout()).columnWeights =newdouble[]{1.0, 0.0};
//- closeButton -
closeButton.setText("Close");
closeButton.addActionListener(new ActionListener(){
publicvoid actionPerformed(ActionEvent e){
closeButtonActionPerformed();
}
});
buttonBar.add(closeButton,new GridBagConstraints(1, 0, 1, 1, 0.0, 0.0,
GridBagConstraints.CENTER, GridBagConstraints.BOTH,
new Insets(0, 0, 0, 0), 0, 0));
dialogPane.add(buttonBar, BorderLayout.SOUTH);
contentPane.add(dialogPane, BorderLayout.CENTER);
pack();
setLocationRelativeTo(getOwner());
}
private JPanel dialogPane;
private JPanel contentPanel;
private JPanel panel1;
private HideableComboBox hideableComboBox1;
private JButton enablePopupButton;
private JButton disablePopupButton;
private JPanel buttonBar;
private JButton closeButton;
}
Thanks

