Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.awt.Component;
- import java.awt.event.*;
- import javax.swing.*;
- public class ShowButton {
- static JButton button;
- public static void main (String [] args)
- {
- JFrame frame = new JFrame();
- frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- frame.setSize(400, 400);
- frame.setVisible(true);
- final JPanel panel = new JPanel();
- panel.addMouseListener(new HighlightJPanels());
- panel.setOpaque(false);
- panel.setVisible(true);
- frame.add(panel);
- button = new JButton("Button");
- button.addMouseListener(new MouseAdapter() {
- public void mouseClicked(MouseEvent e) {
- redispatchToParent(e);
- }
- public void mousePressed(MouseEvent e) {
- redispatchToParent(e);
- }
- public void mouseReleased(MouseEvent e) {
- redispatchToParent(e);
- }
- public void mouseEntered(MouseEvent e) {
- redispatchToParent(e);
- }
- public void mouseExited(MouseEvent e) {
- redispatchToParent(e);
- }
- public void mouseWheelMoved(MouseWheelEvent e){
- redispatchToParent(e);
- }
- public void mouseDragged(MouseEvent e){
- redispatchToParent(e);
- }
- public void mouseMoved(MouseEvent e) {
- redispatchToParent(e);
- }
- private void redispatchToParent(MouseEvent e){
- Component source = (Component) e.getSource();
- MouseEvent parentEvent = SwingUtilities.convertMouseEvent(source, e, source.getParent());
- source.getParent().dispatchEvent(parentEvent);
- }
- });
- button.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent e)
- {
- System.out.println("Yay you clicked the button");
- }
- });
- panel.add(button);
- }
- public static class HighlightJPanels extends MouseAdapter {
- public void mouseEntered(MouseEvent e)
- {
- button.setVisible(true);
- }
- public void mouseExited(MouseEvent e)
- {
- button.setVisible(false);
- }
- }
- }
Add Comment
Please, Sign In to add comment