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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T19:26:16+00:00 2026-05-24T19:26:16+00:00

I have 2 Jcombo Boxs: which is combo1 and combo2 I choose combo1 and

  • 0

I have 2 Jcombo Boxs: which is combo1 and combo2

I choose combo1 and I can get information for combo2 but The problem is I can get informatiob for combo2 but it is not updated. I also try to use updata.UI() but it doesn’t help.

This is the code in side

public void actionPerformed(ActionEvent e) { 
    JComboBox cb = (JComboBox)e.getSource();
    String uname1 = (String)cb.getSelectedItem();

        combo2 = update(uname1);
        combo2.updateUI();

}

This is code inside update

protected JComboBox update(String name) {
    JComboBox tmp = new JComboBox();
    //Read Content from XML file (University is bigger than Year)
    NodeList nList = doc.getElementsByTagName("University");
    System.out.println("Inside Fn " + name);
    for(int i = 0 ; i < nList.getLength();i++) {
        Element el = (Element)nList.item(i);
        if(name.contentEquals(el.getAttributeNode("name").getNodeValue()))
        {
            NodeList tmpyList = el.getElementsByTagName("Year");
            for(int j = 0 ; j < tmpyList.getLength();j++) 
            {
                Element yl = (Element)tmpyList.item(j);
                System.out.println(yl.getAttribute("yr"));
                tmp.addItem(yl.getAttribute("yr"));
            }
        }
    }
    return tmp; //Return ComboBox to combo2
}

Thank you for your kindness, I try to use your code but it is not work (It still not update), please help me

This is my constructor

public JFrameExample() {
    String[] comboboxdefault = { "Select" };

    JComboBox combo1 = Universitylist();
    JComboBox combo2 = new JComboBox(comboboxdefault);
    JComboBox combo3 = new JComboBox(comboboxdefault);

    uList.addActionListener(this);
    yList.addActionListener(this);
    dList.addActionListener(this);


    JPanel student_information = new JPanel(new GridLayout(0,1));
    uList.setName("University List");
    yList.setName("Year List");

    // University List

    student_information.add(combo1);
    // Database Year List

    student_information.add(combo2);
    // Programme List

    student_information.add(combo3);
    //Add Components to this container, using the default FlowLayout.
    add(student_information);

}

This is the combo2 Update it is return String Array

protected String[] updateyList(String name)  
{
    String[] tmp = null;

           //Read from XML file
    for(int i = 0 ; i < nList.getLength();i++) {
        Element el = (Element)nList.item(i);
        if(name.contentEquals(el.getAttributeNode("name").getNodeValue()))
        {
            NodeList tmpyList = el.getElementsByTagName("Year");
            tmp = new String[tmpyList.getLength()];
            for(int j = 0 ; j < tmpyList.getLength();j++) 
            {
                Element yl = (Element)tmpyList.item(j);
                //Add to String Array
                tmp[j] = yl.getAttribute("yr");
            }
        }
    }

    return tmp;
}

In the Action Perform

public void actionPerformed(ActionEvent e) {
    JComboBox cb = (JComboBox)e.getSource();
    String uname1 = (String)cb.getSelectedItem();
    System.out.println(cb.getName()); // To make sure I got the combo1. 
    try {
        //I change to the model method 
        DefaultComboBoxModel model = new DefaultComboBoxModel(  updateyList(uname1) );
        System.out.println(model.getSize()); 
        combo2 = new JComboBox(); // If I don't have this line it will throw  error Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
        combo2.setModel(model);

    } catch (ParserConfigurationException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    } catch (SAXException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    } catch (IOException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    }

}

And this is for creating GUI function

private static void createAndShowLoginGUI() {

   //Create and set up the window.
    JFrame frame = new JFrame("Login");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    //Create and set up the content pane.
    JFrameExample newContentPane = new JFrameExample();
    newContentPane.setOpaque(true); //content panes must be opaque
    frame.setContentPane(newContentPane);

    //Display the window.
    frame.pack();
    frame.setVisible(true);
}

This is main function

public static void main(String[] args)
{
    //Schedule a job for the event-dispatching thread:

    //creating and showing this application's GUI.

    javax.swing.SwingUtilities.invokeLater(new Runnable() {
        public void run() {
            createAndShowLoginGUI(); 
        }
    });
}

I think I did something wrong but I don’t know where

  • 1 1 Answer
  • 1 View
  • 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-24T19:26:17+00:00Added an answer on May 24, 2026 at 7:26 pm

    As you do now, you recreate the combo box every time (by returning the tmp in update method). This as it seems is not reflecting in the UI and maybe others will post the reason. But if you can change to update the combo values (changing the model or deleting the current values and adding the new ones) instead of recreating, then the following post can help you Dynamically change JComboBox

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

Sidebar

Related Questions

I have a little problem. I had some JComboBox to a JDialog but they
HI, I have a JComboBox to which I'm adding my custom object items. But
I have a JComboBox already created but I would like to update the information
I have strange problem. I have class which behaves similar dropdown list. package test.view;
Currently I have this JComboBox , how can I get to center the content
I do have a strange focus problem when using a JComboBox inside an embedded
have written this little class, which generates a UUID every time an object of
Have a procedure which looks like Procedure TestProc(TVar1, TVar2 : variant); Begin TVar1 :=
Have deployed numerous report parts which reference the same view however one of them
have a problem. At first look at this HTML <div id=map style=background-image: url(map.png); width:

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.