aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes adding scrollbars Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "adding scrollbars" Watch "adding scrollbars" New topic
Author

adding scrollbars

Randall Twede
Ranch Hand

Joined: Oct 21, 2000
Posts: 4339
    
    2

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.


SCJP
Visit my download page
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
> 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

Joined: Oct 21, 2000
Posts: 4339
    
    2

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

Joined: Jun 09, 2003
Posts: 4632
> 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

Joined: May 03, 2008
Posts: 4523
    
    5

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)?


luck, db
There are no new questions, but there may be new answers.
Randall Twede
Ranch Hand

Joined: Oct 21, 2000
Posts: 4339
    
    2

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

Joined: Oct 21, 2000
Posts: 4339
    
    2

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

Joined: May 03, 2008
Posts: 4523
    
    5

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

Joined: Oct 21, 2000
Posts: 4339
    
    2

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

Joined: Oct 21, 2000
Posts: 4339
    
    2

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

Joined: May 03, 2008
Posts: 4523
    
    5

You didn't read my response carefully. There's no use revalidating the panel itself.
Randall Twede
Ranch Hand

Joined: Oct 21, 2000
Posts: 4339
    
    2

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
 
subject: adding scrollbars
 
Similar Threads
Jrame not visible when Try to run from Unit Test
Which mail client do you prefer - Outlook or lotus notes?
Table Viewer GUI from a Database
Spring WEB MVC 3.0 using org.springframework.jdbc.core.simple.SimpleJdbcDaoSupport
java.io.FileNotFoundException: Too many open files