Jake Badlands
Jake Badlands

Reputation: 1026

How to refresh the JComboBox data?

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...

Upvotes: 3

Views: 36058

Answers (4)

William Hou
William Hou

Reputation: 1771

To my knowledge, ComboBox cannot not be refreshed with ".removeAllItems()", ".removeAll()", or ".repaint()".

If you want to refresh it, you have to create a new ComboBox every time, and then add items to it. With the above-given code as an example:

    userBox1 = new JComboBox(); // to replace userBox1.removeAllItems();
    userBox2 = new JComboBox(); // to replace 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);
    }

I had similar issues, but I solved them this way.

Upvotes: 0

This code is the button click event to refresh jframeform in the button click event.

new room().show();  //room() is a jframeform
new room().setVisible(false);

Upvotes: -3

mKorbel
mKorbel

Reputation: 109813

  • 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)

Upvotes: 7

Jack
Jack

Reputation: 133577

static class TestFrame extends JFrame implements ActionListener
{
    private JComboBox testBox = new JComboBox();
    private JButton testButton = new JButton();
    int c = 0;

    public TestFrame()
    {
        testBox = new JComboBox();
        testButton = new JButton("Click Me!");
        testButton.addActionListener(this);

        JPanel panel = new JPanel(new GridLayout(2,1));
        panel.add(testBox);
        panel.add(testButton);
        this.add(panel);
        pack();
        setVisible(true);
    }

    public void actionPerformed(ActionEvent e)
    {
        testBox.addItem("test" + c++);
    }
}

This test case works, are you sure you added the listener to the component that is clicked?

Upvotes: 3

Related Questions