我正在实施MVC
,我希望在我的视图中JToolTip
上有一个JLabel
。我成功但现在我想设置解雇和初始计时器。
我阅读了JToolTip和TollTipManager的Javadoc。我在ToolTipManager中找到了我想要的东西,但现在我无法理解如何使用它。
JToolTip
没有为ToolTipManager
设置,他们在文档中说:
管理系统中的所有工具提示。
我真的不明白我应该如何使用ToolTipManager
。我的观点应该延伸ToolTipManager
还是我完全误导?
答案 0 :(得分:10)
ToolTipManager
是一个单身人士。要使用它,您需要调用一个返回此类实例的静态sharedInstance
方法。
ToolTipManager
管理所有工具提示 - 它是一个全局配置类,对sharedInstance
所做的更改将应用于应用程序中的每个工具提示。
答案 1 :(得分:7)
以下示例说明如何使用ToolTipManager:
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.SwingUtilities;
import javax.swing.ToolTipManager;
public class TestTooltips {
protected static void initUI() {
ToolTipManager.sharedInstance().setInitialDelay(0);
ToolTipManager.sharedInstance().setDismissDelay(500);
JFrame frame = new JFrame("test");
JLabel label = new JLabel("Label text");
label.setToolTipText("My cool nice and fun tooltip");
frame.add(label);
frame.pack();
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
initUI();
}
});
}
}
答案 2 :(得分:5)
不知道你尝试了什么,你真的想要...,你是否成功使用以下代码
import java.awt.*;
import java.awt.event.MouseWheelEvent;
import java.awt.event.MouseWheelListener;
import javax.swing.*;
import javax.swing.table.DefaultTableCellRenderer;
public class JScrollableToolTip extends JToolTip {
private static final long serialVersionUID = 1L;
private final MouseWheelListener mouseWheelListener;
private final JTextArea tipArea;
public JScrollableToolTip(final int rows, final int columns) {
setLayout(new BorderLayout());
mouseWheelListener = createMouseWheelListener();
tipArea = new JTextArea(rows, columns);
tipArea.setLineWrap(true);
tipArea.setWrapStyleWord(true);
tipArea.setEditable(false);
LookAndFeel.installColorsAndFont(tipArea, "ToolTip.background",
"ToolTip.foreground", "ToolTip.font");
JScrollPane scrollpane = new JScrollPane(tipArea);
scrollpane.setBorder(null);
scrollpane.getViewport().setOpaque(false);
add(scrollpane);
}
private MouseWheelListener createMouseWheelListener() {
return new MouseWheelListener() {
@Override
public void mouseWheelMoved(final MouseWheelEvent e) {
JComponent component = getComponent();
if (component != null) {
tipArea.dispatchEvent(new MouseWheelEvent(tipArea, e.getID(),
e.getWhen(), e.getModifiers(), 0, 0, e.getClickCount(),
e.isPopupTrigger(), e.getScrollType(), e.getScrollAmount(),
e.getWheelRotation()));
}
}
};
}
@Override
public void addNotify() {
super.addNotify();
JComponent component = getComponent();
if (component != null) {
component.addMouseWheelListener(mouseWheelListener);
}
}
@Override
public void removeNotify() {
JComponent component = getComponent();
if (component != null) {
component.removeMouseWheelListener(mouseWheelListener);
}
super.removeNotify();
}
@Override
public void setComponent(JComponent c) {
JComponent component = getComponent();
if (component != null) {
component.removeMouseWheelListener(mouseWheelListener);
}
super.setComponent(c);
}
@Override
public void setTipText(final String tipText) {
String oldValue = this.tipArea.getText();
tipArea.setText(tipText);
tipArea.setCaretPosition(0);
firePropertyChange("tiptext", oldValue, tipText);
}
@Override
public Dimension getPreferredSize() {
return getComponent(0).getPreferredSize();
}
@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[]) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (Exception e) {
e.printStackTrace();
}
JFrame f = new JFrame("JScrollableToolTip");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setSize(300, 200);
f.setLocationRelativeTo(null);
ToolTipManager.sharedInstance().setInitialDelay(500);
ToolTipManager.sharedInstance().setDismissDelay(10000);
ToolTipManager.sharedInstance().mousePressed(null);
JTable table = new JTable(50, 4) {
private static final long serialVersionUID = 1L;
@Override
public JToolTip createToolTip() {
JScrollableToolTip tip = new JScrollableToolTip(3, 20);
tip.setComponent(this);
return tip;
}
};
table.setDefaultRenderer(Object.class, new DefaultTableCellRenderer() {
private static final long serialVersionUID = 1L;
@Override
public Component getTableCellRendererComponent(JTable table, Object value,
boolean isSelected, boolean hasFocus, int row, int column) {
super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
setToolTipText("Row " + row + " Column " + column
+ "\nUsed 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.");
return this;
}
});
f.add(new JScrollPane(table));
f.setVisible(true);
}
});
}
}