6.2 use the pop-up menu: Popup class
Not all content we want to bring up must be a menu. Through the Popup and PopupFactory classes, we can bring up any component on other components. This is different from the tooltip. The tooltip is read-only and unselectable. We can pop up a selectable button, tree or table.
6.2.1 create a pop-up component
Popup is a simple class with two methods, hide () and show (). It also has two protected constructors. We cannot directly create a Popup object, but need to obtain it from the PopupFactory class.
PopupFactory factory = PopupFactory. getSharedInstance ();
Popup popup = factory. getPopup (owner, contents, x, y); Popup created by PopupFactory with contents component is located on other components in the owner component.
6.2.2 example of a complete Popup/PopupFactory
Listing 6-7 demonstrates how to use Popup and PopupFactory of a JButton on another JButton. Selecting the initial JButton will create the second one at a random position on top of the first JButton. When the second button is visible, each button is selectable. If you select the initial visible button multiple times, multiple pop-up buttons will appear, as shown in Figure 6-9. Each pop-up menu will disappear in three seconds. In this example, select the pop-up menu to display only one message on the console.
Package net. ariel. ch06;
Import java. awt. Component;
Import java. awt. EventQueue;
Import java. awt. event. ActionEvent;
Import java. awt. event. ActionListener;
Import java. util. Random;
Import javax. swing. JButton;
Import javax. swing. JFrame;
Import javax. swing. Popup;
Import javax. swing. PopupFactory;
Import javax. swing. Timer;
Public class ButtonPopupSample {
Static final Random random = new Random ();
// Define ActionListener
Static class ButtonActionListener implements ActionListener {
Public void actionreceivmed (ActionEvent event ){
System. out. println ("Selected:" + event. getActionCommand ());
}
};
// Define show popu ActionListener
Static class ShowPopupActionListener implements ActionListener {
Private Component component;
ShowPopupActionListener (Component component ){
This. component = component;
}
Public synchronized void actionreceivmed (ActionEvent event ){
JButton button = new JButton ("Hello, world ");
ActionListener listener = new ButtonActionListener ();
Button. addActionListener (listener );
PopupFactory factory = PopupFactory. getSharedInstance ();
Int x = random. nextInt (200 );
Int y = random. nextInt (200 );
Final Popup popup = factory. getPopup (component, button, x, y );
Popup. show ();
ActionListener hider = new ActionListener (){
Public void actionreceivmed (ActionEvent event ){
Popup. hide ();
}
};
// Hide popup in 3 seconds
Timer timer = new Timer (3000, hider );
Timer. start ();
}
};
/**
* @ Param args
*/
Public static void main (String [] args ){
// TODO Auto-generated method stub
Runnable runner = new Runnable (){
Public void run (){
// Create frame
JFrame frame = new JFrame ("Button Popup Sample ");
Frame. setdefaclocloseoperation (JFrame. EXIT_ON_CLOSE );
ActionListener actionListener = new ShowPopupActionListener (frame );
JButton start = new JButton ("Pick Me for Popup ");
Start. addActionListener (actionListener );
Frame. add (start );
Frame. setSize (350,250 );
Frame. setVisible (true );
}
};
EventQueue. invokeLater (runner );
}
}