'How to reload a panel without using CardLayouts?

TLDR; :
I'm looking for a way to reload JPanels and its components.

I'd like to implement a dynamically changing JComboBox. This is the situation:
There is a button, which generates data in background ("generate content") here it is just some random numbers. After the data is generated, this data should be selectable inside a JComboBox.

/**
 * Initialize the contents of the frame.
 */
private void initialize() {
    frame = new JFrame();
    frame.setBounds(100, 100, 450, 300);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    panel = new JPanel();
    frame.getContentPane().add(panel, BorderLayout.CENTER);
    panel.setLayout(null);

    comboBox = new JComboBox<String>();

    comboBox.setBounds(39, 63, 144, 20);
    panel.add(comboBox);

    btnGenerateContent = new JButton("generate content");
    btnGenerateContent.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            comboContent = fillrandoms();
            comboBox = new JComboBox<>(fillrandoms());

        }
    });
    btnGenerateContent.setBounds(39, 11, 117, 23);
    panel.add(btnGenerateContent);
}

private static String[] fillrandoms() {
    String[] array = new String[10];
    for (int i = 0; i < 10; i++) {
        array[i] = "" + Math.random() * 100;

    }

    return array;
}

now when I execute the program, it will declare and initalize the combobox before there is data to display. My idea was now that when I hit the "generate content" button, this will also update the UI for the panel or the ComboBox. but when I insert panel.updateUI() or comboBox.updateUI() nothing happens.
After researches I found some ways to do it with CardLayouts, but I do not use them for my JComboBox, so I propably need another way. I don't even know if this is the common way or is there any other better way.



Solution 1:[1]

To update UI use method panel.revalidate(), panel.repaint(), or revalidate(). revalidate() and repaint() method are proper way to reflect UI update task.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Java-Dev