1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123
| import java.awt.Color;
import java.awt.Dimension;
import java.awt.GradientPaint;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GridBagLayout;
import java.awt.RenderingHints;
import java.awt.Window;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import com.sun.awt.AWTUtilities;
/**
* Exemple de fenêtre popup personnalisée.
* @author François Vogt
*/
public class ToolTipSample extends Window {
/**
* Numéro de version pour sérialisation
*/
private static final long serialVersionUID = 4247440707680206877L;
/**
* Panneau contenant tout le contenu de la tooltip
*/
private JPanel contentPanel = null;
/**
* Composant sous lequel va s'ouvrir la tooltip
*/
private JComponent parent = null;
/**
* Construit la tooltip pour se positionner en dessous d'un composant
* lorsqu'on la rend visible.
*/
public ToolTipSample(JComponent parent) {
super(null);
this.parent = parent;
this.setSize(new Dimension(200,100));
this.setLocation(200, 200);
this.setAlwaysOnTop(true);
AWTUtilities.setWindowOpaque(this, false);
this.add(getContentPanel());
}
/**
* @return the contentPanel
*/
public JPanel getContentPanel() {
if (contentPanel == null) {
contentPanel = new JPanel(new GridBagLayout());
contentPanel.setOpaque(false);
contentPanel.add(new JLabel("Ceci est un "));
contentPanel.add(new JButton("test"));
}
return contentPanel;
}
/*
* (non-Javadoc)
* @see java.awt.Window#setVisible(boolean)
*/
@Override
public void setVisible(boolean b) {
if (b) {
int x = (int)(parent.getLocationOnScreen().getX());
int y = (int)(parent.getLocationOnScreen().getY()
+ parent.getPreferredSize().getHeight());
this.setLocation(x, y);
}
super.setVisible(b);
}
/*
* (non-Javadoc)
* @see javax.swing.JComponent#paint(java.awt.Graphics)
*/
@Override
public void paint(Graphics g) {
Graphics2D g2d = (Graphics2D) g;
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
g2d.setPaint(new Color(63,63,63,50));
g2d.fillRoundRect(3, 3, getWidth()-3, getHeight()-3, 5, 5);
GradientPaint paint = new GradientPaint(0, 0, new Color(255,255,255),
0, getHeight(), new Color(204,204,204));
g2d.setPaint(paint);
g2d.fillRoundRect(1, 1, getWidth()-5, getHeight()-5, 5, 5);
g2d.setPaint(new Color(164,164,164));
g2d.drawRoundRect(0, 0, getWidth()-4, getHeight()-4, 6, 6);
super.paintComponents(g2d);
}
public static void main(String[] args) throws IOException {
JFrame frame = new JFrame("Test");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
final JButton button = new JButton("Ouvrir/Masquer ToolTip");
button.addActionListener(new ActionListener() {
private ToolTipSample tooltip = new ToolTipSample(button);
/*
* (non-Javadoc)
* @see java.awt.event.ActionListener#actionPerformed(
* java.awt.event.ActionEvent)
*/
@Override
public void actionPerformed(ActionEvent e) {
tooltip.setVisible(!tooltip.isVisible());
}
});
frame.add(button);
frame.pack();
frame.setVisible(true);
}
} |