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);
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");
}
}
Ok, 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);
}
}
Use the following library:
import javax.swing.JOptionPane;
Input at the top of the code-line. You must only add this, because the other things is done correctly!
A couple of "enhancements" I use for debugging, especially when running projects (ie not in debug mode).
Automatically copy the caller-name and message to the clipboard, because you can't search an 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's 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 debug and warning methods, but I cut them for brevity and you get the main points anyway. You can use a public static boolean isDebugEnabled to suppress debug messages. If done properly the optimizer will (almost) remove these method calls from your production code. See: http://c2.com/cgi/wiki?ConditionalCompilationInJava
Cheers. Keith.
first you have to import: import javax.swing.JOptionPane; then you can call it using this:
JOptionPane.showMessageDialog(null,
"ALERT MESSAGE",
"TITLE",
JOptionPane.WARNING_MESSAGE);
the null puts it in the middle of the screen. put whatever in quotes under alert message. Title is obviously title and the last part will format it like an error message. if you want a regular message just replace it with PLAIN_MESSAGE
. it works pretty well in a lot of ways mostly for errors.
JOptionPane.showMessageDialog(btn1, "you are clicked save button","title of dialog",2);
btn1 is a JButton variable and its used in this dialog to dialog open position btn1 or textfield etc, by default use null position of the frame.next your message and next is the title of dialog. 2 numbers of alert type icon 3 is the information 1,2,3,4. Ok I hope you understand it
POP UP WINDOWS IN APPLET
hi guys i was searching pop up windows in applet all over the internet but could not find answer for windows.
Although it is simple i am just helping you. Hope you will like it as it is in simpliest form. here's the code :
Filename: PopUpWindow.java
for java file and we need html file too.
For applet let us take its popup.html
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
Source: Stackoverflow.com