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

  • Home
  • SEARCH
  • 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 8894071
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T23:24:20+00:00 2026-06-14T23:24:20+00:00

I have two buttons added in two different panels, if first button is clicked

  • 0

I have two buttons added in two different panels, if first button is clicked then it need to take to next panel with the second button in it. But the button was not replaced when I click first button.

/*Java GUI*/

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;


public class TestFrame extends JFrame{

    private JPanel panel1, panel2;
    private JButton but,but2; 

    public TestFrame()
    {
        createPanel();
        addPanel();
    }

    private void createPanel()
    {
        panel1 = new JPanel();
        but = new JButton("TestButton");
        but.addActionListener(new addButtonListener());

        panel2 = new JPanel();
        but2 = new JButton("TestButton2");

    }

    private void addPanel()
    {
        panel1.add(but);
        panel2.add(but2);

        add(panel1);

    }

    class addButtonListener implements ActionListener
    {
        public void actionPerformed(ActionEvent ae) 
        {
            getContentPane().removeAll();
            add(panel2);

            repaint();
        }
    }


    public static void main(String args[])
    {
        JFrame frame = new TestFrame();
        frame.setTitle("Test Software");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(500,500);

        frame.setVisible(true);
    }



}
  • 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-14T23:24:22+00:00Added an answer on June 14, 2026 at 11:24 pm

    After remowing all from contentPane, try add panel to ContentPane. Second thing is repainting. If you will not update panel content, it will be painted after resize. Here you are example solution:

    
    
        import java.awt.event.ActionEvent;
    
        import java.awt.event.ActionListener;
    
        import javax.swing.*;
    
        public class Frame extends JFrame{
            private JPanel panel1, panel2;
            private JButton but,but2; 
            public Frame()
            {
               createPanel();
               addPanel();
            }
            private void createPanel()
            {
                panel1 = new JPanel();
                but = new JButton("TestButton");
                but.addActionListener(new addButtonListener());
                but.setBounds(50, 90, 190, 30);//There are example values but remember about setting size
                panel2 = new JPanel();
                but2 = new JButton("TestButton2");
                but2.setBounds(50, 50, 90, 30);//There are example values but remember about setting size
            }
            private void addPanel()
            {
                panel1.add(but);
                panel2.add(but2);
                add(panel1);
            }
    
            class addButtonListener implements ActionListener
            {
                public void actionPerformed(ActionEvent ae) 
                {
                    getContentPane().removeAll();
                    getContentPane().add(panel2);//Adding to content pane, not to Frame
                    repaint();
                    printAll(getGraphics());//Extort print all content
                }
            }
    
            public static void main(String args[])
            {
                Frame frame = new Frame();
                frame.setTitle("Test Software");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setSize(500,500);
                frame.setVisible(true);
            }
    
        }
    
    

    Oracle docs explains difference beetwen adding to contentPane or to Frame directly.
    http://docs.oracle.com/javase/tutorial/uiswing/components/toplevel.html

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

Sidebar

Related Questions

I have two buttons with the same ID: <button type=submit onclick=//do something id=theID>button 1</button>
I have two buttons in my flex app next to each other, ButtonA and
guys, I have added two UILongPressGestureRecognizer. What I want is when two buttons are
I have two buttons, one of class button white and another one of class
I have pages with varying numbers of radio buttons. These buttons have two different
I have two buttons that moves a movieclip up/down the Y-axis. How do I
I have two buttons, one for start recording and another for stop recording. i
I have two buttons, which the user can click and will open a FileDiagloag
I have two buttons at the top of a jQuery validated form: one saves
I have two buttons in my layout say b1 and b2. By pressing b1

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.