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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T03:30:06+00:00 2026-05-24T03:30:06+00:00

I have created a JDialog which contains a JComboBox and a panel underneath which

  • 0

I have created a JDialog which contains a JComboBox and a panel underneath which should display a different content based on the value selected in the JComboBox. I have created a JPanel (panel_1) which is added to the content pane of the dialog and an additional JPanel for each of the possible items in the JComboBox (for example panel_item_1 and panel_item_2 if it does have only 2 items). I have attached the following listener class in the JComboBox:

public class SelectedListener implements ActionListener {

private SettingsDialog dialog;

public SelectedListener(SettingsDialog dialog){

    this.dialog = dialog;

}

public void actionPerformed(ActionEvent e) {

   JComboBox cb = (JComboBox)e.getSource();
       String selected_settings = (String)cb.getSelectedItem();

       if(selected_settings.compareTo("Option 1") == 0){

        dialog.panel_1 = dialog.panel_item_1;
        dialog.panel_1.updateUI();

    }else if(selected_settings.compareTo("Option 2") == 0 ){

        dialog.panel_1 = dialog.panel_item_2;
        dialog.panel_1.updateUI();

    }

}

}

However this doesn’t make the panel update with the new content. Any suggestion? Thanks in advance

  • 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-24T03:30:07+00:00Added an answer on May 24, 2026 at 3:30 am

    which should display a different content based on the value selected in the JComboBox.

    Read the section from the Swing tutorial on How to Use Card Layout which has a working example that does exactly what you want.

    Edit:

    dialog.panel_1 = dialog.panel_item_1; 
    

    The real problem is that you can’t just change the reference to a variable and expect the component to show up on the panel. You still need to add the component to the panel before you do a revalidate() on the panel. So your code is like:

    panel.remove(...);
    panel.add(...);
    panel.revalidate();
    panel.repaint();
    

    However, the better solution is to use a CardLayout which does all this work for you.

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

Sidebar

Related Questions

I have created a JDialog which contains a JLabel. Because the length of text,
I have created a JDialog which displays two different tables. When the user selects
I have created a Silverlight project which also contains a Web project. I have
I have a JDialog dlg, created by a JFrame frm, that contains a JList
I have created a JDialog to be opened when I click on the edit
Example now I have a main frame contains jtable display all the customer information,
I have created a PHP form, which, upon submission, goes to another PHP page.
Have created a ATL COM project through which I am inserting Menu Items to
I have created domain model and define entities, value objects, Services and so on.
I have created the following class implementing a ListSelectionListener interface. This class should listen

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.