How do I pop up message in window from java console application?If I use JOptionPane, then I need to pass component which is visible.But if I want to show some warning or error from console application(which does not have any window visible), Then I cant use JOptionPane. Also I want the displayed message to be shown on top of any other application(To get the attention of user ).
Thanks in advance.
You can use
JOptionPane.showMessageDialog(null, "Message goes here");
EDIT:
I tried
import javax.swing.JOptionPane;
public class Foo{
public static void main(String a[])
{
JOptionPane.showMessageDialog(null, "Message goes here");
}
}
and the output was
so your problem could be somewhere else?
If you really need a popup, make the component argument null and it will not have a parent.
Related
I have a class with only static methods and one of them opens a JOptionPane error message dialogue using a JFrame object as component.
This is the class + method:
public class miscMethods
{
static JFrame errorWindow = null;
public static void ErrorPopup(String message)
{
errorWindow = new JFrame();
errorWindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
errorWindow.setAlwaysOnTop(true);
JOptionPane.showMessageDialog(errorWindow, message, "Error", JOptionPane.ERROR_MESSAGE);
errorWindow = null;
}
}
The ErrorPopup method is used inside a JavaFX controller and other places, called like this:
import static code.miscMethods.ErrorPopup;
...
ErrorPopup("This is the error message");
Problem is that the application's process won't close when I close the the program from the window's ✕ after the popup appears, because the JFrame was created and shown.
I know the JFrame is the culprit, so I added the errorWindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
but it doesn't seem to do anything, since the program isn't closing.
In this question: JFrame and why stay running
The accepted answer talks about non-daemon threads, but the only thread I open is a daemon one, so unless JavaFX open one then it can't be that I believe.
So, why does the process keep running and how can I solve it?
I'm still new to Java so if I made a mistake and/or my code shows bad practices please do point them out!
Edit: I'm using a JFrame because I need the setAlwaysOnTop, since using
JOptionPane.showMessageDialog(null, message, "Error", JOptionPane.ERROR_MESSAGE); opens it not on top of the JavaFX window. If there's a better way let me know.
This:
errorWindow = null;
does nothing of use since the object is still displayed. You want this instead:
errorWindow.dispose();
Actually, even better, simply get rid of errorWindow altogether and pass null as the first parameter to the JOptionPane.
I have this idea, that anytime an unhandled exception occurs in my JavaFX program, that instead of relying on console output, I can display an alert to the user. I am thinking that perhaps I can capture the output from System.err to use. Here is what I have tried thus far.
PrintStream myStream = new PrintStream(System.err) {
#Override
public void println(String s) {
super.println(s);
Log.debugLog(s); //this function logs to a file and displays an alert to user
}
};
System.setErr(myStream);
This code segment works if I replace System.err with System.out, and System.setErr to System.setOut. However that is capturing System.out, not System.err. I suppose the better question would be, what exact function does System.err call when displaying an error to the console? So that may override it. Any tips are appreciated.
I think you have the wrong approach. If you want to display an alert to the user when there is an unhandled Exception, you can do that by setting a DefaultUncaughtExceptionHandler:
Thread.setDefaultUncaughtExceptionHandler((t, e) -> {
// show alert to user
e.printStackTrace();
// do whatever you want with the Exception e
});
You can create a JavaFX Alert customized to show an exception stack trace along with a brief message. This link shows the code for the below.
I found a webpage on the java site on how to make dialog windows, but it isn't working when I try it. The site said to type:
JOptionPane.showMessageDialog(frame, "Window text.");
I'm just trying to make a window with a bit of text and an ok button, but when I type this in, my Eclipse IDE wants me to import something for the JOptionPane, and after I do that, it says the the "frame" part is incorrect, that it "cannot be resolved to a variable." What am I doing wrong here?
Start by making sure you have included the import javax.swing.JOptionPane; statement within your import portion of your code.
Next, try using
JOptionPane.showMessageDialog(null, "Window text.");
instead.
For example...
import javax.swing.JOptionPane;
public class TestDialog {
public static void main(String[] args) {
JOptionPane.showMessageDialog(null, "Window text.");
}
}
Take a closer look at How to Make Dialogs for more details.
You should also consult the JavaDocs when in doubt...
The first parameter in the call to JOptionPane.showMessageDialog should be an instance of a JFrame or JWindow that you want to assign your message dialog to. If you don't have a JFrame or JWindow but still want to display the message dialog, just put in null as the first parameter, like this:
JOptionPane.showMessageDialog(null, "Window text.");
When a user clicks a JButton in my Java-Swing application, a string is returned from a method and the user then needs to be able to read the string (somehow). The JButton is within a JPanel. My first thought was to create an 'alert' dialogue (thinking this would be easy), I tried to follow this example that looked easy: http://www.java2s.com/Code/Java/SWT-JFace-Eclipse/DialogExample.htm
I have not yet been able to confirm if this works because I do not know how to import the libraries into eclipse. For example import org.eclipse.swt.SWT; gives the error "... cannot be resolved".
So one possible solution is how to import in Eclipse. Another possible solution is to dynamically change the text within the JPanel somehow.
As Ben mentioned in his comment. I would set a jLabel with a blank text to start. Then, when you click your button that triggers the method, simply tack on:
label.setText(value);
Alternatively you could use another pane to popup and display the message.
If you're talking about Swing, an easy solution is to pop a message box with the string:
button.addActionListener(new ActionListener {
public void actionPerformed(ActionEvent e) {
String message = methodThatReturnsYourString();
JOptionPane.showMessageDialog(null, message);
}
}
In this link you will find the information u require in order to add the library and import it:
http://www.eclipsepluginsite.com/swt.html
as well I would not mix SWT with swing and I would stick to swing you can set the label on the event of your button
button.addActionListener(new ActionListener {
public void actionPerformed(ActionEvent e) {
String message = "Test String";
labelMessage.setText(message );
}
}
I'm learning Java and I have no idea how to do this.
I dragged a button on the form in Netbeans, double clicked it and it created this event:
#Action
public void HelloClickMethod()
{
JOptionPane.showMessageDialog(this, "The message!", "This is supposed to be the MessageBox title.");
}
This is the exception the IDE brings up.
Cannot find symbol. Symbol: showMessageDialog()
Edit 1>
Now I changed it to this:
#Action
public void HelloClickMethod()
{
JOptionPane.showMessageDialog(this, "The message!", "This is supposed to be the MessageBox title.",JOptionPane.ERROR_MESSAGE);
}
However the IDE is saying I have an error in the word 'this'. "Cannot find symbol". I don't understand. Why is it so dificult and why are the errors so esoteric. :P
I can think of the following cause: you might not be "importing" the package containing JOptionPane. Try:
import javax.swing.*;
On top of your source file. Or, use
javax.swing.JOptionPane.showMessageDialog(this, "The message!", "This is supposed to be the MessageBox title.", JOptionPane.ERROR_MESSAGE);
After questioner edit:
Other cause is the location of the method, if you are in an static context, you can't use this.
The showMessageDialog method does not take 3 parameters. Try this:
JOptionPane.showMessageDialog(this, "The message!", "This is supposed to be the MessageBox title.", JOptionPane.ERROR_MESSAGE);
There are 3 methods named showMessageDialog, one with 2 parameters (component and message), 4 parameters (component, message, title, message type) and 5 parameters (component, message, title, message type, icon).
This works fine:
JOptionPane.showMessageDialog(null,"ErrorMSG", "Title!", JOptionPane.WARNING_MESSAGE)