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

[9429 byte] By [Ruanaea] at [2007-11-27 11:42:50]
# 1

I think I've seen a solution in the forum that sets the size of the poup to 0 by overriding the combo box UI.

camickra at 2007-7-29 17:46:27 > top of Java-index,Desktop,Core GUI APIs...
# 2

> 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.

alternative to the popup with 0 size is to use a JSpinner(SpinnerListModel)

Michael_Dunna at 2007-7-29 17:46:27 > top of Java-index,Desktop,Core GUI APIs...
# 3

>I think I've seen a solution in the forum that sets the size of the poup to >0 by overriding the combo box UI.

Okay, I don't know much about UI's so I'll go search the forum and see what I can find. Thanks

> alternative to the popup with 0 size is to use a

> JSpinner(SpinnerListModel)

That could be a possibility but everywhere else in my program I've used combo boxes for selecting values so I'm trying to be consistent. And its not that I dont want the popup to never appear, it all depends on whether the form its on is in "Edit" or "View" mode as it where. But I'll keep that in mind as an alternative.

Thanks for you help

Ruanaea at 2007-7-29 17:46:27 > top of Java-index,Desktop,Core GUI APIs...
# 4

> Okay, I don't know much about UI's so I'll go search the forum and see what I can find.

search for this

popup.setPreferredSize(new Dimension(0,0));

Michael_Dunna at 2007-7-29 17:46:27 > top of Java-index,Desktop,Core GUI APIs...
# 5

> > Okay, I don't know much about UI's so I'll go

> search the forum and see what I can find.

>

> search for this

>

> popup.setPreferredSize(new Dimension(0,0));

Cheers, managed to find something and have come up with the following. Hope it helps anyone else who has this problem

import java.awt.Dimension;

import javax.swing.plaf.basic.BasicComboBoxUI;

import javax.swing.plaf.basic.BasicComboPopup;

import javax.swing.plaf.basic.ComboPopup;

public class ExtendedComboBoxUI extends BasicComboBoxUI{

@Override

protected ComboPopup createPopup()

{

BasicComboPopup popup = new BasicComboPopup(super.comboBox);

popup.setPopupSize(new Dimension(0,0));

return popup;

}

}

One thing, if using a particular look and feel you'll need to change the above class so it extends the relevant L&F ComboUI rather than the Basic one. There may be a way to do this programatically but my knowledge of UI's and L&F is very limited so I don't know.

Thanks again for your help

Ruanaea at 2007-7-29 17:46:27 > top of Java-index,Desktop,Core GUI APIs...