Message display on top of other apps - java

Please help me in showing popup in front of browser. I have used following code, but it's coming behind browser once I clicked a save button.
JFrame frame;
frame = new JFrame("");
JOptionPane.showMessageDialog(frame, "Instance is already created");

You need to specify the window/frame as the first argument to show message dialog in front of that frame. So instead of passing null:
JOptionPane.showMessageDialog(null,
"Instance is already created");
pass the parent Frame

Related

Why JDialog doesn't close after JOptionPane showMessageDialog?

When my server app is starting, a JDialog opens to indicate to the user that the app is loading. But between the opening of JDialog and its closing, I use JOptionPane.showMessageDialog() to display another message.
The problem is, if I display this new message before closing JDialog then JDialog will never close even if I close JOptionPane dialog. If I remove the JOptionPane dialog then the JDialog closes as usual.
Why opening JOptionPane.showMessageDialog() disable JDialog closing ?
Is use this code to open JDialog:
final JDialog dlg = new JDialog(this, "Veuillez patienter, le serveur démarre...", true);
dlg.setDefaultCloseOperation(JDialog.DO_NOTHING_ON_CLOSE);
dlg.setSize(300, 75);
dlg.setResizable(false);
dlg.setLocationRelativeTo(this);
Thread t = new Thread(() -> {
dlg.setVisible(true);
});
t.start();
And this code to close it:
dlg.setVisible(false);
And between these lines of code I do this to open message dialog:
JOptionPane.showMessageDialog(this, String.format(I18n.i18n.getString("PopupWifiCreated"), this.SSID, this.password), null, JOptionPane.INFORMATION_MESSAGE);
Anyone has an idea?
Thanks.
In my opinion setting the visibility of any frame to false is not the greatest idea. Of course it depends what are your goals, but I'd still not recommend it. It should be done like e.g.:
dlg.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
dlg.close();
or
dlg.dispose();
If it still wont work, try to reverse the order of dialogs - first show messageDialog, and then JDialog.

Create jDialog Swing with a message of error with image

I'm new of Java and I want to do a jDialog that is opening when I push a button in the main JFrame and show a message of error in this way:
I can't put the image in another way in NetBeans? I create in the source package a directory with the image and a try much thing:
jDialog1.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
jDialog1.add(new JLabel(new ImageIcon(ImageIO.read(getClass().getResourceAsStream("/img/error_button.png")))));
jDialog1.pack();
jDialog1.setLocationByPlatform(true);
jDialog1.setVisible(true);
Is there a simple way to do this?
We can use an option pane for this. It includes its own icons according to the type of message (and look and feel).
Exception e = new Exception("Error!");
JOptionPane.showMessageDialog(f, e, e.getMessage(), JOptionPane.WARNING_MESSAGE);

Java (JFace Application Window) Setting external label text

I am looking to figure out how to set the text of a label on an external Application Window.
What I have:
I have two windows so far. The first one is the main application window that will appear when the user starts the program. The second window is another separate window that I have created specifically to display a custom error window.
The problem: I seem to be unable to call the label that I have created on the error window and set the text to something custom. Why? I want to be able to reuse this window many times! This window is aimed for things like error handling when there is invalid input or if the application cannot read/save to a file.
I was going to post screen shots but you need 10 rep for that. It would have explained everything better.
Here is the code for the label on the Error_dialog window:
Label Error_label = new Label(container, SWT.NONE);
Error_label.setBounds(10, 10, 348, 13);
Error_label.setText("Label I actively want to change!");
Here is the condition I would like to fire off when it is met:
if(AvailableSpaces == 10){
//Set the label text HERE and then open the window!
showError.open();
}
I have included this at the top of the class as well:
Error_dialog showError = new Error_dialog();
Just save the label as a field in your dialog class and add a 'setter' method. Something like:
public class ErrorDialog extends Dialog
{
private Label errorLabel;
... other code
public void setText(String text)
{
if (errorLabel != null && !errorLabel.isDisposed()) {
errorLabel.setText(text);
}
}
You will need to use your dialog like this:
ErrorDialog dialog = new ErrorDialog(shell);
dialog.create(); // Creates the controls
dialog.setText("Error message");
dialog.open();
Note: you should stick to the rules for Java variable names - they always start with lower case.
Further learn to use Layouts. Using setBounds will cause problems if the user is using different fonts.

convert form.showDialog from vb to Java

I need a similar code of form.showDialog from vb to Java to show a Frame up to its parent Frame. I've tried something like this :
private void button1ActionPerformed(java.awt.event.ActionEvent evt) {
Frame2 form = new Frame2();
form.setVisible(true);
}
and i got 2 problems,
the first : the new frame wont stay on the top alway, that mean i can select the old form
and the second problem : when i close the new form the parent form will close too !
Take a look at Swing's JDialog which has a modal property allowing the dialog to remain as the topmost window

Java GUI, Multiple Frames

How do I go about creating what I describe below?
First, here is the basic look of my GUI:
When I click on Add New Account I want to have the GUI pop up a small window where the user can enter log-in credentials. I would need this information to be passed back into the main GUI, so I am lost as how to approach this.
The same goes for Preferences or Remove Account. How do I go about creating a "GUI Overlay" of sorts. Sorry, I can't figure out the correct terminology for the effect I am looking for.
I wanted to attempt to use JOptionPane's, but after some research this seemed like it was not the route to be taking.
I was also toying with the idea of creating a new JFrame when the action was preformed. How should this be approached?
Start by using dialogs over frames. Dialogs are designed to gather small pieces of information from the user.
I would create a separate component for each operation you want to perform. Within these components I would provide setters and getters to allow you to gain access to the information managed by the component.
From there I would either use a JOptionPane or JDialog to display the component to the user. The reason for using one over the other for me comes down to begin able to control the action buttons (Okay and Cancel for example). For something like the login dialog, I want to restrict the user from begin able to hit the Login button until they've provided enough information to make the attempt.
The basic follow would be something like this...
LoginDialog dialog = new LoginDialog(SwingUtilities.getWindowAncestor(this)); // this is a reference any valid Component
dialog.setModal(true); // I would have already done this internally to the LoginDialog class...
dialog.setVisible(true); // A modal dialog will block at this point until the window is closed
if (dialog.isSuccessfulLogin()) {
login = dialog.getLogin(); // Login is a simple class containing the login information...
}
The LoginDialog might look something like this...
public class LoginDialog extends JDialog {
private LoginPanel loginPane;
public LoginDialog(Window wnd) {
super(wnd);
setModal(true);
loginPane = new LoginPanel();
setLayout(new BorderLayout());
add(loginPane);
// Typically, I create another panel and add the buttons I want to use to it.
// These buttons would call dispose once they've completed there work
}
public Login getLogin() {
return loginPane.getLogin();
}
public boolean isSuccessfulLogin() {
return loginPane.isSuccessfulLogin();
}
}
The dialog is simply acting as proxy/container for the login pane.
This is, of course an overview, you will need to fill in the blanks ;)
Now, if you don't want to go to the trouble of creating your own dialog, you can take advantage of the JOptionPane instead.
LoginPanel loginPane = new LoginPanel();
int option = JOptionPane.showOptionDialog(
this, // A reference to the parent component
loginPane,
"Login", // Title
JOptionPane.YES_NO_OPTION,
JOptionPane.QUESTION_MESSAGE,
null, // You can supply your own icon it if you want
new Object[]{"Login", "Cancel"}, // The available options to the user
"Login" // The "initial" option
);
if (option == 0) {
// Attempt login...
}

Categories