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 8090097
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T19:36:21+00:00 2026-06-05T19:36:21+00:00

Edited at the request of commenters. I hope this is compliant. First post! Trying

  • 0

Edited at the request of commenters. I hope this is compliant.

First post! Trying to understand why my Swing application will not advance from one panel to the next. Here is the general flow of the code :

public class MainWindow {

JFrame mainFrame;
ChangeablePanel currentScreen; // abstract and extends JPanel, has getters &
setters for a Timer (swing timer), a String (nextScreen), and an Image 
(background image).  also has a close(AWTEvent e) method that simply calls 
"this.setVisible(false);"

public MainWindow() {
    mainFrame = new JFrame("New Arcana");
    mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);       
    setTitleFrame();
} // MainFrame constructor

public void changeFrame(String frameType, String frameName) {               
    switch (frameType) {
        case "Title":
            setTitleFrame();
            break;
        case "Town":
            setTownFrame(frameName);
            break;
        case "Movie":
            setMovieFrame(frameName);               
            break;
        default:
            break;
    } // switch     
} // changeFrame

private void setTitleFrame() {      
    currentScreen = new TitlePanel();
    currentScreen.addComponentListener(new ScreenChangeListener());
    ...             
    mainFrame.setContentPane(currentScreen);
    mainFrame.setSize(titleScreenLength, titleScreenHeight); // put constants here if you want
    mainFrame.setLocationRelativeTo(null);
    mainFrame.setVisible(true);
} // setTitleFrame

private void setTownFrame(String townName) {        
    currentScreen = new TownPanel(townName);
    currentScreen.addComponentListener(new ScreenChangeListener());
    ...     
    mainFrame.setContentPane(currentScreen);        
    mainFrame.setSize(townScreenLength, townScreenHeight); // put constants here if you want
    mainFrame.setVisible(true);     
} // setTownFrame

private void setMovieFrame(String movieName) {      
    currentScreen = new MoviePanel(movieName);
    currentScreen.addComponentListener(new ScreenChangeListener());
    ...
    mainFrame.setContentPane(currentScreen);
    mainFrame.setSize(titleScreenLength, titleScreenHeight); // put constants here if you want      
    mainFrame.setVisible(true);     
} // setMovieFrame

private class ScreenChangeListener implements ComponentListener {
    @Override
    public void componentHidden(ComponentEvent e) {
        gotoNextScreen(e);
    }

    public void componentMoved(ComponentEvent e) {}

    public void componentResized(ComponentEvent e) {}

    public void componentShown(ComponentEvent e) {}     
} // ScreenChangeListener

public void gotoNextScreen(ComponentEvent e) {        
    changeFrame(currentScreen.getNextScreen(), null);   
}
} // MainWindow

public class Start {
...
public static void main(String[] args) {
    initialize();   

    SwingUtilities.invokeLater(new Runnable() {
        public void run() {
            new MainWindow();
        }
    });   
} // main
...
} // Start

public class TitlePanel extends ChangeablePanel implements ActionListener {

JButton newGame, continueGame;

public TitlePanel() {       
    setFocusable(true);     
    ...

    newGame = new JButton("New Game");
    continueGame = new JButton("Continue");     

    newGame.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            setNextScreen("Movie");
            close(e);               
        }       
    });

    add(newGame);
    add(continueGame);

    createTimer(10, this);
    getTimer().start();
} // TitlePanel constructor

@Override
public void actionPerformed(ActionEvent e) {
    repaint();      
}

public void paintComponent(Graphics g) {
    super.paintComponent(g);
    Graphics2D g2d = (Graphics2D) g;        
    drawTitleScreen(g2d);       
} // paintComponent

private void drawTitleScreen(Graphics2D g2d) {
    g2d.drawImage(getBGImage(), 0, 0, null);
    newGame.setLocation(170, 550);
    continueGame.setLocation(605, 550);
} // drawTitleScreen
} // TitlePanel

public class MoviePanel extends ChangeablePanel implements ActionListener { 

public MoviePanel(String movieName) {       
    setFocusable(true); 

    addKeyListener(new AnyKeyActionListener());
    ...
    createTimer(10, this);
    getTimer().start();
} // TitlePanel constructor

@Override
public void actionPerformed(ActionEvent e) {
    repaint();
}

public void paint(Graphics g) {
    super.paint(g);
    Graphics2D g2d = (Graphics2D) g;        
    drawMovie(g2d);     
} // paintComponent 

private void drawMovie(Graphics2D g2d) {
    g2d.drawImage(getBGImage(), 0, 0, null);        
} // drawTitleScreen    

private class AnyKeyActionListener extends KeyAdapter {     
    public void keyTyped(KeyEvent e) {
        setNextScreen("Town");
        close(e);
    } // keyPressed     
} // listener to check for keystrokes   
} // MoviePanel

The MainFrame is to be populated with more frames as the application advances based on user-input (currently, only MoviePanel and TownPanel are coded), and their code is fairly analogous to this one — I pasted MoviePanel as well.

Execution breaks down after the KeyAdapter-based listener above. However, when I run my application in Debug mode in Eclipse with breakpoints, this indeed does what it’s supposed to do and advances from the MoviePanel to the TownPanel. It is because of this that I suspect threading is the culprit here. Note that I did try many different combinations of the SwingUtilities.invokeLater() technique on the code-blocks above, but it didn’t change anything. Any help would be appreciated; thanks!

  • 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-06-05T19:36:23+00:00Added an answer on June 5, 2026 at 7:36 pm

    Do the following:

    • invokeLater for creation ont the GUI Event Dispatch Thread
    • No repaint() during construction
    • setVisible last

    Especially on event listeners again use invokeLater, to let buttons and such be responsive, and have then actions being taken with response too.

    public static void main(String[] args) {
        ...
        SwingUtilities.invokeLater() {
            @Override()
            new Runnable() {
                new MainFrame().setVisible(true);
            }
        };        
    }
    

    Code review

    In TitlePanel.TitlePanel better use an absolute layout (that means null), instead of using setLocation in the painting code.

        setLayout(null);
        newGame = new JButton("New Game");
        continueGame = new JButton("Continue");
        newGame.setBounds(170, 550, 120, 24);
        continueGame.setBounds(605, 550, 120, 24);
    

    In ChangeablePanel.close ensure also timer.stop().

    In MainWindow use invokeLater:

    public void gotoNextScreen(ComponentEvent e) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                changeFrame(currentScreen.getNextScreen(), null);
            }
        });   
    }
    

    In MoviePanel I cannot see that addKeyListener could function; maybe the left-out code? Or is this maybe the error you saw?
    Furthermore I find a simple repaint() dubious; would have expected something like:

    public void actionPerformed(ActionEvent e) {
        invalidate();
        repaint(10L);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

(Edited for clarification) My (non-OSGi) application build is in Gradle, and I am trying
Edited: SOLUTION FOUND. This is strange and not the best solution, but I just
I've edited this post to make it simpler to read, I think. I need
EDITED: Java httpPost into .asp form I am making an application that will display
This post has been heavily edited and updated! The Intent: I am writing an
N.B This question has been significantly edited before the first answer was given. Hi,
I have this piece of code: vehicle = get_object_or_404(Vehicle, stock_number=stock_number) if request.method == 'POST':
EDITED I'm using Twitter Bootstrap and need to move navbar right( not float:right, but
Edited Question: This should be clear. using System; namespace UpdateDateTimeFields { class Program {
Hello and thank you in advance. This is a follow up question from the

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.