Results 1 to 5 of 5
  1. #1
    Adomini is offline Member
    Join Date
    Aug 2010
    Posts
    70
    Rep Power
    0

    Default MouseListener - Flashing label

    Good morning again everyone. I am trying to create a program that has a flashing label , and it stops flashing when we click/hold on the mouse, then starts flashing again when we release the click on the mouse. I thought I have all the code below, so it flashes, but when I try to use the MouseListener, the MouseListener is not working. The flashing never stops. '

    Can someone point me in the right direction? Like, was MouseListener not declared... etc etc. There are no compiler errors at all. Thanks to all moderators and people who help us noobs out there........

    Java Code:
    
    import java.awt.*;
    import java.awt.event.*;
    
    import javax.swing.*;
    
    public class FlashingLabel extends JFrame 
    {
      public FlashingLabel() 
      {
        this.getContentPane().add(new FlashLabel("Welcome to Java"));
      }
    
      // Main method
      public static void main(String[] args) 
      {
        // Create a frame
        JFrame frame = new FlashingLabel();
        frame.setTitle("Flashing Label");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    
        // Display the frame
        frame.setSize(200, 200);
        frame.setLocationRelativeTo(null); // Center the frame
        frame.setVisible(true);
      }
    
      class FlashLabel extends JPanel implements ActionListener, MouseListener 
      {
        private boolean show = true;
        private Timer timer = new Timer(200, this);
    
        String label = "Welcome to Java";
    
        public FlashLabel(String label) 
        {
          this.label = label;
          timer.start();
        }
    
        public void setLabel(String label)
        {
          this.label = label;
        }
    
     
    
        public void paintComponent(Graphics g) 
        {
          super.paintComponent(g);
    
          if (show)
            g.drawString(label, 20, 20);
          show = !show;
        }
    
        public Dimension getPreferredSize() 
        {
          return new Dimension(200, 50);
        }
    
        public void mouseClicked(MouseEvent e) 
        {
        }
    
        public void mousePressed(MouseEvent e) 
        {
          timer.stop();
        }
    
        public void mouseReleased(MouseEvent e) 
        {
          timer.start();
        }
    
        public void mouseEntered(MouseEvent e) 
        {
        }
    
        public void mouseExited(MouseEvent e) 
        {
        }
    
        public void actionPerformed(java.awt.event.ActionEvent actionEvent) 
        {
          repaint();
        }
      }
    
      }

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,923
    Rep Power
    8

    Default

    I don't see any calls to addMouseListener() anywhere in your code. And the only thing the Timer does is call repaint(), which won't do anything by itself. You shouldn't have "business logic" in your paintComponet method: what would happen if the user resized the JFrame?

  3. #3
    Adomini is offline Member
    Join Date
    Aug 2010
    Posts
    70
    Rep Power
    0

    Default

    I will find out where to add the mouselistener. Thanks for pointing that out.

    I wanted the timer to call repaint() - my idea was to have the label start flashing again after the mousepress is released. I guess I should just call repaint(), but that would only repaint it once , correct? That is why I wanted the timer to start up again.

  4. #4
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,923
    Rep Power
    8

    Default

    The Timer is a fine idea, if that's what you want. But all your logic (and by that, I mean toggling of the shown variable) is in your paintComponent() method. That's a no-no. Instead, put your logic in your Timer, then call repaint.

  5. #5
    Adomini is offline Member
    Join Date
    Aug 2010
    Posts
    70
    Rep Power
    0

    Default

    Ok. Thanks for the tip. my professor always has it in the paint method. I will mess around with it tonight!

Similar Threads

  1. can't get x and y from mouselistener
    By j2me64 in forum Java 2D
    Replies: 3
    Last Post: 04-24-2010, 04:57 PM
  2. Remove Flashing in Applet
    By Unome in forum Java Applets
    Replies: 5
    Last Post: 05-30-2009, 07:26 PM
  3. Flashing
    By Supamagier in forum Java 2D
    Replies: 6
    Last Post: 04-29-2009, 03:43 PM
  4. i need help for MouseListener
    By sfaxianovic in forum New To Java
    Replies: 2
    Last Post: 08-21-2008, 03:30 AM
  5. MouseListener
    By Aswq in forum New To Java
    Replies: 12
    Last Post: 07-18-2008, 08:10 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •