How to Show Popup in Java Swing?

Leave a comment

October 7, 2012 by huionn

I want to create a simple-looking popup that has components like text field, combo box etc. I tried a few methods.

  1. javax.swing.JPopupMenu
  2. javax.swing.Popup
  3. JPanel combined with JLayeredPane
  4. javax.swing.JDialog

At the end, I found that JDialog is the simplest solution.

dialog

final JDialog d = new JDialog();
Editor p = new Editor();
p.setBorder(BorderFactory.createEtchedBorder());
d.setUndecorated(true); // hide the title bar
d.setSize(p.getPreferredSize());
d.setLocationRelativeTo(this);
d.setAlwaysOnTop(true);
d.add(p);
d.setVisible(true);
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: