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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T15:49:26+00:00 2026-06-15T15:49:26+00:00

I have two views : test1 and test2 Switching from test1 to test 2

  • 0

I have two views : test1 and test2

Switching from test1 to test 2 works fine, but when I switch back from test2 the contentPanel is empty.
This is my code.
Can someone point out what I’m doing wrong?

test1

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JButton;


public class test1 extends JFrame {

JPanel contentPane;

/**
 * Launch the application.
 */
public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {
        public void run() {
            try {
                test1 frame = new test1();
                frame.setVisible(true);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });
}

/**
 * Create the frame.
 */
public test1() {
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setBounds(100, 100, 450, 300);
    contentPane = new JPanel();
    contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
    contentPane.setLayout(new BorderLayout(0, 0));
    setContentPane(contentPane);

    JButton btnView = new JButton("View2");
    contentPane.add(btnView, BorderLayout.CENTER);
    btnView.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent arg0) {
            contentPane.removeAll();
            contentPane.invalidate();
            test2 obj=new test2();
            obj.contentPane.setVisible(true);
            contentPane.add(obj.contentPane);
            ((JPanel)contentPane).revalidate();
            contentPane.repaint();          }
    });
}

}

test2

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JButton;

public class test2 extends JFrame {

JPanel contentPane;

/**
 * Launch the application.
 */
public static void main(String[] args) {

    EventQueue.invokeLater(new Runnable() {
        public void run() {
            try {
                test2 frame = new test2();
                frame.setVisible(true);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });
}

/**
 * Create the frame.
 */
public test2() {
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setBounds(100, 100, 450, 300);
    contentPane = new JPanel();
    contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
    setContentPane(contentPane);
    contentPane.setLayout(null);

    JLabel lblThisIsView = new JLabel("this is view 2!");
    lblThisIsView.setBounds(178, 107, 110, 14);
    contentPane.add(lblThisIsView);

    JButton btnView = new JButton("View 1!");
    btnView.setBounds(168, 161, 89, 23);
    contentPane.add(btnView);

    btnView.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent arg0) {
            contentPane.removeAll();
            contentPane.invalidate();
            test1 obj=new test1();
            obj.contentPane.setVisible(true);
            contentPane.add(obj.contentPane);
            ((JPanel)contentPane).revalidate();
            contentPane.repaint();
        }
    });
}
    }
  • 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-15T15:49:27+00:00Added an answer on June 15, 2026 at 3:49 pm

    To the current frame I just set:

    "classname.this.setVisible(false);"
    

    Frame switching works fine now. It isn’t the right method but my project submission is today and the solution is simple enough to implement everywhere.

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

Sidebar

Related Questions

I have two image views. They are puzzle pieces I want to test if
I have two classes ( Test1 and Test2 ) that each have a property
I have two model: test1 , test2 And an action in test1 : public
I have two views, both under the NavigationController. I don't want View 1 (on
I have two views, jumpBarPortrait and jumpBarLandscape I would like to animate between the
I have two views in my ViewFlipper . One of views contains a ListView
I have two views, one view takes the whole screen, the second view covers
I have two views within one .xib (one view for landscape, another for portrait).
I have two views that I would like to combine into one. The first
I have two views.In first view, I have a table view and I am

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.