Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Page 1 of 3 123 LastLast
Results 1 to 25 of 60

Thread: Trying to figure out how to make a screen saver.

  1. #1
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Smile Trying to figure out how to make a screen saver.

    I know that in order to make a good screensaver, I need to ditch the title bar and the minimizing components thing.

    I've tried a few things including JWindow but it's not even showing up.

    What class would be good as a sort of window or GUI that wouldn't have a minimize or maximize or title bar to it and yet would still let me add a Mouse Listener to it?

    I tried these things below and nothing worked.

    JPanel appears to have windows too. If not, how to you get one to show up without a window? And how do you add a Mouse Listener to it?

    Most screensavers will exit when you click a mouse or hit a key. (Is there a way you can make it exit if you press a key?

    Like if (KEY_PRESSED == true) or something like that)

    JFrame is out.

    Frame is also probably out.

    JSplitPane is out.

    JLayeredPane seems too strange.

    JApplet also a title bar, and I don't want anything that's internet run at the moment anyway.

    JViewPort, whatever that is, isn't working either.




    import java.awt.*;
    import javax.swing.*;
    import java.util.*;
    import java.io.*;
     
    public class Demo5 extends JWindow
    {
    private JButton button, button2;
    private JPanel p;
    public Demo5()
    {
    setVisible(true);
    button = new JButton("Button");
    button2 = new JButton("Button 2");
    p = new JPanel();
    p.add(button);
    p.add(button2);
    setFocusable(true);
    add(button);
    setContentPane(p);
     
     
     
     
    }
     
    public static void main(String[] args)
    {
     
    Demo5 d5 = new Demo5();
    d5.setVisible(true);
    JLayeredPane jlp = new JLayeredPane();
    jlp.setVisible(true);
     
    }
    }
    Last edited by javapenguin; July 25th, 2011 at 09:18 PM.


  2. #2
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: Trying to figure out how to make a screen saver.

    Frame (which is extended by JFrame) has setUndecorated method. You should also be able to add Key and Mouse Listeners.
    Improving the world one idiot at a time!

  3. The Following User Says Thank You to Junky For This Useful Post:

    javapenguin (July 26th, 2011)

  4. #3
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: Trying to figure out how to make a screen saver.

    What's setUndecorated() do?

  5. #4
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: Trying to figure out how to make a screen saver.

    It causes the planet to be sucked into a blackhole. You can either trust me or write some code to experiment and find out for yourself.
    Improving the world one idiot at a time!

  6. The Following User Says Thank You to Junky For This Useful Post:

    KevinWorkman (July 28th, 2011)

  7. #5
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: Trying to figure out how to make a screen saver.

    Ok. It worked.

    However, I'm trying a bunch of MouseListeners and MouseMotionListeners and I can't figure out what method I should put the code that will exit the program if the mouse is moved.

       import java.awt.*;
       import javax.swing.*;
       import java.util.*;
       import java.io.*;
       import java.awt.event.*;
     
     
       public class Demo5 extends JFrame implements MouseMotionListener
       {
          private JButton button, button2;
          private JPanel p;
          public Demo5()
          {
     
             setExtendedState(JFrame.MAXIMIZED_BOTH);
             setUndecorated(true);
             setVisible(true);
             p = new JPanel();
     
     
     
             setContentPane(p);
     
     
     
     
          }
     
          public void mouseDragged(MouseEvent e)
          {
     
          }
     
          public void mouseMoved(MouseEvent e)
          {
             System.exit(0);
          }     
     
          public static void main(String[] args)
          {
     
             Demo5 d5 = new Demo5();
     
     
          }
       }

    Also, I figured out how to start the windows opened maximized.

    However, if you compile it, you'll probably need Ctrl+Alt+Delete or something as there's no way of getting out, unless you have a Windows Button that's in the shape of a four piece flag or window on your computer.

    It's supposed to be a screen saver.

  8. #6
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: Trying to figure out how to make a screen saver.

    Quote Originally Posted by Junky View Post
    It causes the planet to be sucked into a blackhole. You can either trust me or write some code to experiment and find out for yourself.
    Man, Java programming sure has gotten a lot more dangerous!

    Ok, so it hides the title bar and window buttons. Done that.

    Thanks.

  9. #7
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: Trying to figure out how to make a screen saver.

    Ok, I have fixed that problem, though why I had to take that route is beyond me.

    I told it to add a MouseListener and told it to add the MouseListener the class implemented.

    However, I had thought that, like when you implement ActionListener, the class would automatically add the MouseListener that the class implements to itself without me having to bother to do it manually. Any idea why that happened?

    Now I can't figure out how to get it to exit if ANY key is pressed, rather than having to list all like 200 of them.

    Is it even possible to do that?

    So far, it's not working.

    Also, if I try to set it so that I attach an event to mouseMoved(), it will exit immediately.

    That's not quite what I had hoped for.

    How do I get it so that it won't exit immediately but will exit if the mouse moves?

       import java.awt.*;
       import javax.swing.*;
       import java.util.*;
       import java.io.*;
       import java.awt.event.*;
       import javax.swing.event.*;
     
     
       public class Demo5 extends JFrame implements MouseInputListener, KeyListener
       {
     
          private JPanel p;
         private int times;
     
     
          public Demo5()
          {
     
     
             setExtendedState(JFrame.MAXIMIZED_BOTH);
             setUndecorated(true);
             setVisible(true);
             p = new JPanel();
     
     
             setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
             addMouseListener(this);
             addMouseMotionListener(this);
     
             addKeyListener(this);
     
             setContentPane(p);
          }
          public  void 	mouseClicked(MouseEvent e) 
          {
             System.exit(0);
     
     
          }
     
          public void 	mouseEntered(MouseEvent e) 
          {
     
          }
     
          public  void 	mouseExited(MouseEvent e) 
          {
     
     
          }
     
          public  void 	mousePressed(MouseEvent e) 
          {
     
          }
     
          public  void 	mouseReleased(MouseEvent e) 
          {
     
          }
     
          public  void 	keyPressed(KeyEvent e) 
          {
             if (e.getModifiers() == KeyEvent.KEY_PRESSED)
                System.exit(0);
          }
     
          public  void 	keyReleased(KeyEvent e) 
          {
     
          }
     
          public  void 	keyTyped(KeyEvent e) 
          {
             if (e.getModifiers() == KeyEvent.KEY_TYPED)
                System.exit(0);
          }
     
          public void mouseDragged(MouseEvent e)
          {
             System.exit(0);
          }
     
          public void mouseMoved(MouseEvent e)
          {
     
           int y = 0;
             int y2 = 0;
     
             if (times == 0)
             {
                y = e.getYOnScreen();
                times++;
             }
     
             else 
             {
                y2 = e.getYOnScreen();
     
                if (y != y2)
                   System.exit(0);
             }
     
     
          }
     
          public static void main(String[] args)
          {
     
             Demo5 d5 = new Demo5();
     
     
          }
       }

    Ok, I've got it so that it now works if mouse is moved too, but still the exit for any key pressed isn't working.
    Last edited by javapenguin; July 26th, 2011 at 08:50 PM.

  10. #8
    Junior Member
    Join Date
    Jul 2011
    Location
    toronto
    Posts
    14
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Re: Trying to figure out how to make a screen saver.

    Change this method

    public void keyPressed(KeyEvent e)
    {
    if (e.getModifiers() == KeyEvent.KEY_PRESSED)
    System.exit(0);
    }

  11. #9
    Junior Member
    Join Date
    Jul 2011
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question Re: Trying to figure out how to make a screen saver.

    Quote Originally Posted by javapenguin View Post
    I know that in order to make a good screensaver, I need to ditch the title bar and the minimizing components thing.

    I've tried a few things including JWindow but it's not even showing up.

    What class would be good as a sort of window or GUI that wouldn't have a minimize or maximize or title bar to it and yet would still let me add a Mouse Listener to it?

    I tried these things below and nothing worked.

    JPanel appears to have windows too. If not, how to you get one to show up without a window? And how do you add a Mouse Listener to it?

    Most screensavers will exit when you click a mouse or hit a key. (Is there a way you can make it exit if you press a key?

    Like if (KEY_PRESSED == true) or something like that)

    JFrame is out.

    Frame is also probably out.

    JSplitPane is out.

    JLayeredPane seems too strange.

    JApplet also a title bar, and I don't want anything that's internet run at the moment anyway.

    JViewPort, whatever that is, isn't working either.




    import java.awt.*;
    import javax.swing.*;
    import java.util.*;
    import java.io.*;
     
    public class Demo5 extends JWindow
    {
    private JButton button, button2;
    private JPanel p;
    public Demo5()
    {
    setVisible(true);
    button = new JButton("Button");
    button2 = new JButton("Button 2");
    p = new JPanel();
    p.add(button);
    p.add(button2);
    setFocusable(true);
    add(button);
    setContentPane(p);
     
     
     
     
    }
     
    public static void main(String[] args)
    {
     
    Demo5 d5 = new Demo5();
    d5.setVisible(true);
    JLayeredPane jlp = new JLayeredPane();
    jlp.setVisible(true);
     
    }
    }
    Quote Originally Posted by javapenguin View Post
    I know that in order to make a good screensaver, I need to ditch the title bar and the minimizing components thing.

    I've tried a few things including JWindow but it's not even showing up.

    What class would be good as a sort of window or GUI that wouldn't have a minimize or maximize or title bar to it and yet would still let me add a Mouse Listener to it?

    I tried these things below and nothing worked.

    JPanel appears to have windows too. If not, how to you get one to show up without a window? And how do you add a Mouse Listener to it?

    Most screensavers will exit when you click a mouse or hit a key. (Is there a way you can make it exit if you press a key?

    Like if (KEY_PRESSED == true) or something like that)

    JFrame is out.

    Frame is also probably out.

    JSplitPane is out.

    JLayeredPane seems too strange.

    JApplet also a title bar, and I don't want anything that's internet run at the moment anyway.

    JViewPort, whatever that is, isn't working either.




    import java.awt.*;
    import javax.swing.*;
    import java.util.*;
    import java.io.*;
     
    public class Demo5 extends JWindow
    {
    private JButton button, button2;
    private JPanel p;
    public Demo5()
    {
    setVisible(true);
    button = new JButton("Button");
    button2 = new JButton("Button 2");
    p = new JPanel();
    p.add(button);
    p.add(button2);
    setFocusable(true);
    add(button);
    setContentPane(p);
     
     
     
     
    }
     
    public static void main(String[] args)
    {
     
    Demo5 d5 = new Demo5();
    d5.setVisible(true);
    JLayeredPane jlp = new JLayeredPane();
    jlp.setVisible(true);
     
    }
    }

  12. #10
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: Trying to figure out how to make a screen saver.

    Quote Originally Posted by wookyoo View Post
    Change this method

    public void keyPressed(KeyEvent e)
    {
    if (e.getModifiers() == KeyEvent.KEY_PRESSED)
    System.exit(0);
    }
    Change it to what?

  13. #11
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: Trying to figure out how to make a screen saver.

    Ok, could somebody please explain what I'm doing wrong? I tried a key listener to get it to exit when any key is pressed.

    It should be stopping invalid keys, which always seem to have a key code of 0.

    However, now it won't even print the key text.

    If I remove the str = bla bla bla and the if statement, it will print again.

    Why I have to add key and mouse listeners to the class manually with addKeyListener(this), etc, is beyond me.

    I already have the class implementing key listener and mouse motion listener, which is an interface that implements mouse listener and some other listener, yet I still have to add them.

       import java.awt.*;
       import javax.swing.*;
       import java.util.*;
       import java.io.*;
       import java.awt.event.*;
       import javax.swing.event.*;
     
     
       public class Demo5 extends JFrame implements MouseInputListener, KeyListener
       {
     
          private ScreenSaverPanel p;
          private int times;
     
     
          private class ScreenSaverPanel extends JPanel
          {
     
             public ScreenSaverPanel()
             {
                setBackground(Color.BLACK);
             }
     
             protected void paintComponent(Graphics g)
             {
                super.paintComponent(g);
             }
     
             protected void paintBorder(Graphics g)
             {
                super.paintBorder(g);
     
             }
     
          }
          public Demo5()
          {
     
     
             setExtendedState(JFrame.MAXIMIZED_BOTH);
             setUndecorated(true);
             setVisible(true);
             p = new ScreenSaverPanel();
     
     
             setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
             addMouseListener(this);
             addMouseMotionListener(this);
     
             addKeyListener(this);
     
             setContentPane(p);
          }
          public  void 	mouseClicked(MouseEvent e) 
          {
             System.exit(0);
     
     
          }
     
          public void 	mouseEntered(MouseEvent e) 
          {
     
          }
     
          public  void 	mouseExited(MouseEvent e) 
          {
     
     
          }
     
          public  void 	mousePressed(MouseEvent e) 
          {
     
          }
     
          public  void 	mouseReleased(MouseEvent e) 
          {
     
          }
     
          public  void 	keyPressed(KeyEvent e) 
          {
     
             System.out.println("Key Code: " + e.getKeyCode());
             System.out.println("Text: " + e.getKeyText(e.getKeyCode()));
     
     
             String str = e.getKeyText(e.getKeyCode());
     
     
     
             if (str.equals(str) && e.getKeyCode() != 0)
                System.exit(0);
          }
     
          public  void 	keyReleased(KeyEvent e) 
          {
     
          }
     
          public  void 	keyTyped(KeyEvent e) 
          {
     
          }
     
          public void mouseDragged(MouseEvent e)
          {
             System.exit(0);
          }
     
          public void mouseMoved(MouseEvent e)
          {
             int y = 0;
             int y2 = 0;
             int x = 0;
             int x2 = 0;
     
             if (times == 0)
             {
                y = e.getYOnScreen();
                x = e.getXOnScreen();
                times++;
             }
     
             else 
             {
                y2 = e.getYOnScreen();
                x2 = e.getXOnScreen();
                if (y != y2 || x != x2)
                   System.exit(0);
             }
     
     
     
     
          }
     
          public static void main(String[] args)
          {
     
             Demo5 d5 = new Demo5();
     
     
          }
       }

    -----Edit--------
    Ok, by removing the two printlines, now it will exit if I hit one of the alphabetical, not the numerical or any other, keys. But I have to hit them first. I can't hit "1b" for instance.

    Ok, it'll only work for about 10 seconds after the program is started. Otherwise, no key will work.
    Last edited by javapenguin; July 28th, 2011 at 10:54 AM. Reason: Ok, what am I doing wrong now?

  14. #12
    Forum old-timer
    Join Date
    Nov 2008
    Location
    Faversham, Kent, UK
    Posts
    472
    My Mood
    Mellow
    Thanks
    4
    Thanked 58 Times in 54 Posts

    Default Re: Trying to figure out how to make a screen saver.

    Seems to me that Full-screen Exclusive Mode would be ideally suited to screensavers.

  15. The Following User Says Thank You to dlorde For This Useful Post:

    javapenguin (July 28th, 2011)

  16. #13
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Trying to figure out how to make a screen saver.

    What in the name of James Gosling do you think this is doing:

    if (str.equals(str)

    And why are you comparing the x and y values in the mouseMoved() function?

    And if you don't care which key is pressed, why put any if statement in at all?

    Also, check which component, if any, has the focus.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  17. #14
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: Trying to figure out how to make a screen saver.

    Just telling it to exit when I press a key either causes it to exit at nothing or not exit when I press a key.

    I tried that Full Screen Exclusive Mode and the screen is flickering.

    Also, why is my background sometimes starting out black like it's supposed to and other times starting out gray?

    Is it one of my paint methods, though they're not being used, that's responsible for that?

  18. #15
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Trying to figure out how to make a screen saver.

    I don't have time to look through the multiple code dumps and edits you've done- you should keep us up to date with, you guessed it, an SSCCE. Why do you have multiple paint methods? Flickering is usually caused by incorrectly handling painting, which I know we've talked about before.

    I recommend splitting up your problem into multiple parts- create a normal program that responds to ANY keypress. Create another program, completely separately, that does full-screen painting. Create an SSCCE for each, and ask specific questions.

    Only when you have both working should you worry about combining them.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  19. #16
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: Trying to figure out how to make a screen saver.

    Ok, here's on SSCCE. It still has the key problem in it.

       import java.awt.event.*;
       import java.awt.*;
       import java.util.*;
       import java.io.*;
       import javax.swing.event.*;
       import javax.swing.*;
     
       public class KeyListenerTest extends JFrame implements KeyListener
       {
          private JPanel panel;
          public KeyListenerTest()
          {
             panel = new JPanel();
             panel.setBackground(Color.BLACK);
             addKeyListener(this);
             setDefaultCloseOperation(EXIT_ON_CLOSE);
             setContentPane(panel);
     
          }
          public  void 	keyPressed(KeyEvent e) 
          {
     
             System.out.println("A key was pressed.");
     
             System.exit(0);
          }
     
          public  void 	keyReleased(KeyEvent e) 
          {
     
          }
     
          public  void 	keyTyped(KeyEvent e) 
          {
             System.out.println("A key was typed.");
             System.exit(0);
          }
     
          public static void main(String[] args)
          {
             KeyListenerTest klt = new KeyListenerTest();
             klt.setVisible(true);
          }
     
       }

    Sometimes it'll exit without a key pressed and others it won't even exit at all.

    Who's James Gosling?
    Last edited by javapenguin; July 28th, 2011 at 11:54 AM.

  20. #17
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Trying to figure out how to make a screen saver.

    Thanks for making the SSCCE. Sorry, but that code seems to work just fine for me. It doesn't exit until I press a key when the JFrame is focused. And it exits every time I press a key when the JFrame is in focus. It behaves exactly how I expect it to.

    Are you using some kind of weird keyboard or anything like that?
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  21. #18
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: Trying to figure out how to make a screen saver.

    Laptop keyboard. One of the keys is missing I'll admit, but the button that lets the key function is still functioning as I can still type a J.

    However, I never called the method setFocusable(true). Let's see what that would do.

    This is weird. I use setFocusable and it exits if I minimize it and sometimes exits when no key is pressed.

  22. #19
    Forum old-timer
    Join Date
    Nov 2008
    Location
    Faversham, Kent, UK
    Posts
    472
    My Mood
    Mellow
    Thanks
    4
    Thanked 58 Times in 54 Posts

    Default Re: Trying to figure out how to make a screen saver.

    The code you posted does what it says on the tin here - it exits when a key is pressed (it never gets to the key typed handler, because it exits before that).

    How can you tell it exits without a key being pressed? does it flash on the screen then exit before you can do anything?
    What about it ignoring keys - do you make sure the frame has focus before pressing a key?

  23. #20
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: Trying to figure out how to make a screen saver.

    How do you make sure the frame has focus?

    I tried setFocusable().

    The thing is, at least it was for me, I can get it to exit if I click within like 10 seconds otherwise no.

    Sometimes it exits immediately as soon as I start it.

    As for how I can tell, it gives the println output for key pressed though none was and then exits.

  24. #21
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Trying to figure out how to make a screen saver.

    Quote Originally Posted by javapenguin View Post
    How do you make sure the frame has focus?

    I tried setFocusable().
    What did you expect that to do? What does the API say that does? Does the API contain any methods that returns whether a component is focused?

    Quote Originally Posted by javapenguin View Post
    The thing is, at least it was for me, I can get it to exit if I click within like 10 seconds otherwise no.

    Sometimes it exits immediately as soon as I start it.

    As for how I can tell, it gives the println output for key pressed though none was and then exits.
    Are you sure you're running the code you posted, and not code with a MouseListener in it? Did you recompile everything, etc? What happens if, instead of exiting, you print out the key that was pressed?
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  25. #22
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: Trying to figure out how to make a screen saver.

    It's focusable but the key listener isn't working.

    I'm using a test program. No, it has no mouse listener.

       import java.awt.event.*;
       import java.awt.*;
       import java.util.*;
       import java.io.*;
       import javax.swing.event.*;
       import javax.swing.*;
     
       public class KeyListenerTest extends JFrame implements KeyListener
       {
          private JPanel panel;
          public KeyListenerTest()
          {
             panel = new JPanel();
             setExtendedState(JFrame.MAXIMIZED_BOTH);
             panel.setBackground(Color.BLACK);
             setUndecorated(true);
             setVisible(true);
             addKeyListener(this);
          	//setFocusable(true);
             setDefaultCloseOperation(EXIT_ON_CLOSE);
             System.out.println(isFocusable());
             setContentPane(panel);
     
          }
          public  void 	keyPressed(KeyEvent e) 
          {
     
             if (isFocusable())
             {
                System.out.println("A key was pressed.");
     
                System.exit(0);
             }
     
          }
     
          public  void 	keyReleased(KeyEvent e) 
          {
     
          }
     
          public  void 	keyTyped(KeyEvent e) 
          {
             System.out.println("A key was typed.");
             System.exit(0);
          }
     
          public static void main(String[] args)
          {
             KeyListenerTest klt = new KeyListenerTest();
     
          }
     
       }

    It seems it's not often going into the key listener.

    A few times it goes into it without any key being pressed.

    It'll print out "A key was pressed." if the key listener is called and then will exit.

    Should I add a Key Listener to the JPanel instead.
    Last edited by javapenguin; July 29th, 2011 at 07:17 PM.

  26. #23
    Junior Member
    Join Date
    Jul 2011
    Location
    toronto
    Posts
    14
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Re: Trying to figure out how to make a screen saver.

    if you want the program exits when any key is pressed then...

    when keyPressed system exits, right?

    i tried this and worked...

    public void keyPressed(KeyEvent e)
    {
    System.exit(0);
    }
    if i didn't understand then... please comment again.

  27. #24
    Forum old-timer
    Join Date
    Nov 2008
    Location
    Faversham, Kent, UK
    Posts
    472
    My Mood
    Mellow
    Thanks
    4
    Thanked 58 Times in 54 Posts

    Default Re: Trying to figure out how to make a screen saver.

    Quote Originally Posted by wookyoo View Post
    if i didn't understand then... please comment again.
    If you read his code, I think you'll find he knows about System.exit(0) - and uses it. If you read his posts, I think you'll find that has nothing to do with his problem.

  28. #25
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: Trying to figure out how to make a screen saver.

    Still can't figure it out.

    It's not my calling setExtendedState() is it?

    Sometimes the panel doesn't even change to the background I told it to.

    Of course, a couple of my programs have sometimes, until refreshed or minimized then maximized, shown improper stuff that's not always there or left out components that later appear once minimized and maximized.

    Not often, but it happens every once in a while.

    Why isn't it calling the keyListener?

    I found it's calling none of the key listener methods.

    Or rather, it's not usually calling them.

    Ok, I made sure with a println.

    It has the key Listener, but why won't it normally use it, and why, when it does, will it exit without any keys being pressed?
    Last edited by javapenguin; July 30th, 2011 at 04:15 PM.

Page 1 of 3 123 LastLast

Similar Threads

  1. How to make a mouseclick somewhere on the screen?
    By Broodjie in forum Java Theory & Questions
    Replies: 4
    Last Post: March 22nd, 2011, 06:58 AM
  2. [SOLVED] can't figure out..
    By darego in forum What's Wrong With My Code?
    Replies: 6
    Last Post: December 10th, 2010, 02:26 PM
  3. I'm new at this and can't figure it out
    By jaheh06 in forum What's Wrong With My Code?
    Replies: 5
    Last Post: September 22nd, 2010, 08:44 AM
  4. Unnable to make validations work for login screen
    By fierof2 in forum Web Frameworks
    Replies: 8
    Last Post: July 29th, 2010, 07:12 AM
  5. [SOLVED] Java program to prompt and display the user input number as odd or even
    By napenthia in forum Java Theory & Questions
    Replies: 11
    Last Post: April 22nd, 2009, 01:19 AM