Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make PopUp window in java

I am currently developing a java application.

I want to show a new Window which contains a text area and a button.

Do you have any ideas?

like image 654
Carlo Avatar asked Jan 13 '12 15:01

Carlo


People also ask

What is popup menu in Java?

A class that implements a menu which can be dynamically popped up at a specified position within a component. As the inheritance hierarchy implies, a PopupMenu can be used anywhere a Menu can be used.

What creates a window dialog box in Java?

The showMessageDialog method creates a basic one-button dialog box. The showOptionDialog method, however, enables you to customize features like the number of buttons, the words on the buttons, and even allows you to ask for input in your dialog box. Notice that line 1 in the code above imports the Swing package.

Which of these can be used to pop up alert window in Java?

JOptionPane; then you can call it using this: JOptionPane. showMessageDialog(null, "ALERT MESSAGE", "TITLE", JOptionPane. WARNING_MESSAGE);


2 Answers

The same answer : JOptionpane with an example :)

package experiments;  import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JOptionPane;  public class CreateDialogFromOptionPane {      public static void main(final String[] args) {         final JFrame parent = new JFrame();         JButton button = new JButton();          button.setText("Click me to show dialog!");         parent.add(button);         parent.pack();         parent.setVisible(true);          button.addActionListener(new java.awt.event.ActionListener() {             @Override             public void actionPerformed(java.awt.event.ActionEvent evt) {                 String name = JOptionPane.showInputDialog(parent,                         "What is your name?", null);             }         });     } } 

enter image description here

like image 163
COD3BOY Avatar answered Oct 04 '22 19:10

COD3BOY


Hmm it has been a little while but from what I remember...
If you want a custom window you can just make a new frame and make it show up just like you would with the main window. Java also has a great dialog library that you can check out here:

How to Make Dialogs

That may be able to give you the functionality you are looking for with a whole lot less effort.

Object[] possibilities = {"ham", "spam", "yam"}; String s = (String)JOptionPane.showInputDialog(                     frame,                     "Complete the sentence:\n"                     + "\"Green eggs and...\"",                     "Customized Dialog",                     JOptionPane.PLAIN_MESSAGE,                     icon,                     possibilities,                     "ham");  //If a string was returned, say so. if ((s != null) && (s.length() > 0)) {     setLabel("Green eggs and... " + s + "!");     return; }  //If you're here, the return value was null/empty. setLabel("Come on, finish the sentence!"); 

If you do not care to limit the user's choices, you can either use a form of the showInputDialog method that takes fewer arguments or specify null for the array of objects. In the Java look and feel, substituting null for possibilities results in a dialog that has a text field and looks like this:

like image 36
kreynolds Avatar answered Oct 04 '22 19:10

kreynolds