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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T06:38:16+00:00 2026-06-05T06:38:16+00:00

I am having trouble refreshing the data inside the JComboBox . There is a

  • 0

I am having trouble refreshing the data inside the JComboBox.

There is a button “Create” which has ActionListener, which adds the item to JComboBox.

But the changes are not reflected in the GUI: I still don’t see the new added item.

repaint() doesn’t help.

Update: Here is a (almost) full GUI code:

public class Main extends JFrame implements ActionListener
{
    static Connection conn;
    static PreparedStatement ps = null;
    static ResultSet res;

    static Statement sta;

private final static int ITERATION_NUMBER = 1000;

public void GUI () throws SQLException {
    setBounds(0, 0, 320, 240);
    addWindowListener(new WindowAdapter(){
    public void windowClosing(WindowEvent we){
        close(ps);
        close(res);
        close(conn);
        System.exit(0);
        }
    });
    setMinimumSize(new Dimension(320, 240));
    setResizable(false);

    this.setTitle("Accounts");

    JPanel panel = new JPanel();
    GridLayout2 GL = new GridLayout2(4,3);
    GL.setHgap(10);
    panel.setLayout(GL);

    Font font = new Font("Serif", Font.BOLD, 20);
    Font font2 = new Font("Courier New", Font.BOLD, 16);

    JLabel label1 = new JLabel("Username");
    JLabel label2 = new JLabel("Password");
    JLabel label3 = new JLabel("Controls");

    label1.setFont(font2);
    label2.setFont(font2);
    label3.setFont(font2);

    final JTextField username = new JTextField();
    final JTextField password1 = new JPasswordField();
    final JTextField password2 = new JPasswordField();

    final JComboBox userBox1 = new JComboBox();
    final JComboBox userBox2 = new JComboBox();

    JButton create = new JButton("CREATE");

    create.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e)
        {
            try {
                createUser(conn, username.getText(), password1.getText());
userBox1.addItem(username.getText());
                userBox2.addItem(username.getText());
            } catch (NoSuchAlgorithmException
                    | UnsupportedEncodingException | SQLException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            }
        }
    });



    userBox1.removeAllItems();
    userBox2.removeAllItems();

    res = (ResultSet) sta.executeQuery("SELECT LOGIN FROM ACCOUNTS");

    String temp;

    for (int i=0; res.next(); i++) {
        temp = (String)res.getString("LOGIN");
        userBox1.addItem(temp);
        userBox2.addItem(temp);
    }

    panel.add(label1);
    panel.add(label2);
    panel.add(label3);

    panel.add(username);
    panel.add(password1);
    panel.add(create);

    panel.add(userBox1);
    panel.add(password2);
    panel.add(modify);

    panel.add(userBox2);
    panel.add(new JLabel(""));
    panel.add(delete);

    add(panel);

    setVisible(true);
}

SOLUTION: Adding password1.setText(“”); just after “createUser” solved the problem! That’s strange, maybe it somehow refreshed the GUI…

  • 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-05T06:38:17+00:00Added an answer on June 5, 2026 at 6:38 am
    • you have to add ComboBoxModel to the JComboBox,

    • there you can to add / remove / modify the value,

    • events implemented in the API refreshing your view (JComboBox) without moreover code lines

    • all updates must be done on Event Dispatch Thread

    EDIT

    maybe I missread your question, if you want to add JComboBox to the already visible GUI, then you have to call (as last code lines and with success only once for one Container)

    myContainer.revalidate() // for JFrame up to Java7 is there only validate()
    myContainer.repaint()
    

    (sorry @timaschew)

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

Sidebar

Related Questions

Having trouble with this -- a couple of other related posts out there, but
I'm having trouble refreshing a view after a form submit creates an Item on
I'm having trouble with refreshing a nimbus image and not sure if I'm correctly
I'm having trouble with refreshing objects in my database. I have an two PC's
I am new to JOGL but I am having trouble with the basics. I
I am having some trouble with refreshing the related collection of entities. Essentially the
Having trouble finding a solution for my situation here. Sorry if this has been
Having trouble finding the first character of Field42 inside xpath, see below. Any suggestions?
Having trouble with this..I'm sure I'm missing something simple but I simply want the
Having trouble when trying to create a class using another class(and 2 inner classes),

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.