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 2 of 3 FirstFirst 123 LastLast
Results 26 to 50 of 60

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

  1. #26
    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've got conclusive proof that it's not the extended state, nor the decorated thing, nor it not recognizing the key listener.

    Maybe it's a JGrasp glitch.

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

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

    Ok, I've tried everything I can think of. Nothing is working.

    Can I see what code you used that you said you got it to work?

    Ok, I did find something odd.

    Sometimes it would work when the key code was "0".

    That's the only time it worked and that's when I never pushed anything.

    That's the only lead I can think of right now.

    Ok, the text for that mysterious key that I never even push is:

    Unknown keyCode: 0x0

    I've tried and tried to tweak the code.

    Here's what it is now:

       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
       {
     
     
     
          public  void 	keyPressed(KeyEvent e) 
          {
     
     
              //   System.out.println("A key was pressed.");
     
     
             if (e.getKeyCode() == KeyEvent.KEY_PRESSED)
             {
                System.out.println("Help!");
                System.exit(0);
             }
             System.out.println(e.getKeyCode());
     
     
             exit();
     
     
          }
     
          private void exit()
          {
             System.exit(0);
          }
     
          public  void 	keyReleased(KeyEvent e) 
          {
             System.out.println("Released:");
          }
     
          public  void 	keyTyped(KeyEvent e) 
          {
            //  System.out.println("A key was typed.");
             System.exit(0);
          }
     
          private JPanel panel;
          public KeyListenerTest()
          {
             System.out.println(KeyEvent.getKeyText(0));
             panel = new JPanel();
             addKeyListener(this);
             setExtendedState(JFrame.MAXIMIZED_BOTH);
             panel.setBackground(Color.BLACK);
             setUndecorated(true);
             setVisible(true);
          	// setFocusable(true);
     
          	//setFocusable(true);
             setDefaultCloseOperation(EXIT_ON_CLOSE);
             System.out.println(isFocusable());
     
             setContentPane(panel);
     
          }
     
          public static void main(String[] args)
          {
             KeyListenerTest klt = new KeyListenerTest();
            // System.out.println(klt.getKeyListeners()[0]);
     
     
          }
     
       }
    Last edited by javapenguin; July 30th, 2011 at 05:15 PM.

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

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

    JDK 7 has come out at last!

  4. #29
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,152
    My Mood
    Hungover
    Thanks
    141
    Thanked 595 Times in 510 Posts

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

    It still seems to mostly work for me, ignoring the problem with adding components to your JFrame after it's visible. Why do you do that? Also, why are you extending JFrame when you don't override any of its methods?

    And how does Java 7 help with this problem?
    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!

  5. #30
    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 KevinWorkman View Post
    It still seems to mostly work for me, ignoring the problem with adding components to your JFrame after it's visible. Why do you do that? Also, why are you extending JFrame when you don't override any of its methods?

    And how does Java 7 help with this problem?
    I never thought of that. I mean, why not just use a JFrame object.

    As for Java 7, I was waiting forever for it to come out. Anyway, maybe it has new methods that will help me.

    I was just merely mentioned it as I noticed there was a new Java 7 API that normally had, till then, been a work in progress.

    Anyway, maybe it's my compiler.

    I've tried different programs and adding it to other programs to make sure it wasn't some typo glitch.

    Unless I need to use the KeyEvent class for something to make it do anything, it's the compiler's fault.

    I've just downloaded BlueJ to see if it'll work there.

    If not, then it's probably the code. Though what's wrong with the code, I won't know.

  6. #31
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,152
    My Mood
    Hungover
    Thanks
    141
    Thanked 595 Times in 510 Posts

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

    Quote Originally Posted by javapenguin View Post
    I never thought of that. I mean, why not just use a JFrame object.
    True that. This is the concept of "composition over inheritance" and it might be worth a google.


    Quote Originally Posted by javapenguin View Post
    As for Java 7, I was waiting forever for it to come out. Anyway, maybe it has new methods that will help me.

    I was just merely mentioned it as I noticed there was a new Java 7 API that normally had, till then, been a work in progress.
    Fair enough. But I doubt that Java 7 has anything that will help you, as your problem seems like a basic misunderstanding.


    Quote Originally Posted by javapenguin View Post
    Anyway, maybe it's my compiler.
    No. No no no. 99.99999% of the time, "compiler glitches" or "bugs" are really just a programmer misunderstanding how something works, or just doing something flat out wrong- one thing I've told you to fix is the adding of components after you make your JFrame visible. You shouldn't even be thinking that it's not your fault, until you've fixed ALL of your errors. What does "it's my compiler" even mean?


    Quote Originally Posted by javapenguin View Post
    I've tried different programs and adding it to other programs to make sure it wasn't some typo glitch.
    I don't really know what this means. Take the SSCCE you created, fix the problems I pointed out, and try again. Make sure you're actually running that code. Try figuring out which key is being pressed. These are all suggestions you've received in the last few days, and I haven't seen you really try them, yet you're already jumping to blaming the compiler.

    Quote Originally Posted by javapenguin View Post
    Unless I need to use the KeyEvent class for something to make it do anything, it's the compiler's fault.
    Again, it's almost definitely NOT the compiler's fault. What compiler are you using?

    Quote Originally Posted by javapenguin View Post
    I've just downloaded BlueJ to see if it'll work there.
    Why? What do you hope to accomplish with that? You've received a ton of advice, which you've only half followed.

    Quote Originally Posted by javapenguin View Post
    If not, then it's probably the code. Though what's wrong with the code, I won't know.
    All the more reason to follow the advice you've already been given. Have you stepped through it with a debugger? Have you tried printing out the stack trace to see where the mystery key press is coming from?
    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!

  7. #32
    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 appears the key being "pressed" is some kind of invalid key.

    It'll exit, the SSCCE without the mouse listener that is, when I hit the right mouse button on my laptop.

    I did just notice that.

    However, sometimes it'll exit even without the mouse doing anything.

    Is it possible that it's immediately expecting a key, but it can't find one but exits when none is hit?

    I mean, it'll exit and consider "no key" to be a key at times.

    But why isn't it always doing that if that's the case?

    Now it will recognize it, but it's delayed a bit and needs more than one try.

    I made sure it wouldn't exit for an invalid key code, i.e. if no key was pressed.
    Last edited by javapenguin; August 2nd, 2011 at 09:54 AM.

  8. #33
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,152
    My Mood
    Hungover
    Thanks
    141
    Thanked 595 Times in 510 Posts

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

    I'm curious enough to try to help you, but you have to do what I've asked first. Post an updated SSCCE that fixes the problem with adding components after the JFrame is made visible. Make sure you aren't running old code- sometimes, especially if you're using an IDE, you can get stuck with an old version. Add a println somewhere to make sure it's updating correctly. Clear out all your old .class files and compile and run form the command line. Instead of exiting, do something else (like print the key), that way you can get an idea of what's going on.
    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!

  9. #34
    Junior Member
    Join Date
    Apr 2010
    Posts
    4
    Thanks
    0
    Thanked 1 Time in 1 Post

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

    Try turning off "Build" / "Run Topmost" in jGRASP.

    If that doesn't help, try running from the command line.

  10. #35
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,152
    My Mood
    Hungover
    Thanks
    141
    Thanked 595 Times in 510 Posts

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

    Quote Originally Posted by lbarowski View Post
    Try turning off "Build" / "Run Topmost" in jGRASP.

    If that doesn't help, try running from the command line.
    That's good to know, but to get the best help, definitely run from the command line. That's the best way to guarantee that you are running the same thing as the people trying to help you.
    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!

  11. #36
    Junior Member
    Join Date
    Apr 2010
    Posts
    4
    Thanks
    0
    Thanked 1 Time in 1 Post

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

    Quote Originally Posted by KevinWorkman View Post
    That's good to know, but to get the best help, definitely run from the command line. That's the best way to guarantee that you are running the same thing as the people trying to help you.
    That's why I said "If that doesn't help". If it does help, his problems are over.

  12. #37
    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.

    Now it nearly is working. It's just that sometimes it won't respond after the first key pressed.

    It probably was my own error that it wasn't working much before.


    However, it's still not always working now.

    What is command line? Do you mean like console or like command prompt? I'm thinking you mean console, but I'm just checking.

    Anyway, now it works, sometimes. It's doing better than before.

       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
       {
     
     
     
     
          public  void 	keyPressed(KeyEvent e) 
          {
     
     
              //   System.out.println("A key was pressed.");
     
     
             if (e.getKeyCode() == 0)
             {
                return;
             }
             System.out.println(e.getKeyCode());
     
     
             exit();
     
     
          }
     
          private void exit()
          {
             System.exit(0);
          }
     
          public  void 	keyReleased(KeyEvent e) 
          {
             System.out.println("Released:");
          }
     
          public  void 	keyTyped(KeyEvent e) 
          {
            //  System.out.println("A key was typed.");
             System.exit(0);
          }
     
          private JPanel panel;
          public KeyListenerTest()
          {
             System.out.println(KeyEvent.getKeyText(0));
             panel = new JPanel();
             addKeyListener(this);
             setExtendedState(JFrame.MAXIMIZED_BOTH);
             panel.setBackground(Color.BLACK);
             setUndecorated(true);
             // add(panel);
             setVisible(true);
          	// setFocusable(true);
     
          	//setFocusable(true);
             setDefaultCloseOperation(EXIT_ON_CLOSE);
             System.out.println(isFocusable());
     
             setContentPane(panel);
     
          }
     
          public static void main(String[] args)
          {
             KeyListenerTest klt = new KeyListenerTest();
            // System.out.println(klt.getKeyListeners()[0]);
     
     
          }
     
       }

    Sometimes it needs a right mouse click and then a key to work.

    I'd still like to know why it's printing out

    Invalid keycode: 0x0 despite me having told it to return if there is no key hit.

    Also, why does it sometimes call key released twice before going through key pressed successfully?


    Aha! I think I've spotted something! The Key Listener is checking immediately for a key. Almost always, once isn't pressed the second the program starts, so it takes in an invalid key.
    Last edited by javapenguin; August 2nd, 2011 at 10:24 AM.

  13. #38
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,152
    My Mood
    Hungover
    Thanks
    141
    Thanked 595 Times in 510 Posts

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

    It sounds like the application window doesn't have the focus, which we talked about dozens of posts ago (note- focused != focusable). You still haven't fixed the problem of adding components after the JFrame is made visible.

    Yes, by command line, I mean the command prompt, or the console, or whatever you want to call it. How are you running it now? Are you using a keyboard shortcut from some IDE? Are you clicking a button to run it? Something else?

    I've been trying to hint at the KeyEvent.getKeyText() method. What does that give you for the input key?
    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!

  14. #39
    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.

    Unknown keyCode: 0x0

    It had been an invalid key. As I've said, I've found that the key listener was always looking for a key or something so I told it to ignore invalid ones.

    Now it no longer exits over nothing.

    However, as it's not always going to the key listener, trying to figure out which key is hit is usually pointless.

    I've already tried that KeyEvent method.

    No, I've not been using console, just hitting keys with GUI and hoping it'll notice, which sometimes it does.

    Don't know how I'd do it with console.
    Last edited by javapenguin; August 2nd, 2011 at 10:32 AM.

  15. #40
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,152
    My Mood
    Hungover
    Thanks
    141
    Thanked 595 Times in 510 Posts

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

    Quote Originally Posted by javapenguin View Post
    Unknown keyCode: 0x0

    It had been an invalid key. As I've said, I've found that the key listener was always looking for a key or something so I told it to ignore invalid ones.

    Now it no longer exits over nothing.

    However, as it's not always going to the key listener, trying to figure out which key is hit is usually pointless.

    I've already tried that KeyEvent method.

    No, I've not been using console, just hitting keys with GUI and hoping it'll notice, which sometimes it does.

    Don't know how I'd do it with console.
    I honestly don't really know what most of that means. You'd "do it with console" by running it from the command line. I'm not saying enter your input through the console, I'm just saying run the program from it.

    This is the first result for googling "java keyevent keycode 0x0": event handling - Java KeyListener is giving me "Unknown keyCode:0x0" - Stack Overflow

    I can't tell, is your problem fixed now? You still haven't told us how exactly you're running your program now- from an IDE? By clicking a button on the IDE? By using a keyboard shortcut? The reason I'm asking these things and telling you to run from the command line is for this scenario in your IDE-

    You press a keyboard shortcut (what is it in eclipse? F5 or something?) to run the program.
    The program pops up while the key is pressed, maybe or maybe not grabbing the focus.
    You release the keyboard button, which may or may not be detected by the JFrame, depending on the focus.

    Is something like that happening? I've been asking questions that seem to be ignored, which is why it's so hard to help you.
    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!

  16. #41
    Forum old-timer
    Join Date
    Nov 2008
    Location
    Faversham, Kent, UK
    Posts
    471
    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 javapenguin View Post
    I never thought of that. I mean, why not just use a JFrame object.
    Before you use a class, you should already know why you want to use it and how to use it to do what you want. If you know why a JFrame fits your requirement, and you know how to use it to achieve your goal, then by all means use it. Otherwise do some more study until you do know these things. Don't use classes just because 'why not?'.

    As for Java 7, I was waiting forever for it to come out. Anyway, maybe it has new methods that will help me.
    What makes you think so? If you were that serious about Java 7, you'd already know what new classes and methods it provides. That information has been public for months.

    Anyway, maybe it's my compiler.
    It's not your compiler. If you get code that runs, the compiler is working fine.

    Unless I need to use the KeyEvent class for something to make it do anything, it's the compiler's fault.
    Ever heard the saying "A bad workman blames his tools" ? It's not your compiler, it's your code.

    If not, then it's probably the code. Though what's wrong with the code, I won't know.
    It's the code. It's hard to say what's wrong with it, because you haven't clearly stated exactly what you want from it. You've spent most of this thread posting vague stream-of-consciousness maunderings. If you spent a fraction of that time asking coherent questions, listening carefully to the responses, and trying the suggestions, you'd have finished pages ago.

  17. #42
    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.

    There is no setFocused(boolean) method.

    I am running it with GUI. I just hit a key on my laptop keyboard and it's supposed to exit.

    There isn't supposed to be any components.

    A screensaver doesn't need buttons or whatever, though it could have labels, true. Had forgotten about that.

    I only set the contentPane(panel) to the JPanel object. I never added the JPanel object to the JFrame.

    Am I looking for keyPressed() or keyTyped()?

    Though either will exit it, or SHOULD exit it, with the code I have.

    With my code

    if(e.getCode() == 0)
    {
    return;
    }

    It should stop the listener from causing it to exit over nothing.

    However, it's not always exiting.

    Sometimes it requires a mouse button (left or right I've found) and then a key.

    Then it outputs that last key that I hit, though it's showing up capital or whatever, even if the key is lowercase.

    Sometimes it will exit if I hit a key, though only sometimes.

    It varies every time I compile and run it.

    The background is now always black, which is a change over before where it sometimes never set the color for some odd reason.

    I'm not entering keys into a JTextField or JTextArea or whatever.

    It's showing up in the console area, though it doesn't usually respond to them.


    It will usually recognize a key released before it recognizes a key pressed.


    How do I set the focus?

    And to what, the JPanel or the JFrame?

    I"m assuming the JFrame.

       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
       {
     
     
     
     
          public  void 	keyPressed(KeyEvent e) 
          {
     
     
              //   System.out.println("A key was pressed.");
     
     
             if (e.getKeyCode() == 0)
             {
                System.out.println(KeyEvent.getKeyText(e.getKeyCode()));
                return;
             }
             System.out.println(KeyEvent.getKeyText(e.getKeyCode()));
     
     
             exit();
     
     
          }
     
          private void exit()
          {
             System.exit(0);
          }
     
          public  void 	keyReleased(KeyEvent e) 
          {
             System.out.println("Released:");
          }
     
          public  void 	keyTyped(KeyEvent e) 
          {
            //  System.out.println("A key was typed.");
             System.exit(0);
          }
     
          private JPanel panel;
          public KeyListenerTest()
          {
     
     
             panel = new JPanel();
             addKeyListener(this);
             setExtendedState(JFrame.MAXIMIZED_BOTH);
             panel.setBackground(Color.BLACK);
             setUndecorated(true);
             // add(panel);
             setVisible(true);
          	// setFocusable(true);
     
          	//setFocusable(true);
             setDefaultCloseOperation(EXIT_ON_CLOSE);
             System.out.println(isFocusable());
     
             setContentPane(panel);
     
          }
     
          public static void main(String[] args)
          {
             KeyListenerTest klt = new KeyListenerTest();
            // System.out.println(klt.getKeyListeners()[0]);
     
     
          }
     
       }

    It's got less than 100 lines of code.

    I've checked over a lot of things.


    How do I tell it to run though console?

    When I type, letters appear below in the RUN I/O messages area.

    Is it where I'm adding the KeyListener that's causing the problem?

    Basically, it's not always going to the Key Listener.

    I want it, when the screen is maximized, to exit when I hit a key. ANY key.

    Screensavers usually exit when a key is hit or the mouse is moved.

    That's what I was after. I already got the mouse thing down, but I'm having trouble with the key part.
    Last edited by javapenguin; August 2nd, 2011 at 11:38 AM.

  18. #43
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,152
    My Mood
    Hungover
    Thanks
    141
    Thanked 595 Times in 510 Posts

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

    Quote Originally Posted by javapenguin View Post
    There is no setFocused(boolean) method.
    There sure isn't. Why would there be? I did not tell you to set whether the JFrame was focused, I asked whether it was.

    Quote Originally Posted by javapenguin View Post
    I am running it with GUI. I just hit a key on my laptop keyboard and it's supposed to exit.
    Uh, okay. And HOW are you running it? From an IDE? By clicking a mouse? Pressing a keyboard key? What do you do to run the program? I've explained why I'm asking this, and it's a really simple question to answer.


    Quote Originally Posted by javapenguin View Post
    There isn't supposed to be any components.
    Then why are you adding them? And why are you adding them after the JFrame is visible?

    Quote Originally Posted by javapenguin View Post
    A screensaver doesn't need buttons or whatever, though it could have labels, true. Had forgotten about that.
    This is what people mean when they say you just talk to yourself in a stream of consciousness in your own posts.

    Quote Originally Posted by javapenguin View Post
    I only set the contentPane(panel) to the JPanel object. I never added the JPanel object to the JFrame.
    And what do you think that does? Why do you think you need it? What does the API tell you?

    Quote Originally Posted by javapenguin View Post
    Am I looking for keyPressed() or keyTyped()?
    For what? You tell us.


    Quote Originally Posted by javapenguin View Post
    Though either will exit it, or SHOULD exit it, with the code I have.
    And what happens instead?

    Quote Originally Posted by javapenguin View Post
    With my code

    if(e.getCode() == 0)
    {
    return;
    }

    It should stop the listener from causing it to exit over nothing.
    And what happens instead?

    Quote Originally Posted by javapenguin View Post
    However, it's not always exiting.
    And what happens instead? Does it get into the method at all? Does it hit an if statement? Something else?

    Quote Originally Posted by javapenguin View Post
    Sometimes it requires a mouse button (left or right I've found) and then a key.
    For the hundredth time, that sounds like a focus problem. Does your JFrame have the focus? Does adding components to it after it's visible mess that up? Does how you're running the program affect the focus?

    Quote Originally Posted by javapenguin View Post
    Then it outputs that last key that I hit, though it's showing up capital or whatever, even if the key is lowercase.
    Why do you think that is? Have you written a separate test program to figure that out?

    Quote Originally Posted by javapenguin View Post
    Sometimes it will exit if I hit a key, though only sometimes.

    It varies every time I compile and run it.
    And how do you compile and run it?

    Quote Originally Posted by javapenguin View Post
    The background is now always black, which is a change over before where it sometimes never set the color for some odd reason.
    The "odd reason" is because you're adding components to an already visible JFrame.


    Quote Originally Posted by javapenguin View Post
    I'm not entering keys into a JTextField or JTextArea or whatever.
    Not sure what this has to do with your problem.



    Quote Originally Posted by javapenguin View Post
    It's showing up in the console area, though it doesn't usually respond to them.
    No idea what this means.


    Quote Originally Posted by javapenguin View Post
    It will usually recognize a key released before it recognizes a key pressed.
    Again, what? Does your SSCCE do that? Is it repeatable? What exactly are you doing for that to happen? Where are you clicking? Where are you typing?


    Quote Originally Posted by javapenguin View Post
    How do I set the focus?
    What did the API tell you? And why do you think you need to? Instead of trying random things, stop and answer some of the questions you've been asked.

    Quote Originally Posted by javapenguin View Post
    And to what, the JPanel or the JFrame?

    I"m assuming the JFrame.
    Again, stop assuming and trying random things. Stop. Answer our questions.



    Quote Originally Posted by javapenguin View Post
    It's got less than 100 lines of code.
    I'm not running your code until you answer the questions you've been asked and fix the problems that have been pointed out.

    Quote Originally Posted by javapenguin View Post
    I've checked over a lot of things.
    Like what? What did you figure out?


    Quote Originally Posted by javapenguin View Post
    How do I tell it to run though console?
    What did google tell you? Follow this: "Hello World!" for Microsoft Windows (The Java™ Tutorials > Getting Started > The "Hello World!" Application)


    Quote Originally Posted by javapenguin View Post
    When I type, letters appear below in the RUN I/O messages area.
    No idea what this means. Sounds like your JFrame doesn't have the focus, and whatever that "messages area" has it instead.


    Quote Originally Posted by javapenguin View Post
    Is it where I'm adding the KeyListener that's causing the problem?
    I don't know, what have you tried? Have you answered ANY of my questions about the focus? Have you tried to fix the problems I pointed out?


    Quote Originally Posted by javapenguin View Post
    Basically, it's not always going to the Key Listener.
    Focus. Focus. Focus. Focus. Focus.


    Quote Originally Posted by javapenguin View Post
    I want it, when the screen is maximized, to exit when I hit a key. ANY key.
    And what happens instead?


    Quote Originally Posted by javapenguin View Post
    Screensavers usually exit when a key is hit or the mouse is moved.
    Right.


    Quote Originally Posted by javapenguin View Post
    That's what I was after. I already got the mouse thing down, but I'm having trouble with the key part.
    You sure are. I'm done trying to help you until you answer my questions and address the problems I pointed out.
    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. #44
    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.

    I start the program by hitting the "compile" button on the compiler.

    Also, how do you set the focus? Should I add a focus listener or something?

    I'm going to add a focus listener to this SSCCE and tell it, if there's a method called foucsedChanged(), or something, to say that the focus has changed.


    Also, maybe I can use the Listener to figure out where the focus is going.

  20. #45
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,152
    My Mood
    Hungover
    Thanks
    141
    Thanked 595 Times in 510 Posts

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

    Quote Originally Posted by javapenguin View Post
    I start the program by hitting the "compile" button on the compiler.
    Compilers don't have buttons. But I assume you're clicking your mouse in an IDE? Does the JFrame sometimes pop up before you release the mouse, or does clicking the mouse sometimes cause the JFrame to not have focus?

    Quote Originally Posted by javapenguin View Post
    Also, how do you set the focus?
    Why do you think you need to? First you have to test the theory- does your JFrame have the focus when it's not receiving your keyboard input?

    Quote Originally Posted by javapenguin View Post
    Should I add a focus listener or something?
    What happened when you tried?

    Quote Originally Posted by javapenguin View Post
    I'm going to add a focus listener to this SSCCE and tell it, if there's a method called foucsedChanged(), or something, to say that the focus has changed.
    Um, okay. Post the updated SSCCE when you finish it. But since this is only happening on your machine, I'm not sure how much we can help you. I think you should worry more about the isFocused() method and its cousins.


    Quote Originally Posted by javapenguin View Post
    Also, maybe I can use the Listener to figure out where the focus is going.
    Maybe. But first you have to figure out where it ISN'T. Slow down, stop doing things randomly, and answer the questions that have already been asked, or nobody is going to want to help you. This happens every time you post.
    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. #46
    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 should be having the focus.

    I added a WindowFocusListener to it and told it to say different things for windowFocusLost() and windowFocusGained().

    It appears at one point, near the very beginning, the focus is gained.

    However, it now appears, due to a println I had that checked if it was focusable, that it's getting to that, which means it's already visible by that time, BEFORE it goes into the focus listener, which means that, perhaps, the key listener is only going into effect AFTER the JFrame is visible. Is that a problem?

  22. #47
    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 not always focused. Also, the window doesn't come up right away, it takes about a second or two for that to happen.

    It maximizes on its own. So it can't be always having the focus if not maximized at the start, can it?

    So what do I do?


    false
    Has focus
    fdsamm5mfdmfffffff bf af dfffaaaaaa
    Unknown keyCode: 0x0
    Released:
    F

    The line with a bunch of letters is where the input started at.

    There is a definite delay. Also, it's reaching the isFocusable() println before it goes into the focus listener.

    Why is that?

    If it doesn't have focus in the beginning, though, why wouldn't it have it, like it says it does, when the window is fully up? Then why does it not work until either a left or right mouse button is clicked?

    However, there have been times, if I like hit a key within a few seconds of when it pops up, that it will exit, though it's behavior is very random.

    How do I fix the thing where it adds the component?

    If the content pane isn't set before the isFocused() check then that might explain why it's returning false.


    However, why the 2 second delay?

    And how do I make it so it can add components?

       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, WindowFocusListener
       {
     
          public void windowGainedFocus(WindowEvent e)
          {
             System.out.println("Has focus");
     
          }
     
          public void windowLostFocus(WindowEvent e)
          {
             System.out.println("Lost focus");
     
          }
     
     
     
          public  void 	keyPressed(KeyEvent e) 
          {
     
     
              //   System.out.println("A key was pressed.");
     
     
             if (e.getKeyCode() == 0)
             {
                System.out.println(KeyEvent.getKeyText(e.getKeyCode()));
                return;
             }
             System.out.println(KeyEvent.getKeyText(e.getKeyCode()));
     
     
             exit();
     
     
          }
     
          private void exit()
          {
             System.exit(0);
          }
     
          public  void 	keyReleased(KeyEvent e) 
          {
             System.out.println("Released:");
          }
     
          public  void 	keyTyped(KeyEvent e) 
          {
            //  System.out.println("A key was typed.");
             System.exit(0);
          }
     
          private JPanel panel;
          public KeyListenerTest()
          {
     
     
             panel = new JPanel();
             panel.setOpaque(true);
     
             addWindowFocusListener(this);
             addKeyListener(this);
     
             panel.setBackground(Color.BLACK);
             setUndecorated(true);
             // add(panel);
             setVisible(true);
          	// setFocusable(true);
             setExtendedState(JFrame.MAXIMIZED_BOTH);
          	//setFocusable(true);
             setDefaultCloseOperation(EXIT_ON_CLOSE);
             System.out.println(isFocused());
     
             setContentPane(panel);
     
          }
     
          public static void main(String[] args)
          {
             KeyListenerTest klt = new KeyListenerTest();
            // System.out.println(klt.getKeyListeners()[0]);
     
     
          }
     
       }

    Ok, so what am I doing wrong?

    It doesn't have the focus at the very beginning I've found.

    The window should be maximizing immediately. But it's not.

    false
    Has focus
    Unknown keyCode: 0x0
    Released:
    Enter


    Why is it requiring a mouse button + a regular key in order to activate the key listener?

    Why isn't the window showing up immediately? I made sure that the method that should be setting it to full size was the first thing mentioned in the constructor.

    Why doesn't it have the focus at the beginning?


    Also, how do I make sure I can add components though it's undecorated, though that shouldn't matter as I've done it before with no problem?
    Last edited by javapenguin; August 3rd, 2011 at 02:30 PM.

  23. #48
    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, this is truly bizarre. I ran it in BlueJ and it ran like it should.

    At least, so far it is.

    Only flaw is the window isn't coming up immediately.

    There's still like a second or so delay.

    I'm wondering, do some compilers run on different platforms or something?

    I never really took much notice of the platform thing when I downloaded them.

    This is the second compiler that has done weird things to me.

    However, my code above might be messed up when I was trying to fix what probably was a compiler program error.

  24. #49
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,152
    My Mood
    Hungover
    Thanks
    141
    Thanked 595 Times in 510 Posts

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

    It's not the compiler. I'm not helping you until you slow down and answer the questions I've asked and address the problem I've pointed out. Why bother coming here for help if you're just going to ignore us anyway?
    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. #50
    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 KevinWorkman View Post
    It's not the compiler. I'm not helping you until you slow down and answer the questions I've asked and address the problem I've pointed out. Why bother coming here for help if you're just going to ignore us anyway?
    Because I used BlueJ and it worked! The only problem is that the window doesn't often maximize right away and sometimes you have to maximize it yourself, but once you do, the key listener and mouse listener go into effect.

    This time, it probably was the compiler's fault.

    Either that, or the new Java 7 might not be working that well with JGrasp.


    It appears to be just the key listener that's the problem. However, I've had problems with Eclipse, which won't work as I tried to reset it because sometimes it was causing programs to not upgrade, no matter what code I added or removed, even if I removed all the code!

    Also, to make things worse with Eclipse, I had downloaded the entire beta Java 7 into a project folder when I was trying to do something, making the program pretty much almost crash, or at least run very slow and then lock up later and close, thought I think I might have fixed that problem by deleting the stuff out of there.

    But still, I've had problems that can only be compiler related before.

    And, you said yourself that you got it to work fine with your compiler.

    I'm telling you, it works now I think on BlueJ, though why my screen isn't maximizing instantly or even after a 2 second delay like it did on JGrasp is still a mystery. Perhaps because of how I kept restructuring it to try and make it work.

    I'm telling you, it now works better and it had to have been the compiler.

    Now all that remains is to figure out how to get it to maximize right away and I can be done with this thread.

    I saw the console on BlueJ and it printed differently, though perhaps I forgot to clear it as it doesn't autoclean between compiles, but still, it worked and did what it wouldn't do on JGrasp.

    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;
          public boolean pressed = false;
     
     
          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()
          {
     
             p = new ScreenSaverPanel();
             p.setOpaque(true);
             p.setBackground(Color.BLACK);
             setExtendedState(JFrame.MAXIMIZED_BOTH);
             setUndecorated(true);
             setVisible(true);
     
             //setFocusable(true);
             setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
             addMouseListener(this);
             addMouseMotionListener(this);
     
             addKeyListener(this);
     
            /*
             GraphicsDevice myDevice = getGraphicsConfiguration().getDevice();	  
             try
             {
                myDevice.setFullScreenWindow(this);
             }
     
             finally {
                myDevice.setFullScreenWindow(null);
             }
          	*/
             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.getKeyCode() != 0)
             {
                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 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();
     
     
          }
       }

    It compiles just fine for me on BlueJ, runs fine too though I wish it'd maximize right away instead of taking several seconds or making me have to do it myself. That is quite annoying to have it be doing that, though things are much better after the compiler change.

    That code above was the whole program, though only the constructor of Demo5 should be focused on as that's probably what's causing the maximization problem.

    I'm wondering if the problem is caused by where I'm placing setExtendedState().
    Last edited by javapenguin; August 3rd, 2011 at 09:19 PM.

Page 2 of 3 FirstFirst 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, 01: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