Popup and PopupFactory are a part of the Java Swing library. Popups are used when we want to display to the user a Component on the top of all the other Components in that particular containment hierarchy. PopupFactory is the class that is used to create popups. Popups have a very small life cycle, and generally are lightweight components having limited sub-components.
Constructor for the class are :
- PopupFactory(): creates an object for popup factory
Commonly used methods:
method | explanation |
---|---|
getPopup(Component o, Component c, int x, int y) | Creates a Popup for the Component o containing the Component c at a location x, y on the owner component. |
hide() | Hides and disposes of the Popup. |
show() | Makes the Popup visible. |
Below programs will illustrate the use of a popup:
- Java Program to create a popup and display it on a parent frame: We create a popup p by creating a popup factory and using the function getpopup() which returns a popup. We add this popup to a frame f titled “pop” we will create a label and add t to a container panel p2 and set the background of panel p2 using setBackground() function. We will add the container panel p2 to the popup. We will also create a button b titled “click” added with an action listener and displays the popup when the button is pressed. The button b is added to a panel and the panel is added to the frame. Frame is set to the size 400,400 using setSize(400,400). Finally, the frame is displayed using show() function.
// Java Program to create a popup and display
// it on a parent frame
import
javax.swing.*;
import
java.awt.*;
import
java.awt.event.*;
class
pop
extends
JFrame
implements
ActionListener {
// popup
Popup p;
// constructor
pop()
{
// create a frame
JFrame f =
new
JFrame(
"pop"
);
// create a label
JLabel l =
new
JLabel(
"This is a popup"
);
f.setSize(
400
,
400
);
PopupFactory pf =
new
PopupFactory();
// create a panel
JPanel p2 =
new
JPanel();
// set Background of panel
p2.setBackground(Color.red);
p2.add(l);
// create a popup
p = pf.getPopup(f, p2,
180
,
100
);
// create a button
JButton b =
new
JButton(
"click"
);
// add action listener
b.addActionListener(
this
);
// create a panel
JPanel p1 =
new
JPanel();
p1.add(b);
f.add(p1);
f.show();
}
// if the button is pressed
public
void
actionPerformed(ActionEvent e)
{
p.show();
}
// main class
public
static
void
main(String args[])
{
pop p =
new
pop();
}
}
Output:
- Java Program to create a popup (add a panel) and display it on a parent frame and also add an action listener to the popup: We create a popup p by creating a popup factory and using the function getpopup() which returns a popup. We will set the look and feel to System look and feel. We add this popup to a frame f titled “pop”, we will create a label and a button b19 titled “OK”( we will set the font of the label to a specified font object) and add it to a container panel p2 and set the background of panel p2 using setBackground() function. We will add the container panel p2 to the popup. We will also create a button b titled “click” added with an action listener and displays the popup when the button is pressed. The button b is added to a panel and the panel is added to the frame. Frame is set to the size 400,400 using setSize(400,400). finally, the frame is displayed using show() function.
// Java Program to create a popup (add a panel) and
// display it on a parent frame and also
// add action listener to the popup
import
javax.swing.*;
import
java.awt.*;
import
java.awt.event.*;
class
pop1
extends
JFrame
implements
ActionListener {
// popup
Popup po;
// frame
JFrame f;
// panel
JPanel p;
// popupfactory
PopupFactory pf;
// constructor
pop1()
{
// create a frame
f =
new
JFrame(
"pop"
);
f.setSize(
400
,
400
);
pf =
new
PopupFactory();
// create a label
JLabel l =
new
JLabel(
"This is a popup menu"
);
// create a new button
JButton b19 =
new
JButton(
"OK"
);
// add action listener
b19.addActionListener(
this
);
try
{
// set windows look and feel
UIManager.setLookAndFeel(UIManager.
getSystemLookAndFeelClassName());
}
catch
(Exception e) {
}
// create a panel
p =
new
JPanel();
p.setBackground(Color.blue);
// create a font
Font fo =
new
Font(
"BOLD"
,
1
,
14
);
l.setFont(fo);
// add contents to panel
p.add(l);
p.add(b19);
p.setLayout(
new
GridLayout(
2
,
1
));
// create a popup
po = pf.getPopup(f, p,
180
,
100
);
// create a button
JButton b =
new
JButton(
"click"
);
// add action listener
b.addActionListener(
this
);
// create a panel
JPanel p1 =
new
JPanel();
p1.add(b);
f.add(p1);
f.show();
}
// if the button is pressed
public
void
actionPerformed(ActionEvent e)
{
String d = e.getActionCommand();
// if ok buton is pressed hide the popup
if
(d.equals(
"OK"
)) {
po.hide();
// create a popup
po = pf.getPopup(f, p,
180
,
100
);
}
else
po.show();
}
// main class
public
static
void
main(String args[])
{
pop1 p =
new
pop1();
}
}
Output:
- https://docs.oracle.com/javase/7/docs/api/javax/swing/Popup.html
- https://docs.oracle.com/javase/7/docs/api/javax/swing/PopupFactory.html
Note: The above programs might not run in an online IDE please use an offline compiler.
Reference:
Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready.