Hello,
i've got a problem. I want to scroll a picture which is in a
JScrollPane and this JScrollPane is a tab of a JTabbedpane.
Here is a part of the source code:
....
private JTabbedPane JMainTab = new JTabbedPane();
private JPanel plInfo = new JPanel();
private JPanel plZustand = new JPanel();
public JScrollPane Scroller = new JScrollPane();
public ImagePanel IP = new ImagePanel("dcschutz.jpg");
Scroller.add (IP);
JMainTab.addTab("Informationen",null,plInfo,"Informationen zum
geladenen Plan.");
JMainTab.addTab("Plan",null ,Scroller,"Den geladenen Plan anzeigen");
JMainTab.addTab("Zustände",null, plZustand,"Einzelne Zustände laden
und speichern");
cp.add(JMainTab);
....
the ImagePanel class
import java.awt.*;
import java.awt.event.*;
import java.awt.image.BufferedImage;
import javax.swing.*;
public class ImagePanel extends JPanel {
protected BufferedImage iImage = null;
protected int iImageWidth = 0;
protected int iImageHeight = 0;
public ImagePanel(String imageName) {
super(false);
setBackground(Color.black);
iImage=loadImage(imageName);
if (iImage!=null) {
iImageWidth=iImage.getWidth();
iImageHeight=iImage.getHeight();
setPreferredSize(new Dimension(iImageWidth,iImageHeight));
}
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
Insets insets=getInsets();
if (iImage!=null) {
g.drawImage(iImage,insets.left,insets.top,null);
}
}
private BufferedImage loadImage(String fileName) {
Image image=Toolkit.getDefaultToolkit().getImage(fileName);
MediaTracker mt=new MediaTracker(this);
mt.addImage(image,0);
try {
mt.waitForID(0);
}
catch (InterruptedException ie) {
return null;
}
if (mt.isErrorID(0)) return null;
BufferedImage bufferedImage = new BufferedImage(
image.getWidth(null), image.getHeight(null),
BufferedImage.TYPE_INT_RGB);
Graphics2D g2d=bufferedImage.createGraphics();
g2d.drawImage(image,null,null);
return bufferedImage;
}
}
when i start the programm and switch to the "Plan"-tab, i can see
nothing. But when I replace the JScrollPane with the ScrollPane from
AWT it runs. but I have to use swing because the items in the menubar
are under the Scrollpane.
could somebody help me???
Vova Reznik - 01 Aug 2005 21:36 GMT
> Scroller.add (IP);
Use instead
Scroller.setViewportView(IP);
or do it while initialization:
public ImagePanel IP = new ImagePanel("dcschutz.jpg");
public JScrollPane Scroller = new JScrollPane(IP);
bad style, what if FileNotFoundException