Page 1 of 1

DateChooserPanel as popup

Posted: Mon Dec 12, 2005 5:27 pm
by uvoigt
Hi,

has anyone tried to use the DateChooserPanel class in a popup menu? I want to use an inputfield with a button that opens the DateChooserPanel to help the user choosing the date.

It's easy to put it in a JPopupMenu, but it's not so easy to implement that the popup does not close when selecting the month combo box :?

Any hints?

Uli

Here is the easy test code to play with:

Code: Select all

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Date;

import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFormattedTextField;
import javax.swing.JFrame;
import javax.swing.JPopupMenu;

import org.jfree.ui.DateChooserPanel;

public class DateChooserPanelTest
    extends JFrame
{

    public DateChooserPanelTest()
    {
        super();

        getContentPane().setLayout(new BorderLayout());

        getContentPane().add(new JFormattedTextField(new Date()), BorderLayout.CENTER);

        JButton popup = new JButton("v");

        popup.addActionListener(new ActionListener()
        {

            public void actionPerformed(ActionEvent e)
            {
                DateChooserPanel dcp = new DateChooserPanel();
                JPopupMenu mn = new JPopupMenu();
                mn.setLayout(new BorderLayout());
                mn.add(dcp, BorderLayout.CENTER);
                
                JComponent j =(JComponent)e.getSource(); 
                mn.show(j,0,j.getHeight());
            }

        });

        getContentPane().add(popup, BorderLayout.EAST);
        pack();
    }

    /**
     * @param args
     */
    public static void main(String[] args)
    {
        DateChooserPanelTest dt = new DateChooserPanelTest();
        dt.setVisible(true);
    }

}

Posted: Tue Dec 13, 2005 1:28 am
by Taqua
Hi,

this kind of ugly behaviour is controled by the JPopupMenu's UI implementation. That thing listens on all mouse events and controls the visiblity of the popup menu.

If you dare to look at the sourcecode of the BasicPopupMenuUI, you'll find a perfect example on how to make some code almost impossible to extend or modify. The inner class MouseGrabber listens for *all* mouse events and if the click event goes to a menu which is not the currently selected JMenu-object, the popup closes. I dont see a hook on how to change this.

I guess it is easier to copy the idea they use of listening to global mouse events and to create an own JComponent with a similiar behaviour (but modified to accept and ignore clicks on all childs of the popup menu).

Regards,
Thomas

Posted: Tue Dec 13, 2005 8:58 am
by uvoigt
Uiii, JPopupMenu seems to be the dark side of Java? :twisted:
The implementation looks really awful.

I will try the idea with creating an own JComponent with similar behaviour. Thanks for your help! :D

Ulrich