JCombobox question
Hi there,
I am making such functions on a JCombobox:
When click the popup key, a new dialog display whithout showing popup menu.
I implemeneted the PopupMenuListener. The dialog shows but the popup menu is still there and never disappear.
Is there any way to solve that?
Thanks
[314 byte] By [
tmdfana] at [2007-10-3 8:44:09]

you don't want the standard JCombobox popup to show?if so, couple of options here http://forum.java.sun.com/thread.jspa?forumID=57&threadID=751967
The samples seems not suitable for mouse clicking.
post a sample working program and run us through the steps to reproduce the problem(I thought your problem was the popup was showing and you didn't want this)
The code is like following:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
public class ComboBoxTest extends JFrame
{
JComboBox comboBox;
public ComboBoxTest()
{
setSize(100, 100);
String[] items = { "Item1", "Item2"};
comboBox = new JComboBox(items);
Container c = getContentPane();
c.setLayout(new FlowLayout());
c.add( comboBox );
comboBox.addPopupMenuListener(new MyPopupMenuListener());
}
public JFrame getCurrentInstance(){ return this;}
public static void main(String[] args)
{
ComboBoxDownKey frame = new ComboBoxDownKey();
frame.setDefaultCloseOperation( EXIT_ON_CLOSE );
frame.setLocationRelativeTo( null );
frame.setVisible( true );
}
class MyPopupMenuListener implements PopupMenuListener
{
public void popupMenuCanceled(PopupMenuEvent e){}
public void popupMenuWillBecomeInvisible(PopupMenuEvent e){}
public void popupMenuWillBecomeVisible(PopupMenuEvent e)
{
JDialog dialog = new JDialog(getCurrentInstance(), "Test", true);
dialog.setLocationRelativeTo(comboBox);
dialog.show();
}
}
}
The problem is when I set the coming dialog to be modal, the popup menu will not disappear. Is there any way to solve that?
Thanks
After over 100 posting on the forum you should know how to use the code formatting tags so that the posted code is readable.
In fact you where given instructions on how to do this before:
http://forum.java.sun.com/thread.jspa?threadID=766598
So I know I'm not going to bother to attempt to read the code.
Sorry for the mess. The code is like following:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
public class ComboBoxTest extends JFrame
{
JComboBox comboBox;
public ComboBoxTest()
{
setSize(100, 100);
String[] items = { "Item1", "Item2"};
comboBox = new JComboBox(items);
Container c = getContentPane();
c.setLayout(new FlowLayout());
c.add( comboBox );
comboBox.addPopupMenuListener(new MyPopupMenuListener());
}
public JFrame getCurrentInstance(){ return this;}
public static void main(String[] args)
{
ComboBoxDownKey frame = new ComboBoxDownKey();
frame.setDefaultCloseOperation( EXIT_ON_CLOSE );
frame.setLocationRelativeTo( null );
frame.setVisible( true );
}
class MyPopupMenuListener implements PopupMenuListener
{
public void popupMenuCanceled(PopupMenuEvent e){}
public void popupMenuWillBecomeInvisible(PopupMenuEvent e){}
public void popupMenuWillBecomeVisible(PopupMenuEvent e)
{
JDialog dialog = new JDialog(getCurrentInstance(), "Test", true);
dialog.setLocationRelativeTo(comboBox);
dialog.show();
}
}
}
The problem is when I set the coming dialog to be modal, the popup menu will not disappear. Is there any way to solve that?
Thanks
do you need to use the popupListener?
if not, alternative
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
class ComboBoxTest extends JFrame
{
JComboBox comboBox;
public ComboBoxTest()
{
setSize(100, 100);
String[] items = { "Item1", "Item2"};
comboBox = new JComboBox(items);
Container c = getContentPane();
c.setLayout(new FlowLayout());
c.add( comboBox );
comboBox.setUI(new MyUI());
}
public JFrame getCurrentInstance(){ return this;}
public static void main(String[] args)
{
ComboBoxTest frame = new ComboBoxTest();
frame.setDefaultCloseOperation( EXIT_ON_CLOSE );
frame.setLocationRelativeTo( null );
frame.setVisible( true );
}
class MyUI extends javax.swing.plaf.basic.BasicComboBoxUI
{
protected JButton createArrowButton()
{
JButton btn = new JButton();
btn.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent ae){
JDialog dialog = new JDialog(getCurrentInstance(), "Test", true);
dialog.setLocationRelativeTo(comboBox);
dialog.show();
}
});
return btn;
}
}
}
The code in the popup menu listener isn't even executed.
It should work now. I tried to edit the UI to be a button. The problem is when clicking the box not the button, the popup menu still display.
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
public class ComboBoxTest extends JFrame
{
JComboBox comboBox;
public ComboBoxTest()
{
setSize(800, 600);
String[] items = { "Item1"};
comboBox = new JComboBox( items );
Container c = getContentPane();
c.setLayout(new FlowLayout());
c.add( comboBox );
comboBox.addPopupMenuListener(new MyPopupMenuListener());
}
public JFrame getCurrentInstance(){ return this;}
public static void main(String[] args)
{
ComboBoxTest frame = new ComboBoxTest();
frame.setDefaultCloseOperation( EXIT_ON_CLOSE );
frame.setLocationRelativeTo( null );
frame.setVisible( true );
}
class MyPopupMenuListener implements PopupMenuListener
{
public void popupMenuCanceled(PopupMenuEvent e){}
public void popupMenuWillBecomeInvisible(PopupMenuEvent e){}
public void popupMenuWillBecomeVisible(PopupMenuEvent e)
{
JDialog dialog = new JDialog(getCurrentInstance(), "Test", true);
dialog.setLocationRelativeTo(comboBox);
dialog.show();
}
}
}
I fail to see what you are trying to accomplish with this design so its difficult to suggest an alternate solution.
Why are you using a combo box? If the user can never see the drop down menu, what is the point?
If tried using SwingUtilities.invokeLater to show the dialog after using comboBox.setPopupVisible( false ), but it doesn't work. Any time I use the invokeLater the combobox button is still painted in its pressed state.
You could try override the BasicComboBoxUI. Override the createPopup() method and set the size (or preferredSize) of the popup to be 0.
I am finalizing an application. And now it goes to another person to do usability testing.
The function is to display a date input dialog. I used a button with the date as the label. It open the date input dialog after clicking. The testing person said it is not comprehensive. He recommand me to use Microsoft style date input. That is a combobox with nothing inside but showing the date input dialog after clicking.
I feel that is a stupid idea. But he is very serious. Maybe I should make something looks like a combobox but not a real one.
why don't you just use JCalendar http://www.toedter.com