Results 1 to 4 of 4
- 05-11-2012, 01:51 AM #1
Member
- Join Date
- Feb 2012
- Posts
- 20
- Rep Power
- 0
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"); } }
-
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.
- 05-11-2012, 02:09 AM #3
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,547
- Rep Power
- 11
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
- 05-11-2012, 04:43 AM #4
Member
- Join Date
- Feb 2012
- Posts
- 20
- Rep Power
- 0
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:
Thanks a lot for the quick responses and information you both gave.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(); } });
Similar Threads
-
why my car can not turn left and turn 5 round,Help!!THx
By seongchog in forum Java AppletsReplies: 7Last Post: 03-01-2012, 09:53 AM -
Non resizable java windows turn black
By captain alge in forum New To JavaReplies: 8Last Post: 03-20-2011, 12:48 PM -
Red/Black Trees
By f1gh in forum New To JavaReplies: 4Last Post: 12-12-2010, 02:30 AM -
red black tree
By ahmakki in forum New To JavaReplies: 1Last Post: 03-21-2010, 06:49 AM -
Red Black tree insertion
By unicorn in forum New To JavaReplies: 16Last Post: 11-03-2009, 05:46 PM


1Likes
LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks