fubarable

Untitled

Sep 1st, 2020
1,248
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.89 KB | None | 0 0
  1. import java.awt.Component;
  2. import java.awt.event.*;
  3. import javax.swing.*;
  4.  
  5. public class ShowButton {
  6.     static JButton button;
  7.  
  8.     public static void main (String [] args)
  9.     {
  10.         JFrame frame = new JFrame();
  11.         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  12.         frame.setSize(400, 400);
  13.         frame.setVisible(true);
  14.        
  15.         final JPanel panel = new JPanel();
  16.         panel.addMouseListener(new HighlightJPanels());
  17.         panel.setOpaque(false);
  18.         panel.setVisible(true);
  19.         frame.add(panel);
  20.        
  21.         button = new JButton("Button");
  22.        
  23.         button.addMouseListener(new MouseAdapter() {
  24.             public void mouseClicked(MouseEvent e) {
  25.                 redispatchToParent(e);
  26.             }
  27.  
  28.             public void mousePressed(MouseEvent e) {
  29.                 redispatchToParent(e);
  30.             }
  31.  
  32.             public void mouseReleased(MouseEvent e) {
  33.                 redispatchToParent(e);
  34.             }
  35.  
  36.             public void mouseEntered(MouseEvent e) {
  37.                 redispatchToParent(e);
  38.             }
  39.  
  40.             public void mouseExited(MouseEvent e) {
  41.                 redispatchToParent(e);
  42.             }
  43.  
  44.             public void mouseWheelMoved(MouseWheelEvent e){
  45.                 redispatchToParent(e);
  46.             }
  47.  
  48.             public void mouseDragged(MouseEvent e){
  49.                 redispatchToParent(e);
  50.             }
  51.  
  52.             public void mouseMoved(MouseEvent e) {
  53.                 redispatchToParent(e);
  54.             }
  55.  
  56.             private void redispatchToParent(MouseEvent e){
  57.                 Component source = (Component) e.getSource();
  58.                 MouseEvent parentEvent = SwingUtilities.convertMouseEvent(source, e, source.getParent());
  59.                 source.getParent().dispatchEvent(parentEvent);
  60.             }
  61.            
  62.         });
  63.        
  64.         button.addActionListener(new ActionListener() {
  65.             public void actionPerformed(ActionEvent e)
  66.             {
  67.                 System.out.println("Yay you clicked the button");
  68.             }
  69.         });
  70.         panel.add(button);
  71.     }
  72.  
  73.     public static class HighlightJPanels extends MouseAdapter {
  74.         public void mouseEntered(MouseEvent e)
  75.         {
  76.             button.setVisible(true);
  77.         }
  78.  
  79.         public void mouseExited(MouseEvent e)
  80.         {
  81.             button.setVisible(false);
  82.         }
  83.  
  84.     }
  85. }
Add Comment
Please, Sign In to add comment