How do I make dialog windows in java? - java

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.");

Related

JFrame keeps application running even after closing

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.

Text Output in Java Swing

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 );
}
}

displaying message from console application

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.

How do I get a MessageBox like information window to appear in Java?

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)

JOptionPane won't show its dialog on top of other windows

I have problem currently for my swing reminder application, which able to minimize to tray on close. My problem here is, I need JOptionPane dialog to pop up on time according to what I set, but problem here is, when I minimize it, the dialog will pop up, but not in the top of windows when other application like explorer, firefox is running, anyone know how to pop up the dialog box on top of windows no matter what application is running?
Create an empty respectively dummy JFrame, set it always on top and use it as the component for the JOptionPane instead of null. So the JOptionPane remains always on top over all other windows of an application. You can also determine where the JOptionPane appears on screen with the location of the dummy JFrame.
JFrame frmOpt; //dummy JFrame
private void question() {
if (frmOpt == null) {
frmOpt = new JFrame();
}
frmOpt.setVisible(true);
frmOpt.setLocation(100, 100);
frmOpt.setAlwaysOnTop(true);
String[] options = {"delete", "hide", "break"};
int response = JOptionPane.showOptionDialog(frmOpt, msg, title, JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE, null, options, "delete");
if (response == JOptionPane.YES_OPTION) {
removeRow();
}
frmOpt.dispose();
}
Old post, but I was struggling with this.
My problem was more with Javafx allowing the JOptionPane to go behind the current Java window.
Therefore I used the following which does what the original poster asked by putting the JOptionPane in front of all windows; even JAVAFX.
Firstly the old JOptionPane:
JOptionPane.showMessageDialog(null, "Here I am");
Now an JOptionPane that stays in front:
final JDialog dialog = new JDialog();
dialog.setAlwaysOnTop(true);
JOptionPane.showMessageDialog(dialog, "Here I am");
And for fun here is everything in one long line:
JOptionPane.showMessageDialog(
((Supplier<JDialog>) () -> {final JDialog dialog = new JDialog(); dialog.setAlwaysOnTop(true); return dialog;}).get()
, "Here I am");
You can make a static method some where that will return the JDialog for you and then just call it in the JOptionPane to clean up your code a bit.
Are you using one of the canned JOptionPanes? (Like JOptionPane.showCOnfirmDialog(...))
You may want to look at extending JDialog and making your own dialog panel, and then calling myDialog.setAlwaysOnTop(true);
Windows is blocking this operation since XP.
The scenario before was like:
Your a tiping in some text in an editor and not recognize that another dialog is coming to front when you are tipping the text. The coming dialog gets the focus and you are tiping in the new dialog. Maybe you click enter after you are ready and do this in the wrong dialog, which is asking whether you realy want to delet your hard disk ;)
The come to front call in java is only working for java windows.
The possibibilty to notify the user of a new window is to implement a Frame, which will highlighted/flashing in the windows task bar.
Correction the post above..
I have resolve my problem as below:
this.setVisible(true); // show main frame
MyDialog dialog = New MyDialog(this, true); // show my custom dialog
dialog.setVisible(true);
this.setVisible(false);
it works fine for me :)
You might think about using a JFrame instead. It may give you a little more flexibility.
If you are using a JFrame and you want it to popup on top of the other windows use:
myFrame.setVisible(true);
myFrame.setState(Frame.NORMAL);
The setState will show the window to the user if it was in minimized state previously.

Categories

Resources