• Post Reply Bookmark Topic Watch Topic
  • New Topic

Displaying an image in a subpanel  RSS feed

 
Roy Cinco
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • 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?
 
Craig Wood
Ranch Hand
Posts: 1535
  • Mark post as helpful
  • send pies
  • 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
  • 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.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!