Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / GUI / August 2005

Tip: Looking for answers? Try searching our database.

problem with an image in a JScrollPane and JTabbedPane

Thread view: 
mkpls - 01 Aug 2005 19:24 GMT
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


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.