File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

adding scrollbars

 
Randall Twede
Ranch Hand
Posts: 4353
2
Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i added scrollbars to a class that extends JFrame. here is the code i used. image is a BufferedImage and canvas is a JPanel.

it seems to work fine, but it seems too easy. does it look like the way you would do it? the alternative is to set the preferred size in 3 different places.
 
Michael Dunn
Ranch Hand
Posts: 4632
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
> does it look like the way you would do it?

never - painting paints, that's it.

and, for Swing, you should be overriding paintComponent(),
unless paint() belongs to the frame, and you shouldn't be doing that -
the frame is just a window/container.
 
Randall Twede
Ranch Hand
Posts: 4353
2
Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
paint() belongs to the frame. the first version of this program was a homework assignment in 2004. back then i(we) used to extend JFrame for our main GUI class. i never thought about overriding paint() until this need arose. myself i like it. it is better than having

in 3 different places isn't it?
 
Michael Dunn
Ranch Hand
Posts: 4632
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
> myself i like it. it is better than having .... in 3 different places isn't it?

have a read of this current thread (just a few below this thread on main page)

http://www.coderanch.com/t/600458/GUI/java/move-position-position-animation

he creates a Jframe inside paint. is that also OK to do, just because it's an easy place to put it?
 
Darryl Burke
Bartender
Posts: 5116
11
Java Netbeans IDE Opera
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Randall Twede wrote:it is better than having

in 3 different places isn't it?

Why not override getPreferredSize() to return that new Dimension (or super.getPreferredSize() if image is null)?
 
Randall Twede
Ranch Hand
Posts: 4353
2
Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Darryl, thanks for your reply. that was exactly what i wanted: almost

i had a gut feeling there was a simpler way than repeating code(almost always a sign of a poor coding), or calling a very short private method from several spots.

there is still a problem though. when i get an image, say by opening a file, if the image is too large there are no scrollbars until i change the size of the frame(even a tiny bit). any ideas about how to fix this?

and please don't anyone change the subject to why i give the one class package access to a variable in another class. it is by choice.

and michael, i actually agree with your point. that is why i posted the question in the first place.

P.S. for the curious, there are 3 way the image can be too large. they are all menu choices. file/open, edit/paste, and image/scale(if you scale it to be bigger).
 
Randall Twede
Ranch Hand
Posts: 4353
2
Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i thought for a minute i had figured it out. in the action performed for 2 of the possibilities i called a private method that the last line was to repaint the panel. i changed it to repaint the frame but to no avail. i was suspicious this would not work because the third one repainted the frame at the end but still no scrollbars. i will post a little more code from the main class in case it helps.
 
Darryl Burke
Bartender
Posts: 5116
11
Java Netbeans IDE Opera
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
when i get an image, say by opening a file, if the image is too large there are no scrollbars until i change the size of the frame(even a tiny bit). any ideas about how to fix this?

In a word: revalidate(). Not the panel that shows the image, but its parent. After any code that may result in a new preferredSize.
 
Randall Twede
Ranch Hand
Posts: 4353
2
Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks again Darryl! now that you mention it i remember reading something about that.

thank god this program is now almost done. i didn't have much hair left anyway but i have less now.
 
Randall Twede
Ranch Hand
Posts: 4353
2
Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
crap! it didn't work.
i replaced repaint() with revalidate...no change
it tried adding a repaint after that and still no change

here is the entire main class
 
Darryl Burke
Bartender
Posts: 5116
11
Java Netbeans IDE Opera
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You didn't read my response carefully. There's no use revalidating the panel itself.
 
Randall Twede
Ranch Hand
Posts: 4353
2
Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
haha

you didn't read my response closely enough. i called the revaliadate method of the frame and it did not work.
as it turns out when i called the revalidate method of the panel(canvas.revalidate();) , now it does work

thanks so much Darryl! now the code is where it belongs: in getPreferredSize()
and i learned about the revalidate method
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic