Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Popup Message boxes

I am unsure of how to code popup message box in my methods.

public String verify(){     String result = "failed";     int authcode = staffBean.getVerifyCodeByName(getLoginUserName());      if (code == authcode){                result ="success";     }         else{ //statement to popup an error message box      }     return result; } 

I have tried to use JOptionPane in my method but it does not work:

String st = "Welcome"; JOptionPane.showMessageDialog(null, st); 
like image 445
gymcode Avatar asked Aug 16 '11 14:08

gymcode


People also ask

What are the 3 kinds of popup boxes?

JavaScript has three kind of popup boxes: Alert box, Confirm box, and Prompt box.

What are pop-up boxes called?

Pop-up Boxes might be referred to by a number of names — modal, slide-in box, fade-in box, notification, overlay, scroll box, smart bar, pop-up window, etc.

What is a popup box?

Popup boxes (or dialog boxes) are modal windows used to notify or warn the user, or to get input from the user. Popup boxes prevent the user from accessing other aspects of a program until the popup is closed, so they should not be overused.

How do you make a popup box in HTML?

If you want the ability to close the alert message, add a <span> element with an onclick attribute that says "when you click on me, hide my parent element" - which is the container <div> (class="alert"). Tip: Use the HTML entity " &times; " to create the letter "x".


1 Answers

javax.swing.JOptionPane

Here is the code to a method I call whenever I want an information box to pop up, it hogs the screen until it is accepted:

import javax.swing.JOptionPane;  public class ClassNameHere {      public static void infoBox(String infoMessage, String titleBar)     {         JOptionPane.showMessageDialog(null, infoMessage, "InfoBox: " + titleBar, JOptionPane.INFORMATION_MESSAGE);     } } 

The first JOptionPane parameter (null in this example) is used to align the dialog. null causes it to center itself on the screen, however any java.awt.Component can be specified and the dialog will appear in the center of that Component instead.

I tend to use the titleBar String to describe where in the code the box is being called from, that way if it gets annoying I can easily track down and delete the code responsible for spamming my screen with infoBoxes.

To use this method call:

ClassNameHere.infoBox("YOUR INFORMATION HERE", "TITLE BAR MESSAGE"); 

javafx.scene.control.Alert

For a an in depth description of how to use JavaFX dialogs see: JavaFX Dialogs (official) by code.makery. They are much more powerful and flexible than Swing dialogs and capable of far more than just popping up messages.

As above I'll post a small example of how you could use JavaFX dialogs to achieve the same result

import javafx.scene.control.Alert; import javafx.scene.control.Alert.AlertType; import javafx.application.Platform;  public class ClassNameHere {      public static void infoBox(String infoMessage, String titleBar)     {         /* By specifying a null headerMessage String, we cause the dialog to            not have a header */         infoBox(infoMessage, titleBar, null);     }      public static void infoBox(String infoMessage, String titleBar, String headerMessage)     {         Alert alert = new Alert(AlertType.INFORMATION);         alert.setTitle(titleBar);         alert.setHeaderText(headerMessage);         alert.setContentText(infoMessage);         alert.showAndWait();     } } 

One thing to keep in mind is that JavaFX is a single threaded GUI toolkit, which means this method should be called directly from the JavaFX application thread. If you have another thread doing work, which needs a dialog then see these SO Q&As: JavaFX2: Can I pause a background Task / Service? and Platform.Runlater and Task Javafx.

To use this method call:

ClassNameHere.infoBox("YOUR INFORMATION HERE", "TITLE BAR MESSAGE"); 

or

ClassNameHere.infoBox("YOUR INFORMATION HERE", "TITLE BAR MESSAGE", "HEADER MESSAGE"); 
like image 121
Troyseph Avatar answered Sep 26 '22 10:09

Troyseph