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;
}
}
|
|
0
|
|
|
|
Reply
|
rdbsJUNKREJECTED
|
7/13/2004 2:03:16 AM |
|
> 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
|
|
0
|
|
|
|
Reply
|
rdbsJUNKREJECTED
|
7/13/2004 12:26:34 PM
|
|