wood burning stoves 2.0*
The moose likes Swing / AWT / SWT and the fly likes How to display alert box at the top of all frames ? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "How to display alert box at the top of all frames ?" Watch "How to display alert box at the top of all frames ?" New topic
Author

How to display alert box at the top of all frames ?

Rite Sara
Ranch Hand

Joined: Feb 13, 2010
Posts: 56
Hi,

I am using JOptionPane to show alert box but, it doesn't appear at the top of my current frame. The alert box is getting displayed below all the frames.
Here is the code I am using to show alert box.

JOptionPane.showMessageDialog(null, "Please enter File path", "Error", JOptionPane.WARNING_MESSAGE );

Please let me know how I can make it appear at the top.


Thanks,

Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19760
    
  20

Change the first parameter from null to a real component; one inside the frame you want to block (or the frame itself).


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Rite Sara
Ranch Hand

Joined: Feb 13, 2010
Posts: 56
I didn't get your reply ..
Could you please elaborate in more details ??
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39828
    
  28
Rite Sara wrote:Could you please elaborate in more details
Read the code you posted in your first post.
Rite Sara
Ranch Hand

Joined: Feb 13, 2010
Posts: 56
Actually the part I didnt understand was

a real component; one inside the frame you want to block (or the frame itself).


I didn't get what exactly I have to replace null with.
Darryl Burke
Bartender

Joined: May 03, 2008
Posts: 4658
    
    5

You have to replace null with
a real component; one inside the frame you want to block (or the frame itself).


luck, db
There are no new questions, but there may be new answers.
Rite Sara
Ranch Hand

Joined: Feb 13, 2010
Posts: 56
That's exactly what I am not getting .. Please elaborate on this ..
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
> Please elaborate on this ..

you first.

"I am using JOptionPane to show alert box but, it doesn't appear at the top of my current frame"

do you mean above, or in front?

if in front (i.e. middle), you have the answer, at least twice, and, if you don't understand,
seriously, consider a career path change.

if, instead, you want to specify where, on the screen, the optionPane is to show,
there is a simple trick that works now, but no guarantees it will work in future versions.
Petar Thomas
Ranch Hand

Joined: Oct 11, 2009
Posts: 234
I'll try...

the parameters of the method look like this:

public static void showMessageDialog(Component parentComponent,
Object message,
String title,
int messageType)
throws HeadlessException

Parameters:

parentComponent - determines the Frame in which the dialog is displayed; if null, or if the parentComponent has no Frame, a default Frame is used

message - the Object to display

title - the title string for the dialog

messageType - the type of message to be displayed: ERROR_MESSAGE, INFORMATION_MESSAGE, WARNING_MESSAGE, QUESTION_MESSAGE, or PLAIN_MESSAGE

icon - an icon to display in the dialog that helps the user identify the kind of message that is being displayed



I quoted this from JavaDocs.. Like for example, from here:
http://java.sun.com/j2se/1.4.2/docs/api/javax/swing/JOptionPane.html


And this is also what you would need:
http://download.oracle.com/docs/cd/E17409_01/javase/tutorial/uiswing/components/dialog.html


So, when You writted it, it was like:

JOptionPane.showMessageDialog(null, "Please enter File path", "Error", JOptionPane.WARNING_MESSAGE );


You can see, that you have written 'null' in the place of the 'parentComponent' (Component),



So, when Rob Prime have written:
from null to a real component


, he meant that You should try to change from null to *"the real" Component*...


I am not sure which is it, becaouse I am learning it now, but it could be a JFrame (a "parent Window"), or a JPanel, or something like this. You should read the "how to..", and javadoc...

But for now, simply replace 'null' with anything that you have in your program (the real component) like JFrame, panel, etc... i don't know, but it's in examples...


[EDIT:]
There are different types of dialogs... That is important. You can read about it in "How to use dialogs", link is above, and in the javadoc.

[EDIT2:]
Actually,...




Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19760
    
  20

You can use the frame as the first parameter, or any control (component) you put into that frame. The only difference will be how the dialog will be placed on the screen; it will be centered around the component. If the frame is the first parameter than the dialog will be shown exactly in the center of your frame. If it's a button in your frame it will be centered around the button. In both cases it will block access to the frame.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to display alert box at the top of all frames ?