• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

problem with JDialog

 
Ranch Hand
Posts: 4716
9
Scala Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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???
 
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Sheriff
Posts: 22815
132
Eclipse IDE Spring Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 4716
9
Scala Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 4716
9
Scala Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Bartender
Posts: 5167
11
Netbeans IDE Opera Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Rancher
Posts: 3324
32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
I am a man of mystery. Mostly because of this tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic