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 / July 2004

Tip: Looking for answers? Try searching our database.

Background not repainted after display of tooltip.

Thread view: 
Rick - 13 Jul 2004 03:03 GMT
I have a JPanel subclass to which I add a regular JPanel. The JPanel I
add has a tooltip. After this tooltip appears and disappears, the area
occupied by the tooltip is not redrawn correctly.

My subclass of JPanel has a custom paintComponent() method that draws
an awt.Image (created from JPEG file) on the JPanel's Graphics. Is
this problem caused because of mixing awt (Image) and swing (JPanel)?

Below is a simpified version of my code. The code does not do anything
useful now except show the problem. Before running the code change the
path name used in the setImage() method invoked in the ImagePanel
constructor to a valid image file path on your computer.

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JPanel;
import java.awt.Dimension;
import java.awt.Color;
import java.awt.Image;
import java.awt.Graphics;
import java.awt.MediaTracker;

public class Viewer extends JFrame {
   
    protected JScrollPane             scrPicturePane;
    protected ImagePanel             pnlImage;
   
    private class ImagePanel extends JPanel  {

        private Image             image = null;
       
        public ImagePanel(){
       
            JPanel p  = new JPanel();
            p.setPreferredSize(new Dimension(100,100));
            p.setBackground(new Color(255, 255, 0, 255 * 50/100));
            p.setToolTipText("tooltip text");
            add(p);

            setImage("C:/Pictures/FormSample19.jpg");  
       
        }

        public java.awt.Dimension getPreferredSize(){
       
            if (image == null)
                return new java.awt.Dimension(130, 130);
            else
                return new java.awt.Dimension(image.getWidth(this),
image.getHeight(this));
           
        }
       
        public void paintComponent(Graphics g) {
            super.paintComponent(g);
            if (image != null) {
                g.drawImage(image,0,0,null);
            }
        }
       
        public void setImage(String pathToImageFile) {
               
            image = getToolkit().getImage(pathToImageFile);
            MediaTracker mediaTracker = new MediaTracker(this);
            mediaTracker.addImage(image, 0);
            try {
                mediaTracker.waitForID(0);
               
            } catch (InterruptedException ie) {
                ie.printStackTrace();
            }
               
            revalidate();
            repaint();
           
        }

    }

    /**
    * Constructs a Viewer
    */
    public Viewer() {
        getContentPane().setLayout(new GridBagLayout());
       
        GridBagConstraints gbcPicturePane = new GridBagConstraints();
        gbcPicturePane.gridx = 0;
        gbcPicturePane.gridy = 1;
        gbcPicturePane.gridwidth = 6;
        gbcPicturePane.gridheight = 4;
        gbcPicturePane.weightx = 1.0;
        gbcPicturePane.weighty = 1.0;
        gbcPicturePane.fill = GridBagConstraints.BOTH;
        gbcPicturePane.anchor = GridBagConstraints.CENTER;
        gbcPicturePane.insets = new java.awt.Insets(10, 10, 10, 10);
        getContentPane().add(getScrPicturePane(), gbcPicturePane);

        setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        setSize(800, 600);
   
    }

    public static void main(String[] args) {
    Viewer v = new Viewer();
    v.setVisible(true);
    }

   
    public JScrollPane getScrPicturePane() {
        if (scrPicturePane == null) {
            scrPicturePane = new JScrollPane(new ImagePanel());
            scrPicturePane
                    .setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
            scrPicturePane
                    .setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
        }
        return scrPicturePane;
    }

}
Rick - 13 Jul 2004 13:26 GMT
> Below is a simpified version of my code. The code does not do anything
> useful now except show the problem.

In the sample code, a pale yellow JPanel will appear painted over the
image you specify. Place the cursor on this panel to expose the
tooltip in such a way that the tooltip window is displayed
_completely_ within the bounds of the yellow JPanel, the area behind
the tooltip will not be redrawn correctly.

thanks

Rick


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.