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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T20:38:18+00:00 2026-06-01T20:38:18+00:00

I have a Swing JFrame, which has a bunch of menu items. The below

  • 0

I have a Swing JFrame, which has a bunch of menu items. The below code tries but fails to set the content of the JFrame on selection of ‘Add Address’ menu item.

        JFrame frame = new JFrame();

    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setResizable(false);
    frame.setVisible(true);

    JMenuBar menuBar = new JMenuBar();
    JMenu clientMenu = new JMenu("Client");
    JMenuItem address = new JMenuItem("Add Address");

    clientMenu.add(address);
    menuBar.add(clientMenu);

    address.addActionListener(new ActionListener() {

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

    private void setupAddressForm() {
        frame.getContentPane().removeAll();

        JPanel infoPanel = new JPanel();
        JLabel infoLabel = new JLabel();
        infoLabel.setText("<html><B>Please enter the address below</B></html>");
        infoPanel.add(infoLabel, SwingConstants.CENTER);
        frame.getContentPane().add(infoPanel, BorderLayout.NORTH);

        JPanel buttonPanel = new JPanel();
        JButton submitButton = new JButton("Submit Address");
        JButton resetButton = new JButton("Reset");

        submitButton.setToolTipText("Submit");
        resetButton.setToolTipText("Reset");

        buttonPanel.add(submitButton);
        buttonPanel.add(resetButton);

        frame.getContentPane().add(buttonPanel, BorderLayout.SOUTH);

        JPanel fieldPanel = new JPanel();
        fieldPanel.setLayout(new GridLayout(10, 10));

        JLabel houseNumberLabel = new JLabel("House Number",
                SwingConstants.LEFT);
        final JTextField houseNumberField = new JTextField(3);

        JLabel streetNameLabel = new JLabel("Street Name", SwingConstants.LEFT);
        final JTextField streetNameField = new JTextField(10);

        JLabel localityLabel = new JLabel("Locality", SwingConstants.LEFT);
        final JTextField localityField = new JTextField(10);

        JLabel cityLabel = new JLabel("City", SwingConstants.LEFT);
        final JTextField cityField = new JTextField(10);

        JLabel stateLabel = new JLabel("State", SwingConstants.LEFT);
        final JTextField stateField = new JTextField(10);

        JLabel zipCodeLabel = new JLabel("Zip Code", SwingConstants.LEFT);
        final JTextField zipCodeField = new JTextField(10);

        fieldPanel.add(houseNumberLabel);
        fieldPanel.add(houseNumberField);

        fieldPanel.add(streetNameLabel);
        fieldPanel.add(streetNameField);

        fieldPanel.add(localityLabel);
        fieldPanel.add(localityField);

        fieldPanel.add(cityLabel);
        fieldPanel.add(cityField);

        fieldPanel.add(stateLabel);
        fieldPanel.add(stateField);

        fieldPanel.add(zipCodeLabel);
        fieldPanel.add(zipCodeField);

        frame.getContentPane().add(fieldPanel, BorderLayout.CENTER);

        submitButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {

                String houseNumber = houseNumberField.getText();
                String street = streetNameField.getText();
                String locality = localityField.getText();
                String city = cityField.getText();
                String state = stateField.getText();
                String zipCode = zipCodeField.getText();

                Address address = new Address();

                if (!"".equals(houseNumber) && !"".equals(street)
                        && !"".equals(locality) && !"".equals(city)
                        && !"".equals(state) && !"".equals(zipCode)) {
                    address.setHouseNumber(houseNumber);
                    address.setStreetName(street);
                    address.setLocality(locality);
                    address.setState(state);
                    address.setZipCode(zipCode);
                }

            }
        });

        frame.getContentPane().doLayout();
        frame.getContentPane().update(frame.getGraphics());

}

When I select the ‘Add Address’ menu item, the frame just goes blank. Not what I expected. I debugged using break point and see that the method to setup the address is being called.
I can’t figure out why it doesn’t work. Could any one point out if I am doing anything wrong here?

  • 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-01T20:38:19+00:00Added an answer on June 1, 2026 at 8:38 pm

    Why go through all this gymnastics? Instead simply use a CardLayout to swap views for you with minimal mess or fuss.

    Also note that the JFrame you create on line (A) in either a method or constructor (you don’t say):

    JFrame frame = new JFrame(); // (A)
    

    is not the same JFrame as the one used on lines (B) and (C):

      private void setupAddressForm() {
          frame.getContentPane().removeAll(); // (B)
    
          JPanel infoPanel = new JPanel();
          JLabel infoLabel = new JLabel();
          infoLabel.setText("<html><B>Please enter the address below</B></html>");
          infoPanel.add(infoLabel, SwingConstants.CENTER);
          frame.getContentPane().add(infoPanel, BorderLayout.NORTH); // (C)
    

    This is because you declare the JFrame on line (A) and so it is only visible within that method or constructor.

    Edit 1
    Also, this is not right:

      frame.getContentPane().doLayout();
      frame.getContentPane().update(frame.getGraphics());
    

    As that’s not how you get new components to show in a container for Swing. Instead I’d do something like so:

      JPanel contentPane = (JPanel) frame.getContentPane();
      contentPane.revalidate();
      contentPane.repaint();
    

    But again, I’d really do neither, and instead would use a CardLayout as noted above.

    Finally, if any of these recommendations don’t make sense, please ask for clarification.

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

Sidebar

Related Questions

I have the following code: import javax.swing.JEditorPane; import javax.swing.JFrame; import javax.swing.JScrollPane; import javax.swing.ScrollPaneConstants; public
I have a JFrame which has 3 JPanels in GridBagLayout .. Now, when I
Hi I work with netbeans. I have written a code which has two classes
The code below plots some simple x-y data, but it has two problems that
I have a JFrame with a CardLayout set as its layout manager. This has
I have this code: class FinalUI1 extends javax.swing.JFrame { //do something Thread t; try
I have a single Swing JFrame which contains a couple JLabels, a few read-only
I am using swing to create my GUI. J have a JFrame containing one
I have a Swing runnable app which updates messages, then I have a Java
This is my code I have developed. This is the main program which holds

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.