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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T04:48:30+00:00 2026-05-30T04:48:30+00:00

I usually don’t need to ask Java questions, but I’m stuck more than ever

  • 0

I usually don’t need to ask Java questions, but I’m stuck more than ever on something now and I think I’m just missing something over and over again..

I have a JFrame application that has a JMenuBar. The items in the menubar have my class PageManager as their ActionListener. Debug shows this all works fine. The whole lot is initialized like this:

    public static void main(String[] args) {
    UI ui = new UI();                       //The JFrame
    PageManager pm = new PageManager(ui);   //Menu ActionListener
    MenuBar mb = new MenuBar(pm);           //MenuBar
    ui.setJMenuBar(mb);
    ui.setDefaultCloseOperation(0);
    ui.setVisible(true);
    ui.setPage(new Home().getPanel());      //a View
}

In the UI class, I have the following method:

    public void setPage(JPanel p) {
    System.out.println("Set page");
    this.remove(page);
    System.out.println("Removed");
    this.add(p);
    System.out.println("Added " + p);
}

The ui.setPage(new Home().getPanel()); method call works fine. If I move it to the constructor of PageManager, it works too. If I replace it with ui.setPage(new Preferences().getPanel());, it works too. The Home and Preferences classes are Views that create a JPanel and return it using the getPanel method.

However, changing the page using the actionlistener doesn’t seem to work. ALL System.out.println lines print fine, but the page isn’t changed. I use this in PageManager:

private void changePage(String s, int i) {
    if(s.equals("P")) {
        //Program options
        System.out.println("program options");
        if(i == 1) {
            ui.setPage(pref.getPanel());
        }
    } else if(s.equals("C")) {
        //Connection options
    } else if(s.equals("A")) {
        //Add rule
    }
}

(ActionListener picks up the event, checks the source and calls changePage with it).

Thing is, everything runs and seems to work fine according to console output, but the page won’t change whatever I do (including small modifications).

Any help would be greatly appreceated!!!

Kind regards,
Mark

  • 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-30T04:48:31+00:00Added an answer on May 30, 2026 at 4:48 am

    Normally to achieve such behaviour the easier way out is the use of CardLayout.

    And Moreover, once you add a new panel to already existing JFrame, by removing the old one, try to revalidate() and repaint(), so that the new addition can be realized.

    If your JDK is 1.6 or below then revalidate on frame wont’ work, instead use frameObject.getRootPane().revalidate();

    revalidate() on JFrame is directly used with JDK 7+
    

    Here is one sample code snippet to help your cause, with JMenuBar added to it :

    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    
    public class FrameTest extends JFrame
    {
        public FrameTest()
        {
            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            setLocationByPlatform(true);        
    
            final JPanel panel1 = new JPanel();
            panel1.setBackground(Color.BLUE);
            final JPanel panel2 = new JPanel();
            panel2.setBackground(Color.DARK_GRAY);
    
            JMenuBar menuBar = new JMenuBar();
    
            JMenu menu1 = new JMenu("MENU 1");
            JMenu menu2 = new JMenu("MENU 2");
            JMenu menu3 = new JMenu("MENU 3");
    
    
            JMenuItem menuItem1 = new JMenuItem("CHANGE PANELS");     
            JMenuItem menuItem2 = new JMenuItem("MENU ITEM 2");
            JMenuItem menuItem3 = new JMenuItem("MENU ITEM 3");
            menuItem1.addActionListener(new ActionListener()
            {
                public void actionPerformed(ActionEvent ae)
                {
                    if (panel1.isShowing())
                    {
                        remove(panel1);
                        add(panel2, BorderLayout.CENTER);
                    }
                    else if (panel2.isShowing())
                    {
                        remove(panel2);
                        add(panel1, BorderLayout.CENTER);
                    }
                    // for getRootPane().revalidate();
                    revalidate(); // For JDK 7+
                    repaint();
                }
            });
    
            menu1.add(menuItem1);
            menu2.add(menuItem2);
            menu3.add(menuItem3);
    
            menuBar.add(menu1);
            menuBar.add(menu2);
            menuBar.add(menu3);
    
            JButton button = new JButton("CHANGE");
            button.addActionListener(new ActionListener()
            {
                public void actionPerformed(ActionEvent ae)
                {
                    if (panel1.isShowing())
                    {
                        remove(panel1);
                        add(panel2, BorderLayout.CENTER);
                    }
                    else if (panel2.isShowing())
                    {
                        remove(panel2);
                        add(panel1, BorderLayout.CENTER);
                    }
                    // for getRootPane().revalidate();
                    revalidate(); // For JDK 7+
                    repaint();
                }
            });
    
            add(panel1, BorderLayout.CENTER);
            add(button, BorderLayout.PAGE_END);
            setJMenuBar(menuBar);
            setSize(200, 200);
            setVisible(true);
        }
    
        public static void main(String... args)
        {
            SwingUtilities.invokeLater(new Runnable()
            {
                public void run()
                {
                    new FrameTest();
                }
            });
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I usually don't post questions on these forums, but I've searched all over the
I usually don't expect help from outside but I need to solve this quickly
net webdeveloper and usually don't make any win32 apps. but now i have to.
I don't know if I can ask this question here. I usually mistake, but...
I don't usually use regular expressions, hence my question. I need a regex to
They usually involve generics. But some methods with generics don't have them, and not
Usually when I need to fork in C, I do something like this: pid_t
I usually don't use Smarty but am in the process of editing a prebuilt
Programmers usually don't pay much attention to user interfaces and focus more on functionalities
I’ve been developing solutions with databases for more than 11 years now, and it

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.