Message Popups

I do not know how to encode a message popup 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 tried using JOptionPane in my method, but it does not work:

 String st = "Welcome"; JOptionPane.showMessageDialog(null, st); 
+57
java swing netbeans
Aug 16 2018-11-11T00:
source share
9 answers

javax.swing.JOptionPane

Here is the code for the method that I call whenever I want the info window to pop up, it launches 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 parameter, JOptionPane ( null in this example), is used to align the dialog. null forces it to center itself on the screen, however any java.awt.Component can be specified, and instead a dialog will appear in the center of this Component .

I usually use the titleBar String to describe where in the code from which the box is called, so if it's annoying, I can easily track and remove the code responsible for sending my screen using infoBoxes.

To use this method call:

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



javafx.scene.control.Alert

For a detailed description of how to use JavaFX dialogs, see JavaFX Dialogs (official) using code.makery. They are much more powerful and flexible than Swing dialogs, and are capable of much more than just pop-up messages.

As above, I will 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(); } } 

Keep in mind that JavaFX is a single-threaded set of GUI tools, which means that this method should be called directly from the JavaFX application thread. If you have another thread doing work that needs a dialog, see These SO Q & As: JavaFX2: Can I pause a background job / service? and Platform and 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"); 
+129
Sep 21 '11 at 19:30
source share

first you need to import: import javax.swing.JOptionPane; then you can call it using this:

 JOptionPane.showMessageDialog(null, "ALERT MESSAGE", "TITLE", JOptionPane.WARNING_MESSAGE); 

zero places it in the middle of the screen. put quotation marks in a warning message. The title is obviously the title, and the last part will format it as an error message. if you want the regular message to just replace it with PLAIN_MESSAGE . It works very well, largely for errors.

+23
Mar 09 '13 at 21:17
source share

A couple of "improvements" that I use for debugging, especially when starting projects (i.e. not in debug mode).

  1. By default, the name of the calling method is indicated in the title bar of the message box. This is convenient for stopping the flow at a given point, but should be cleaned before release.
  2. Automatically copy the subscriberโ€™s name and message to the clipboard because you cannot search for the image!

     package forumposts; import java.awt.Toolkit; import java.awt.datatransfer.Clipboard; import java.awt.datatransfer.StringSelection; import javax.swing.JOptionPane; public final class MsgBox { public static void info(String message) { info(message, theNameOfTheMethodThatCalledMe()); } public static void info(String message, String caller) { show(message, caller, JOptionPane.INFORMATION_MESSAGE); } static void error(String message) { error(message, theNameOfTheMethodThatCalledMe()); } public static void error(String message, String caller) { show(message, caller, JOptionPane.ERROR_MESSAGE); } public static void show(String message, String title, int iconId) { setClipboard(title+":"+NEW_LINE+message); JOptionPane.showMessageDialog(null, message, title, iconId); } private static final String NEW_LINE = System.lineSeparator(); public static String theNameOfTheMethodThatCalledMe() { return Thread.currentThread().getStackTrace()[3].getMethodName(); } public static void setClipboard(String message) { CLIPBOARD.setContents(new StringSelection(message), null); // nb: we don't respond to the "your content was splattered" // event, so it OK to pass a null owner. } private static final Toolkit AWT_TOOLKIT = Toolkit.getDefaultToolkit(); private static final Clipboard CLIPBOARD = AWT_TOOLKIT.getSystemClipboard(); } 

The full class also has debugging and warning methods, but I will shorten them for brevity and you will still get the main points. You can use the public static logical isDebugEnabled to suppress debug messages. If everything is done correctly, the optimizer will (almost) remove these method calls from your working code. See: http://c2.com/cgi/wiki?ConditionalCompilationInJava

Greetings. Whale.

+5
Aug 11 '13 at 5:55 a.m.
source share
 JOptionPane.showMessageDialog(btn1, "you are clicked save button","title of dialog",2); 

btn1 is a JButton variable, and it is used in this dialog box to open the btn1 dialog box or text box, etc., by default, use the zero position of the frame. Next to the message, and then the name of the dialogue. 2 numbers of the alert type 3 icon are information 1,2,3,4. Well, hope you can understand that.

+1
May 04 '17 at 20:10
source share

Good, SO Basically, I think I have a simple and effective solution.

 package AnotherPopUpMessage; import javax.swing.JOptionPane; public class AnotherPopUp { public static void main(String[] args) { // TODO Auto-generated method stub JOptionPane.showMessageDialog(null, "Again? Where do all these come from?", "PopUp4", JOptionPane.CLOSED_OPTION); } } 
0
Aug 13 '17 at 0:56
source share

Use the following library: import javax.swing.JOptionPane;

Enter at the top of the line of code. You should only add this because everything else is done right!

0
Jul 18
source share
 import javax.swing.*; class Demo extends JFrame { String str1; Demo(String s1) { str1=s1; JOptionPane.showMessageDialog(null,"your message : "+str1); } public static void main (String ar[]) { new Demo("Java"); } } 
0
Jan 25 '19 at 14:35
source share

you can call this method

 public void showMessage() { JOptionPane.showMessageDialog(null, "this is a pop up message"); } 
-one
May 17 '16 at 8:51
source share

POP UP WINDOWS IN APPLET

Hi guys, I was looking for pop-ups in an applet all over the Internet, but couldn't find an answer for windows.

Although itโ€™s simple, Iโ€™m just helping you. Hope you enjoy it in its simplest form. here is the code:

File name: PopUpWindow.java for the Java file, and we also need an HTML file.

For the applet, let's take it popup.html

THE CODE:

 import java.awt.*; import java.applet.*; import java.awt.event.*; public class PopUpWindow extends Applet{ public void init(){ Button open = new Button("open window"); add(open); Button close = new Button("close window"); add(close); Frame f = new Frame("pupup win"); f.setSize(200,200); open.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { if(!f.isShowing()) { f.setVisible(true); } } }); close.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { if(f.isShowing()) { f.setVisible(false); } } }); } } /* <html> <body> <APPLET CODE="PopUpWindow" width="" height=""> </APPLET> </body> </html> */ to run: $javac PopUpWindow.java && appletviewer popup.html 
-one
May 08 '18 at 15:46
source share



All Articles