Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

jpanel help

 
rob armstrong
Ranch Hand
Posts: 77
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
First i tried to paint on a extended JFrame which had a picture in the background... That worked until i added the scrollbar than it would no longer paint. I learned that adding scrollbars are bad to do on JFrames so i rewrote the program.

Now i have a extended JFrame calling a constructor to my extended JPanel.
The extended JPanel will than of course be added to the JFrame which as a bunch of JMenus.
Now I can get the picture again on the JPanel on the JFrame but i can't get the scrollbar to work or show up. Attached is my code for the JPanel part of it...
I think the JFrame is fine cause all it does is constructs the JPanel.

public Builder10Panel()
{
ImageIcon picture=new ImageIcon("c:\\programing\\javaclasses\\builder\\9998.jpg"); //file chooser?? must choose picture file here
img=picture.getImage();
//this.add(new PaintSurface(), BorderLayout.CENTER);
// ScrollBar area
JLabel labelPic=new JLabel(picture);
this.add(labelPic);
JScrollPane scroll=new JScrollPane(labelPic, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
//scroll.setViewportView(labelPic);


this.add(scroll); // does not draw after executing this line of code???
//ScrollBar end
this.setVisible(true);
}


}

thanks for your advise.
roba
 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
// this.add(new PaintSurface(), BorderLayout.CENTER);
assuming 'this' refers to the frame, or a panel set as a BorderLayout,
your problems are these

you add labelPic to the container
this.add(labelPic);

you also add it to the scrollPane
JScrollPane scroll=new JScrollPane(labelPic, .......
(or, try to add it - it can't be in two places at once)

the scrollpane is then added to the container
this.add(scroll);

because the container is a BorderLayout, if no location is specifed,
BorderLayout.CENTER becomes the default. so, when you add the label, it
is added to CENTER, then you add the scrollpane (which you've tried to
also add the label to) - it also goes to CENTER, knocking the label out.
so you end up with a scrollpane with nothing in it.

this is one of the normal ways to achieve what you want

create a label, with an imageicon
add the label to a panel
add the panel to a scrollpane
add the scrollpane to another panel //optional - can be added to the frame
add the panel(or scrollpane) to the frame/contentPane

for the scrollpane to work properly, it must be smaller than the component
it contains - so you need to set some preferred sizes, either the panel it
contains, ot the scrollpane itself (depending on how you set things up)
 
rob armstrong
Ranch Hand
Posts: 77
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the advise. Still haveing some trouble with it though...

public class TrackBuilder10Panel extends JPanel
{
Image img;
ImageIcon picture;
JButton hi;
JLabel labelPic;
JScrollPane scroll;


public TrackBuilder10Panel()
{
ImageIcon picture=new ImageIcon("c:\\programing\\javaclasses\\Trackbuilder\\9998.jpg"); //file chooser?? must choose picture file here
img=picture.getImage();
//this.add(new PaintSurface(), BorderLayout.CENTER);
// ScrollBar area
JLabel labelPic=new JLabel(picture);
this.add(labelPic);
JScrollPane scroll=new JScrollPane(labelPic, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
//scroll.setViewportView(labelPic);
scroll.setScrollPosition(200,200);
scroll.add(this);



//this.add(scroll); // does not draw after executing this line of code???
//ScrollBar end
this.setVisible(true);
}


}


I think i completed the tasks you listed. But it still has no scrollbar on it.



JFrame included here as well ****************************************
class TrackBuilder10Main extends JFrame
{
blah bhlah

TrackBuilder10Panel pane1=new TrackBuilder10Panel();

//this.add(scroll); // does not draw after executing this line of code???
//this.add(pane1), BorderLayout.NORTH);
this.setVisible(true);
}
}


Thanks
roba
 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
> I think i completed the tasks you listed. But it still has no scrollbar on it.

the code on ther subsequent lines doesn't show much.
at this stage it's probably easier to show you a simple example



your 2nd post, with the additional line of extends JPanel etc, indicates no
change to the default layout of the panel (which is FlowLayout). This means
your original code (trying to add labalPic to two places) should have shown
the label with the icon on the left, and a very tiny square (almost a square dot)
on the right - this is because it does not contain anything, and has no size.

if you have not been seeing the image, there is a problem with the image.
 
rob armstrong
Ranch Hand
Posts: 77
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok with a bit of tweaking that works great. Thank you.

I guess i should have mentioned that i have a very large jpg pic. about 7000x7000 which i want to view on about 800x800. Since I plan to use various sized ImageIcons is there a way to adjust the size to the size of the pic or do i write the code myself or do you think there is a helpful method?
Thanks
roba
 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
not sure what you're asking:
you can certainly size the panel to (7000,7000)

if you want the image smaller, you can add it to
a panel via paintComponent(Graphics g) and
g.drawImage() - one of the overloaded ones with
width/height arguments.
 
rob armstrong
Ranch Hand
Posts: 77
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yep didn't get time to look yet but sounds like we are on the right path. What I was trying to say was the pictures (ImageIcons) will be loaded from the harddrive and will be various sizes however I want all of them to be viewable on a 800,800 container. With the info you gave me it sounds like it will work fine. Thank you very much.
roba
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic