aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes problem with JDialog Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "problem with JDialog" Watch "problem with JDialog" New topic
Author

problem with JDialog

Randall Twede
Ranch Hand

Joined: Oct 21, 2000
Posts: 4347
    
    2

i dont get this at all. i have used JOptionPane in the past with no problem. here is the relevant code

if the file is an image file it opens it. but if it is not it displays a blank modal dialog with the title Error, but no message and no ok button so i cant close it. i have checked the API and the tutorial but they both say i am doing it correctly
it also throws exceptions the one at the top says:

exception in thread "AWT-EventQueue-0" java.lang.NullPointerEception

i tried replaceing the parameter message with the string literal with the same result. it didnt throw an exception at first and i managed to get it to close using the X in the title bar(which it should'nt do). i kept toying with it and the next time it did throw the exception. it says it is in paintComponent() line 75. im not using an IDE so im not sure but it has to be the line that opens the JDialog. what can possibly be null??? not parent since it displays the image file. not the String. so what is going on???


SCJP
Visit my download page
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
> problem with JDialog

so, where's the JDialog?

if the code you posted is inside a method (unlikely), then:

private Paint parent;
followed by
if(parent.file != null)
has to throw a NPE

> g.drawImage(image, 0, 0, null);

I hope you're not using getGraphics(), but if it's in paintComponent() and
you have a JOptionPane there, slash your wrists and start again.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19761
    
  20

Michael is right. getGraphics() is an evil method that should never be used. He's also right about not displaying any dialogs from the painting methods. You should initialize the image from somewhere (a button press, constructor, etc), then call repaint(). Your overridden paintComponent method should then draw the image if it's not null.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Randall Twede
Ranch Hand

Joined: Oct 21, 2000
Posts: 4347
    
    2

ok, i'll post the entire class:



you say it is because i am displaying the dialog from inside paintComponent() so i will see what i can do with that knowledge.
Randall Twede
Ranch Hand

Joined: Oct 21, 2000
Posts: 4347
    
    2

i did as you suggested and moved most of the code to actionPerformed() in the JFrame. in paintComponent() i only have this now:

it works fine now
Darryl Burke
Bartender

Joined: May 03, 2008
Posts: 4658
    
    5

Just one point: Paint is a bad choice of name for a JFrame subclass, as there is already a java.awt.Paint interface in the standard JDK.


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

Joined: Jun 13, 2009
Posts: 2216
    
    7
If you want to get a file name the use a JFileChooser.

Also, you should never display a dialog in any painting method.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: problem with JDialog