|
| 1 | +import java.awt.*; |
| 2 | +import java.awt.event.*; |
| 3 | +import java.io.*; |
| 4 | +import java.net.*; |
| 5 | +import javax.swing.*; |
| 6 | +import javax.swing.border.*; |
| 7 | +import javax.swing.event.*; |
| 8 | + |
| 9 | +public class ToolTipListenerDemo extends JPanel |
| 10 | + implements ActionListener |
| 11 | +{ |
| 12 | + private JRadioButton noListener; |
| 13 | + private JRadioButton mouseWheelListener; |
| 14 | + private JRadioButton adjustmentListener; |
| 15 | + private JRadioButton componentListener; |
| 16 | + |
| 17 | + ToolTipListenerDemo() |
| 18 | + { |
| 19 | + setLayout( new BoxLayout(this,BoxLayout.X_AXIS) ); |
| 20 | + setBorder( new EmptyBorder(10, 10, 10, 10) ); |
| 21 | + |
| 22 | + JComponent right = createRightPanel(); |
| 23 | + JComponent left = createLeftPanel(); |
| 24 | + |
| 25 | + add(left); |
| 26 | + add( Box.createHorizontalStrut(10) ); |
| 27 | + add(right); |
| 28 | + } |
| 29 | + |
| 30 | + public JComponent createLeftPanel() |
| 31 | + { |
| 32 | + JTable table = new JTable(50, 50) |
| 33 | + { |
| 34 | + public String getToolTipText( MouseEvent e ) |
| 35 | + { |
| 36 | + int row = rowAtPoint( e.getPoint() ); |
| 37 | + int column = columnAtPoint( e.getPoint() ); |
| 38 | + return "(" + row+ "," + column + ")"; |
| 39 | + } |
| 40 | + }; |
| 41 | + table.setAutoResizeMode( JTable.AUTO_RESIZE_OFF ); |
| 42 | + |
| 43 | + JScrollPane scrollPane = new JScrollPane( table ); |
| 44 | + scrollPane.setPreferredSize( new Dimension(300, 240) ); |
| 45 | + scrollPane.getViewport().getView().requestFocusInWindow(); |
| 46 | + |
| 47 | + ToolTipListener listener = new ToolTipListener(); |
| 48 | + |
| 49 | + if (mouseWheelListener.isSelected()) |
| 50 | + { |
| 51 | + scrollPane.addMouseWheelListener(listener); |
| 52 | + } |
| 53 | + if (adjustmentListener.isSelected()) |
| 54 | + { |
| 55 | + scrollPane.getVerticalScrollBar().addAdjustmentListener( listener ); |
| 56 | + } |
| 57 | + if (componentListener.isSelected()) |
| 58 | + { |
| 59 | + table.addComponentListener( listener ); |
| 60 | + } |
| 61 | + |
| 62 | + return scrollPane; |
| 63 | + } |
| 64 | + |
| 65 | + public JPanel createRightPanel() |
| 66 | + { |
| 67 | + noListener = new JRadioButton("No Listener"); |
| 68 | + mouseWheelListener = new JRadioButton("MouseWheel Listener"); |
| 69 | + adjustmentListener = new JRadioButton("Adjustment Listener"); |
| 70 | + componentListener = new JRadioButton("Component Listener"); |
| 71 | + noListener.setSelected( true ); |
| 72 | + |
| 73 | + ButtonGroup bg1 = new ButtonGroup(); |
| 74 | + bg1.add( noListener ); |
| 75 | + bg1.add( mouseWheelListener ); |
| 76 | + bg1.add( adjustmentListener ); |
| 77 | + bg1.add( componentListener ); |
| 78 | + |
| 79 | + JPanel p1 = new JPanel( new GridLayout(0, 1) ); |
| 80 | + p1.setBorder( new TitledBorder("Select Listener to Use") ); |
| 81 | + p1.add( noListener ); |
| 82 | + p1.add( mouseWheelListener ); |
| 83 | + p1.add( adjustmentListener ); |
| 84 | + p1.add( componentListener ); |
| 85 | + |
| 86 | + noListener.addActionListener(this); |
| 87 | + mouseWheelListener.addActionListener(this); |
| 88 | + adjustmentListener.addActionListener(this); |
| 89 | + componentListener.addActionListener(this); |
| 90 | + |
| 91 | + return p1; |
| 92 | + } |
| 93 | + |
| 94 | + |
| 95 | + public void actionPerformed(ActionEvent e) |
| 96 | + { |
| 97 | + remove(0); |
| 98 | + JComponent component = createLeftPanel(); |
| 99 | + add(component, 0); |
| 100 | + revalidate(); |
| 101 | + repaint(); |
| 102 | + component.requestFocusInWindow(); |
| 103 | + } |
| 104 | + |
| 105 | + public static void main(String[] args) |
| 106 | + { |
| 107 | + SwingUtilities.invokeLater(new Runnable() { |
| 108 | + public void run() { |
| 109 | + createAndShowGUI(); |
| 110 | + } |
| 111 | + }); |
| 112 | + } |
| 113 | + |
| 114 | + public static void createAndShowGUI() |
| 115 | + { |
| 116 | + JFrame frame = new JFrame("ToolTip Listener"); |
| 117 | + frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); |
| 118 | + frame.add( new ToolTipListenerDemo() ); |
| 119 | + frame.pack(); |
| 120 | + frame.setLocationRelativeTo(null); |
| 121 | + frame.setVisible(true); |
| 122 | + |
| 123 | + } |
| 124 | +} |
0 commit comments