FileDocCategorySizeDatePackage
DropDownComponent.javaAPI DocExample2791Mon Jan 09 11:01:58 GMT 2006None

DropDownComponent

public class DropDownComponent extends JComponent implements ActionListener, AncestorListener

Fields Summary
protected JComponent
drop_down_comp
protected JComponent
visible_comp
protected JButton
arrow
protected JWindow
popup
Constructors Summary
public DropDownComponent(JComponent vcomp, JComponent ddcomp)

        drop_down_comp = ddcomp;
        visible_comp = vcomp;
        
        arrow = new JButton(new MetalComboBoxIcon());
        Insets insets = arrow.getMargin();
        arrow.setMargin( new Insets( insets.top, 1, insets.bottom, 1 ) );
        setupLayout();
        
        arrow.addActionListener(this);
        addAncestorListener(this);
    
Methods Summary
public voidactionPerformed(java.awt.event.ActionEvent evt)

        // build popup window
        popup = new JWindow(getFrame(null));
        popup.getContentPane().add(drop_down_comp);
        popup.addWindowFocusListener(new WindowAdapter() {
            public void windowLostFocus(WindowEvent evt) {
                popup.setVisible(false);
            }
        });
        popup.pack();
        
        // show the popup window
        Point pt = visible_comp.getLocationOnScreen();
		System.out.println("pt = " + pt);
        pt.translate(visible_comp.getWidth()-popup.getWidth(),visible_comp.getHeight());
		System.out.println("pt = " + pt);
        popup.setLocation(pt);
        popup.toFront();
        popup.setVisible(true);
        popup.requestFocusInWindow();
    
public voidancestorAdded(javax.swing.event.AncestorEvent event)

 
        hidePopup();
    
public voidancestorMoved(javax.swing.event.AncestorEvent event)

 
        if (event.getSource() != popup) {
            hidePopup();
        }
    
public voidancestorRemoved(javax.swing.event.AncestorEvent event)

 
        hidePopup();
    
protected java.awt.FramegetFrame(java.awt.Component comp)

        if(comp == null) {
            comp = this;
        }
        if(comp.getParent() instanceof Frame) {
            return (Frame)comp.getParent();
        }
        return getFrame(comp.getParent());
    
public voidhidePopup()

        if(popup != null && popup.isVisible()) {
            popup.setVisible(false);
        }
    
protected voidsetupLayout()

        GridBagLayout gbl = new GridBagLayout();
        GridBagConstraints c = new GridBagConstraints();
        setLayout(gbl);
        
        c.weightx = 1.0;  c.weighty = 1.0;
        c.gridx = 0;  c.gridy = 0;
        c.fill = c.BOTH;
        gbl.setConstraints(visible_comp,c);
        add(visible_comp);
        
        c.weightx = 0;
        c.gridx++;
        gbl.setConstraints(arrow,c);
        add(arrow);