Java Swing Popup Panel Example

Java Swing | Popup and PopupFactory with examples

View Discussion

Improve Article

Save Article

Like Article

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 :

  1. 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:

  1. 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

import javax.swing.*;

import java.awt.*;

import java.awt.event.*;

class pop extends JFrame implements ActionListener {

Popup p;

pop()

{

JFrame f = new JFrame( "pop" );

JLabel l = new JLabel( "This is a popup" );

f.setSize( 400 , 400 );

PopupFactory pf = new PopupFactory();

JPanel p2 = new JPanel();

p2.setBackground(Color.red);

p2.add(l);

p = pf.getPopup(f, p2, 180 , 100 );

JButton b = new JButton( "click" );

b.addActionListener( this );

JPanel p1 = new JPanel();

p1.add(b);

f.add(p1);

f.show();

}

public void actionPerformed(ActionEvent e)

{

p.show();

}

public static void main(String args[])

{

pop p = new pop();

}

}

  1. Output:

  1. 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

import javax.swing.*;

import java.awt.*;

import java.awt.event.*;

class pop1 extends JFrame implements ActionListener {

Popup po;

JFrame f;

JPanel p;

PopupFactory pf;

pop1()

{

f = new JFrame( "pop" );

f.setSize( 400 , 400 );

pf = new PopupFactory();

JLabel l = new JLabel( "This  is a popup menu" );

JButton b19 = new JButton( "OK" );

b19.addActionListener( this );

try {

UIManager.setLookAndFeel(UIManager.

getSystemLookAndFeelClassName());

}

catch (Exception e) {

}

p = new JPanel();

p.setBackground(Color.blue);

Font fo = new Font( "BOLD" , 1 , 14 );

l.setFont(fo);

p.add(l);

p.add(b19);

p.setLayout( new GridLayout( 2 , 1 ));

po = pf.getPopup(f, p, 180 , 100 );

JButton b = new JButton( "click" );

b.addActionListener( this );

JPanel p1 = new JPanel();

p1.add(b);

f.add(p1);

f.show();

}

public void actionPerformed(ActionEvent e)

{

String d = e.getActionCommand();

if (d.equals( "OK" )) {

po.hide();

po = pf.getPopup(f, p, 180 , 100 );

}

else

po.show();

}

public static void main(String args[])

{

pop1 p = new pop1();

}

}

  1. 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

lunaexpined.blogspot.com

Source: https://www.geeksforgeeks.org/java-swing-popup-and-popupfactory-with-examples/

0 Response to "Java Swing Popup Panel Example"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel