Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

problem with JDialog

 
Randall Twede
Ranch Hand
Posts: 4467
3
Java Python Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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???
 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
> 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
Pie
Posts: 20605
60
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Randall Twede
Ranch Hand
Posts: 4467
3
Java Python Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4467
3
Java Python Scala
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5147
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Rob Camick
Ranch Hand
Posts: 2608
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you want to get a file name the use a JFileChooser.

Also, you should never display a dialog in any painting method.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic