|
Проблема с JToolTip
#37841810
Ссылка:
Ссылка на сообщение:
Ссылка с названием темы:
|
|
|
|
Мне нужна была всплывающая подсказка со скроллом и возможность выделять текст для копипаста. Нашел пример со скроллом, но с выделением там беда какая-то, оно то работает, то нет. Отловить баг не получается. Гляньте, может кто что подскажет?
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.
package example;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.BevelBorder;
import org.jdesktop.swingx.JXTextArea;
public class JScrollableToolTip extends JToolTip implements MouseWheelListener {
private JXTextArea tipArea;
/** Creates a tool tip. */
public JScrollableToolTip(final int width, final int height) {
setPreferredSize(new Dimension(width, height));
setLayout(new BorderLayout());
tipArea = new JXTextArea();
tipArea.setLineWrap(true);
tipArea.setWrapStyleWord(true);
tipArea.setEditable(true);
//tipArea.setBorder(new BevelBorder(BevelBorder.LOWERED, null, null, null, null));
LookAndFeel.installColorsAndFont(tipArea,
"ToolTip.background",
"ToolTip.foreground",
"ToolTip.font");
//LookAndFeel.installBorder(tipArea, "InternalFrame.border");
JScrollPane scrollpane = new JScrollPane(tipArea);
scrollpane.setBorder(UIManager.getBorder("InternalFrame.border"));
scrollpane.getViewport().setOpaque(false);
add(scrollpane);
}
@Override
public void addNotify() {
super.addNotify();
JComponent comp = getComponent();
if (comp != null) {
comp.addMouseWheelListener(this);
}
}
@Override
public void removeNotify() {
JComponent comp = getComponent();
if(comp != null) {
comp.removeMouseWheelListener(this);
}
super.removeNotify();
}
public void mouseWheelMoved(final MouseWheelEvent e) {
JComponent comp = getComponent();
if(comp != 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 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(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().setDismissDelay(Integer.MAX_VALUE);
JTable table = new JTable(50, 4) {
@Override
public JToolTip createToolTip() {
JScrollableToolTip tip = new JScrollableToolTip(200, 80);
tip.setComponent(this);
return tip;
}
};
table.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(new JScrollPane(table));
f.setVisible(true);
}
});
}
}
|
|
|