• 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:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

Displaying an image in a subpanel

 
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi, hope someone can help!

My program is a public class MyJFrame that extends JFrame (the main() is in here). It can pop up a modal dialog, MyJDialog that extends JDialog. MyJDialog actually instantiates MyImageJPanel, and in the MyJDialog constructor does a setContentPane(MyImageJPanel). MyImageJPanel looks like this:



What my program can do right now is allow people to pick picture files with JFileChooser, and then when they're done, they click on another button which brings up the modal dialog which displays all of the pictures they've chosen. It just cycles through them automatically using a Timer in MyJDialog. When the it gets the Timer event, it will call the repaint() method. (My code also calls repaint when the dialog is first brought up.) This all works right now.

However, what I really want to do is add some controls to the dialog. For example, I want to add some buttons so that someone can manually step through the pictures. I thought this would be easy! I figured it would just be a matter of adding a main panel to MYJDialog, adding the image panel to the main panel, and then adding another panel to hold the buttons to main panel.



I see my new button, but I don't see the pictures anymore. Conversely, if I change the last line to setContentPane(imagePanel) then I get my pictures like before, but there is no new button. It doesn't seem like these panels are co-existing with each other! How can I work it, so that I have both the pictures working the way they were, sitting alongside some new buttons, both of them in the dialog?
 
Ranch Hand
Posts: 1535
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The default layout manager for JDialog is BorderLayout. So you could skip the intermediate
"mainPanel" and add your two components directly to the center and south sections of the
BorderLayout:

Also, unrelated, it is better to not load images in the paintComponent method.
Load them in or via the class constructor or a method.
 
Roy Cinco
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It works! Thanks a lot!!!

I wish I had posted here earlier instead of doing this -->

When you (or anyone else) has some time, maybe you can explain why my way didn't work. Your solution makes sense, but it seems like the other way should work too? It's probably something simple...

By the way, I used the method of overriding paintComponent() because that was in the example I started with.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic