Results 1 to 4 of 4
Like Tree1Likes
  • 1 Post By DinnerFork

Thread: MouseListener Causes JFrame to Turn Black

  1. #1
    DinnerFork is offline Member
    Join Date
    Feb 2012
    Posts
    20
    Rep Power
    0

    Default MouseListener Causes JFrame to Turn Black

    I have been trying to slowly change a JFrame's transparency, but when I run the method using my mouselistener, the JFrame does not show up until finished or it turns black until the transparency changes completely. It works fine if I run out outside of the mouselistener. It still did not work after running it in a new thread or repainting. How can I make it so that it shows up and slowly shifts transparency?

    Java Code:
    import java.awt.Dimension;
    import java.awt.GraphicsDevice;
    import java.awt.GraphicsEnvironment;
    import java.awt.event.MouseEvent;
    import java.awt.event.MouseListener;
    
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JPanel;
    
    public class testPopUp {
    
    	public static void main(String[] args) {
    		
    		JFrame aFrame = new JFrame();
    		JPanel datePanel = new JPanel();
    		aFrame.add(datePanel);
    		aFrame.setLocation(250,250);
    		aFrame.setPreferredSize(new Dimension(250,250));
    		datePanel.setPreferredSize(new Dimension(250,250));
    		aFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		datePanel.add(new JLabel("Click Me"));
    
    		aFrame.pack();
    		aFrame.setVisible(true);
    		datePanel.addMouseListener(new MouseListener(){
    			public void mouseClicked(MouseEvent arg0) {
    				new testPopUp();
    				System.out.println("Clicked");
    			}
    			public void mouseEntered(MouseEvent arg0) {}
    			public void mouseExited(MouseEvent arg0) {}
    			public void mousePressed(MouseEvent arg0) {}
    			public void mouseReleased(MouseEvent arg0){}
    		});
    	}
    	
    	public testPopUp(){
    		GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
            GraphicsDevice gd = ge.getDefaultScreenDevice();
            
            JFrame.setDefaultLookAndFeelDecorated(true);
            JFrame aFrame = new JFrame();
            aFrame.setPreferredSize(new Dimension(250,250));
            aFrame.pack();
            aFrame.setOpacity(0.05f);
            aFrame.setVisible(true);
        	
    		//try{Thread.sleep(40);}catch(InterruptedException ie){}
    		System.out.println("Changing Transparency");
    		for(int x = 0; x < 40; x++){
    			aFrame.setOpacity(aFrame.getOpacity()+.021f);
    			try{Thread.sleep(30);}catch(InterruptedException ie){}
    		}
    		System.out.println("Done");
    	}
    }

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: MouseListener Causes JFrame to Turn Black

    You're calling Thread.sleep(...) on the Swing event thread which will freeze your GUI. Don't do that, but instead use a Swing Timer.

  3. #3
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,569
    Rep Power
    12

    Default Re: MouseListener Causes JFrame to Turn Black

    Oracle's Tutorial is bound to have abb example illustrating Timer (you want the Swing one). While you're there pay attention to how the gui is made visible on the event dispatch as well.

    # do all your gui stuff on the EDT

    # have methods on the EDT complete *quickly* - no blocking

  4. #4
    DinnerFork is offline Member
    Join Date
    Feb 2012
    Posts
    20
    Rep Power
    0

    Default Re: MouseListener Causes JFrame to Turn Black

    I changed over from the Thread.sleep() to the Swing Timer and it slowly fades in correctly now. Also, I changed the part where the transition happens so that it is on the EDT as well:
    Java Code:
    SwingUtilities.invokeLater(new Runnable(){
            	public void run(){
            		timer = new Timer(20, new ActionListener(){
            			public void actionPerformed(ActionEvent event){
            				aFrame.setOpacity(aFrame.getOpacity()+.020f);
            				if(aFrame.getOpacity()+.020f >= 1.0f){
            					timer.stop();
            					aFrame.setOpacity(1.0f);
            				}
            			}
            		});
            		timer.start(); 
            	}
            });
    Thanks a lot for the quick responses and information you both gave.
    Fubarable likes this.

Similar Threads

  1. why my car can not turn left and turn 5 round,Help!!THx
    By seongchog in forum Java Applets
    Replies: 7
    Last Post: 03-01-2012, 10:53 AM
  2. Non resizable java windows turn black
    By captain alge in forum New To Java
    Replies: 8
    Last Post: 03-20-2011, 01:48 PM
  3. Red/Black Trees
    By f1gh in forum New To Java
    Replies: 4
    Last Post: 12-12-2010, 03:30 AM
  4. red black tree
    By ahmakki in forum New To Java
    Replies: 1
    Last Post: 03-21-2010, 07:49 AM
  5. Red Black tree insertion
    By unicorn in forum New To Java
    Replies: 16
    Last Post: 11-03-2009, 06:46 PM

Posting Permissions

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