This week's book giveaways are in the Refactoring and Agile forums.
We're giving away four copies each of Re-engineering Legacy Software and Docker in Action and have the authors on-line!
See this thread and this one for details.
Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Rite Sara
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20495
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Change the first parameter from null to a real component; one inside the frame you want to block (or the frame itself).
 
Rite Sara
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I didn't get your reply ..
Could you please elaborate in more details ??
 
Campbell Ritchie
Sheriff
Posts: 48424
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rite Sara wrote:Could you please elaborate in more details
Read the code you posted in your first post.
 
Rite Sara
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5125
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You have to replace null with
a real component; one inside the frame you want to block (or the frame itself).

 
Rite Sara
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's exactly what I am not getting .. Please elaborate on this ..
 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
> 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
Posts: 234
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20495
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic