-
Notifications
You must be signed in to change notification settings - Fork 3
/
ToolTipListenerDemo.java
124 lines (105 loc) · 3.15 KB
/
ToolTipListenerDemo.java
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
124
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.net.*;
import javax.swing.*;
import javax.swing.border.*;
import javax.swing.event.*;
public class ToolTipListenerDemo extends JPanel
implements ActionListener
{
private JRadioButton noListener;
private JRadioButton mouseWheelListener;
private JRadioButton adjustmentListener;
private JRadioButton componentListener;
ToolTipListenerDemo()
{
setLayout( new BoxLayout(this,BoxLayout.X_AXIS) );
setBorder( new EmptyBorder(10, 10, 10, 10) );
JComponent right = createRightPanel();
JComponent left = createLeftPanel();
add(left);
add( Box.createHorizontalStrut(10) );
add(right);
}
public JComponent createLeftPanel()
{
JTable table = new JTable(50, 50)
{
public String getToolTipText( MouseEvent e )
{
int row = rowAtPoint( e.getPoint() );
int column = columnAtPoint( e.getPoint() );
return "(" + row+ "," + column + ")";
}
};
table.setAutoResizeMode( JTable.AUTO_RESIZE_OFF );
JScrollPane scrollPane = new JScrollPane( table );
scrollPane.setPreferredSize( new Dimension(300, 240) );
scrollPane.getViewport().getView().requestFocusInWindow();
ToolTipListener listener = new ToolTipListener();
if (mouseWheelListener.isSelected())
{
scrollPane.addMouseWheelListener(listener);
}
if (adjustmentListener.isSelected())
{
scrollPane.getVerticalScrollBar().addAdjustmentListener( listener );
}
if (componentListener.isSelected())
{
table.addComponentListener( listener );
}
return scrollPane;
}
public JPanel createRightPanel()
{
noListener = new JRadioButton("No Listener");
mouseWheelListener = new JRadioButton("MouseWheel Listener");
adjustmentListener = new JRadioButton("Adjustment Listener");
componentListener = new JRadioButton("Component Listener");
noListener.setSelected( true );
ButtonGroup bg1 = new ButtonGroup();
bg1.add( noListener );
bg1.add( mouseWheelListener );
bg1.add( adjustmentListener );
bg1.add( componentListener );
JPanel p1 = new JPanel( new GridLayout(0, 1) );
p1.setBorder( new TitledBorder("Select Listener to Use") );
p1.add( noListener );
p1.add( mouseWheelListener );
p1.add( adjustmentListener );
p1.add( componentListener );
noListener.addActionListener(this);
mouseWheelListener.addActionListener(this);
adjustmentListener.addActionListener(this);
componentListener.addActionListener(this);
return p1;
}
public void actionPerformed(ActionEvent e)
{
remove(0);
JComponent component = createLeftPanel();
add(component, 0);
revalidate();
repaint();
component.requestFocusInWindow();
}
public static void main(String[] args)
{
SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}
public static void createAndShowGUI()
{
JFrame frame = new JFrame("ToolTip Listener");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add( new ToolTipListenerDemo() );
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
}