Get parent JFrame for displaying exception msg
I have a problem.
Lots of my application components are classes thatextends JPanel
In these classes lot's of different exception could be raised.
Some of them I want to show to user (invalid input, no DB connection error e.t.c.)
I make may GUI with help of GUI-builder + I write own classes As I said. Then I initialize them on my form in code.
How can I reach parent form and send message to user?
Is there any common method like:
getParentForm.showMessageDialog(String mesage);
Did I explain my problem clearly...?
[587 byte] By [
Holoda] at [2007-11-26 22:13:55]

# 1
What if you add the parent as a parameter of the constructor of the class you wrote ?So then you will be able to call the showmessagedialog with the parent parameter set to the parameter of the constructor.
ProZa at 2007-7-10 11:04:36 >

# 2
Hi Holod,Read the tutorial how to use JOptionPane: http://java.sun.com/docs/books/tutorial/uiswing/components/dialog.html#featuresIf you still want to get the parent frame you can use:JOptionPane.getFrameForComponent or SwingUtilities.getWindowAncestor
# 3
Hi Rodney!
I've worked with this tutorial.
I've used new frame in each class, where I wanted to show Dialog.
But it's extremely stupid way!
If I have 10 classes, I will have 11 frames in my application.
one of them is main frame and others are for Dialogs.
Anyway, I will hav ParentFrame. So I want to get access to it's Dialog...
Thank you for methods!
I will try them in my app.
I have a question: I've surfed forum and I have seen tons of typical question and You and camickr gave lot's of answers.
Is it possible to put answers in one catalog.
The name of catalog will be "Solutions for frequently asked questions"
...
Thanks again!
Holoda at 2007-7-10 11:04:36 >

# 4
There's nothing wrong with creating a new dialog to show a message.Most of the applications do it this way.Why do you think that's stupid?The frequently asked questions sounds like a good idea.You can email Sun with this suggestion.
# 5
> The frequently asked questions sounds like a good
> idea.
hmmm... in theory users should search the forum before asking a question if it has not been asked yet. I think situation will be similar with FAQ. Everybody's problem will be so "unique" and "exceptional" that they will go straight to the forum.
# 6
But "see faq" is quicker to type than "do a search" :o)
# 7
No, FAQ - is pretty useless thing.
I suggest this one idea:
see visualization:
http://foto.mail.ru/mail/ice_holod/114/s-494.jpg
no read my idea. I hope you will undestand me.
Excuse me for offtopic.
I suggest to develop "wisdom master.
This master will give user solution for his problem.
It will look like interactive dialog.
For example, I have a question: 揑 want to put JComboBox into JTable?
I open 搘isdom master?br>There I see list of topics:
1.SWING/AWT
Database
Java language basics
I choose 揝WING?
2.The I see lest of SWING components, which were discussed million times:
JFrame
JTable
JComboBox
e.t.c.
I choose 揓Table?
3.Then I see list of ready solutions divided into categories:
Embed something (embed checkboxes, comboboxes e.t.c.)
Control data in JTable (add/delete row)
e.t.c.
I choose 揈mbed something?br>
4.Then I see description and links to several threads:
Embed combobox (several links to most valuable threads)
Embed checkbox (several links to most valuable threads)
e.t.c.
BUT!
If I would choose JComboBox instead of JTable on step #2 anyway I will get JComboBox + JTable solutions through master.
See this chain: SWING->JComboBox (I did not choose JTable)->Embed somewhere->Embed into JTable
Of course, such 搘isdom master?will not help starters. Sometimes they even can抰 understand what they need and what to search (I am a starter and sometimes I do not know what exactly to read, and where to search?
But, I think such service will help users which familiar with Java technology. The y will not have to read 搒earch results?and million topics.
The way to solution will be shorter.
The MAIN PROBLEMS are
1.Separate food solutions and useful threads
2.Develop categories. We do not have to create many categories, user can lost in these categories, we can抰 reduce their quantity. We have to find gold middle.
3.Develop good graph (graph branches = search chains). Categories=nodes of Graph
Holoda at 2007-7-10 11:04:36 >

# 8
1. I mean I have 3 classes where, possibly, I will show user Dialog.
If some exception rises (incorrect input, Database connectio error e.t.c.) I show Dialog.
And inside each class I make personal frame. And I show Dialog with help of Frame of each class.
So, as a result, I have application with three useless frames...
That's bad.
As you have written, I want to use VERY-VERY main frame and generate Dialogs on it.
I do not want to create new frame each time I want to show Dialog.
Holoda at 2007-7-10 11:04:36 >

# 9
And inside each class I make personal frame. And I show Dialog with help of Frame of each class.
So, as a result, I have application with three useless frames...
That's bad.
Yes, that's bad. You shouldn't need to create specific classes, you can just generate content for dialogs, either constructing the dialogs yourself or using JOptionPane. There is in practice never any need to extend JFrame.
I do not want to create new frame each time I want to show Dialog.
Indeed. You should instantiate a new JDialog each time you want to show a dialog.
# 10
Thanks! I just working on it.
Holoda at 2007-7-10 11:04:36 >

# 11
I found easier solution:
JOptionPane.showInputDialog(Object message);
It's static method. You can use it everywhere, just
import javax.swing.JOptionPane;
This method returns string, so you can get Dialog result where you need.
The other way:
JOptionPane.showMessageDialog(null,
"Incorrect user/password.",
"Can't establish connection",
JOptionPane.ERROR_MESSAGE);
This static method shows ERROR_MESSAGE.
You can custom it:
Incorrect user/password is Dialog message
Can't establish connection is Dialog description
ERROR_MESSAGE customs Dialog: error/input/confirm e.t.c.
I've used "null" instead of parent component.
That means, that parent frame will be used for Dialog displaying.
Ofcourse, you must create Frame before using this method.
So this is exactly what I need.
I put this dialog code into my
try{...} catch{..}
blocks
and inform user that there an error occured
Message was edited by:
Holod
Holoda at 2007-7-10 11:04:36 >

# 12
The API doc for JOptionPane tells you all of the above and more.
It's also accurate:
I've used "null" instead of parent component. That means, that parent frame will be used for Dialog displaying.
No, it doesn't. It uses a default frame, not the parent frame, which happens to be one that's not on-screen. The result of this is that if your dialog does not remain at the front of the desktop's z-index (eg you click on another window) it will not be brought to the front by clicking on your application. Result: the application is locked and it can be hard for the user to find the dialog to unlock it.
# 13
Yes,Yes, You are right. I've missprinted.
It uses DEFAULT frame.
No, it doesn't. It uses a default frame, not the parent frame, which happens to be one that's not on-screen. The result of this is that if your dialog does not remain at the front of the desktop's z-index (eg you click on another window) it will not be brought to the front by clicking on your application. Result: the application is locked and it can be hard for the user to find the dialog to unlock it.
Hm...Idid not use such case. I will test it
Holoda at 2007-7-10 11:04:36 >
