/*
* JScrollableToolTip.java
*/
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class JScrollableToolTip extends JToolTip implements MouseWheelListener {
private JTextArea tipArea;
private JScrollPane scrollpane;
private JComponent comp;
/** Creates a tool tip. */
public JScrollableToolTip(final int width, final int height) {
this(width, height, null);
}
public JScrollableToolTip(final int width, final int height, final JComponent comp) {
this.comp = comp;
setPreferredSize(new Dimension(width, height));
setLayout(new BorderLayout());
tipArea = new JTextArea();
tipArea.setLineWrap(true);
tipArea.setWrapStyleWord(true);
tipArea.setEditable(false);
tipArea.setBackground(new Color(255, 255, 204));
scrollpane = new JScrollPane(tipArea);
add(scrollpane);
if(comp != null){
comp.addMouseWheelListener(this);
}
}
public void mouseWheelMoved(final MouseWheelEvent e) {
scrollpane.dispatchEvent(e);
MouseEvent e2 = new MouseEvent(comp, MouseEvent.MOUSE_MOVED, 0, 0, 0, 0, 0, false);
comp.dispatchEvent(e2);
}
@Override
public void setTipText(final String tipText) {
String oldValue = this.tipArea.getText();
tipArea.setText(tipText);
tipArea.setCaretPosition(0);
firePropertyChange("tiptext", oldValue, tipText);
}
@Override
public String getTipText() {
return tipArea == null ? "" : tipArea.getText();
}
@Override
protected String paramString() {
String tipTextString = (tipArea.getText() != null ? tipArea.getText() : "");
return super.paramString() +
",tipText=" + tipTextString;
}
//for testing only:
public static void main(final String args[]) {
JFrame f = new JFrame();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setSize(300, 100);
f.setLocationRelativeTo(null);
ToolTipManager.sharedInstance().setDismissDelay(30000);
JButton button = new JButton("Used to display...") {
@Override
public JToolTip createToolTip() {
JScrollableToolTip tip = new JScrollableToolTip(200, 80, this);
tip.setComponent(this);
return tip;
}
};
button.setToolTipText("Used to display a 'Tip' for a Component. " +
"Typically components provide api to automate the process of " +
"using ToolTips. For example, any Swing component can use the " +
"JComponent setToolTipText method to specify the text for a standard tooltip.");
f.add(button);
f.setVisible(true);
}
}