Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 5930509
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T14:33:53+00:00 2026-05-22T14:33:53+00:00

Good day! I wanted use a standart Swing Timer with Full Screen Exclusive Mode.

  • 0

Good day!
I wanted use a standart Swing Timer with Full Screen Exclusive Mode. To this effect I applied a SwingWorker to control the event when graphic mode should be set. All following steps are executed in run method. run() is called from main.
1)First of all, I create my SwingWorker object and override two its methods(doInBackground and done). Init is important method because it should set all needfull graphic setting to current JFrame object and bind my key listener objet(called screen_list) with it:

...
worker = new SwingWorker<Window, Void>() 
    {
            public Window doInBackground() 
            {
                init();
                return gdev.getFullScreenWindow();
            }

            public void done() 
            {
                try {
                    disp = get();
                } 
                catch (InterruptedException ignore) {}
                catch (java.util.concurrent.ExecutionException e) {
                    String why = null;
                    Throwable cause = e.getCause();
                    if (cause != null) {
                        why = cause.getMessage();
                    } else {
                        why = e.getMessage();
                    }
                    System.err.println("Error retrieving file: " + why);
                }
            }
    };

...

2)then I create my screenlistener that implements an ActionListener and a Key Listener, it is bound with disp as KeyListener in init() method:

private void init()
    {

        ...
    try 
            {
                disp = gdev.getFullScreenWindow();
                if(disp != null)
                {
                    gdev.setDisplayMode(use_dm);
                    disp.createBufferStrategy(2);
                    disp.setFocusTraversalKeysEnabled(false);
                    disp.addKeyListener((KeyListener)screen_list);
                }   
            }
            catch(IllegalArgumentException ex) 
            { 
            }   
        ...
       }

3)I create and initialize my Swing Timer and start it;
4)And finally i call execute method:

public void run(int pause, int delay)
{
...
try
    {   
        screen_list = new ScreenListener();
        tm = new Timer(delay, screen_list);
        tm.setInitialDelay(pause);
        tm.setRepeats(true);
        tm.start();
        worker.execute();
    }
    catch(Exception e)
    {}
    ...
}

Class ScreenListener as i have written implements a KeyListener and an ActionListener. In ActionPerfomed method i check out did worker do its job(init method), if yes, i get ref to current display mode and draw something:

    class ScreenListener implements ActionListener, KeyListener 
    {

        public void actionPerformed(ActionEvent e)
        {

        if(!worker.isDone())
                    {
                        return;
                    }
                    else
                    {
                        //gdev - GraphicsDevice type
                        disp = gdev.getFullScreenWindow();
                        if (disp != null) 
                        {
                            ...         
                            draw(gr);
                            ...
                        }
                    }
          }
    ...
    }

Why aren’t events from keyboard processed?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T14:33:54+00:00Added an answer on May 22, 2026 at 2:33 pm

    I used a SwingWorker capabilities because full screen mode as yet had not set by the time timer already started.
    Ок. I passed up using a SwingWorker. Instead of this I added a simple condition:

    class ScreenListener implements ActionListener, KeyListener 
    {
        public void actionPerformed(ActionEvent e)
                {
                    System.out.println("ScreenListener: actionPerformed");
                    disp = gdev.getFullScreenWindow();
                    if(disp == null)
                    {
                        return;
                    }
                    else
                    {
                     //draw something
                     ...
                    }
                }
    

    }

    So now my run method looks like this

       public void run(int pause, int delay)
        {
            screen_list = new ScreenListener();
            init();
            try
            {   
                tm = new Timer(delay, (ActionListener)screen_list);
                tm.setInitialDelay(pause);
                tm.setRepeats(true);
                tm.start();
            }
            catch(Exception ex)
            {
                 ex.printStackTrace();
            }
            finally
            {
                if(!tm.isRunning())
                {
                 ...
                }
            }
         }
    

    And I make a focusable my disp:

    private void init()
    {
        JFrame frame = new JFrame();
        ...
        disp = new Window(frame);
    
        DisplayMode[] dms = gdev.getDisplayModes();
    
        DisplayMode use_dm = null;
    
        if(gdev.isFullScreenSupported())
        {
            disp.setBackground(Color.CYAN);
            disp.setForeground(Color.WHITE);
            gdev.setFullScreenWindow(disp);
        }
    
        use_dm = getMatchMode(dms, def_dm);
    
        try 
        {
            disp = gdev.getFullScreenWindow();
            if(disp != null)
            {
                ...
                disp.setFocusable(true);
                disp.addKeyListener((KeyListener)screen_list);
                ...
            }   
        }
        catch(IllegalArgumentException ex) 
        { 
             ex.printStackTrace();
        }   
    }
    

    but I can’t still catch my keyboard events. KeyTyped, KeyPressed, KeyReleased aren’t still called so it is my problem in that programm.

    My first aim was make a simple animation with full screen mode. At first i used a simple thread method – sleep – as for main thread. Then I added a swing timer for the same purpose but as you look I got a problem: I can’t make to work my KeyListener.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Good day I wanted to have a general URL to access the Desktop of
Good Day, Can someone confirm what was said at the bottom of this post
Good day, all. I know that this is a pretty basic question in terms
Good day all, First of all, maybe i went about this the wrong way
good day, I am using Joomla 1.5 and wanted to make a real estate
Good day lovely people, I wanted to do the following with my seed data
Good day! I am developing a program using JavaFX SDK. I wanted to have
Good day to all of you... This has been bugging me for quite a
I was thinking about this the other day and wanted to see what the
Good day! I've found this solution here: VS2010 Web Publish command line version of

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.