简体   繁体   中英

I can't update Jcombobox (via model) from a JDialog

I have a simple UI in which my button that calls my method updateModelCmb(), this method just increases the value of a counter and updates the model. The button seems to add the proper values to the model just fine. But when I do the same in my secondUI class, the model does not gets updated... am I doing something wrong? here is my code:

package testing;

import java.awt.BorderLayout;
import java.awt.EventQueue;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import java.awt.FlowLayout;
import javax.swing.JButton;
import javax.swing.JComboBox;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class OneUI extends JFrame {

private JPanel contentPane;
private JComboBox comboBox ;
private DefaultComboBoxModel modeltest;
private Integer count=0;
private JButton btnOpenSecondUi;
/**
 * Launch the application.
 */
public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {
        public void run() {
            try {
                OneUI frame = new OneUI();
                frame.setVisible(true);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });
}

/**
 * Create the frame.
 */
public OneUI() {
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setBounds(100, 100, 450, 300);
    contentPane = new JPanel();
    contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
    setContentPane(contentPane);
    contentPane.setLayout(null);

    JButton btnAddOne = new JButton("Add 1 element");
    btnAddOne.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent arg0) {
            updateModelCmb();
        }
    });
    btnAddOne.setBounds(187, 46, 129, 23);
    contentPane.add(btnAddOne);
    modeltest= new DefaultComboBoxModel() ;
    comboBox= new JComboBox();
    comboBox.setBounds(48, 47, 129, 20);
    comboBox.setModel(modeltest);
    contentPane.add(comboBox);

    btnOpenSecondUi = new JButton("Open second UI");
    btnOpenSecondUi.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            new SecondUI();
        }
    });
    btnOpenSecondUi.setBounds(155, 163, 161, 23);
    contentPane.add(btnOpenSecondUi);
}

public void updateModelCmb(){
    count++;
    modeltest.addElement(count);
    comboBox.setModel(modeltest);

}

}

This is the second class which does not seem to work.

package testing;

import java.awt.BorderLayout;

public class SecondUI extends JDialog {

private final JPanel contentPanel = new JPanel();

/**
 * Create the dialog.
 */
public SecondUI() {
    setVisible(true);
    setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
    setBounds(100, 100, 327, 142);
    getContentPane().setLayout(new BorderLayout());
    contentPanel.setLayout(new FlowLayout());
    contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
    final OneUI obj = new OneUI();
    getContentPane().add(contentPanel, BorderLayout.CENTER);
    {
        JButton btnAddOneElement = new JButton("Add 1 element");
        btnAddOneElement.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                obj.updateModelCmb();
            }
        });
        contentPanel.add(btnAddOneElement);
    }
}

}

Please help :(

in your SecondUI you are creating new instance of OneUI

final OneUI obj = new OneUI();
obj.setVisible(true);// you don't call even this

but you don't call it's visible to true.so there is a hidden jframe and combobox that combobox get updated but not first frame's combobox and you can't see updated combobox because the frame is not visible

to fix this pass oneui reference to secondui then call method of that reference

example OneUI.java

public class OneUI extends JFrame {

    private JPanel contentPane;
    private JComboBox comboBox;
    private DefaultComboBoxModel modeltest;
    private Integer count = 0;
    private JButton btnOpenSecondUi;
    private SecondUI secondui;

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {

            public void run() {
                OneUI frame = new OneUI();
                frame.setVisible(true);
            }
        });
    }

    public OneUI() {

        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100, 100, 450, 300);
        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        setContentPane(contentPane);
        contentPane.setLayout(null);

        JButton btnAddOne = new JButton("Add 1 element");
        btnAddOne.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
                updateModelCmb();
            }
        });
        btnAddOne.setBounds(187, 46, 129, 23);
        contentPane.add(btnAddOne);
        modeltest = new DefaultComboBoxModel();
        comboBox = new JComboBox();
        comboBox.setBounds(48, 47, 129, 20);
        comboBox.setModel(modeltest);
        contentPane.add(comboBox);

        btnOpenSecondUi = new JButton("Open second UI");
        btnOpenSecondUi.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
               secondui= new SecondUI(OneUI.this); // pass reference of this oneui  to secondui .so secondui can catch reference of this class[this frame] and update this combobox by calling updateModelCmb on this reference

            }
        });
        btnOpenSecondUi.setBounds(155, 163, 161, 23);
        contentPane.add(btnOpenSecondUi);
    }

    public void updateModelCmb() {
        count++;
        modeltest.addElement(count);
        comboBox.setModel(modeltest);

    }

}

SecondUI.java

public class SecondUI extends JDialog {

    private final JPanel contentPanel = new JPanel();

    public SecondUI(OneUI oneui) {
        //setVisible(true); // don't call setvisible here call setvisible after you add all the component .
        setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
        setBounds(100, 100, 327, 142);
        getContentPane().setLayout(new BorderLayout());
        contentPanel.setLayout(new FlowLayout());
        contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));

        getContentPane().add(contentPanel, BorderLayout.CENTER);
        {
            JButton btnAddOneElement = new JButton("Add 1 element");
            btnAddOneElement.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    oneui.updateModelCmb();
                }
            });
            contentPanel.add(btnAddOneElement);
        }
        setVisible(true); // call setvisible at last
    }

}

use This May Work : ) .............

public SecondUI() {
    setVisible(true);
    setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
    setBounds(100, 100, 327, 142);
    getContentPane().setLayout(new BorderLayout());
    contentPanel.setLayout(new FlowLayout());
    contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
    final OneUI obj = new OneUI();
    getContentPane().add(contentPanel, BorderLayout.CENTER);
    {
        JButton btnAddOneElement = new JButton("Add 1 element");
        btnAddOneElement.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                obj.updateModelCmb();
            }
        });
        contentPanel.add(btnAddOneElement);
    }

    UpdateUI();    //added by Hamreen Ahmad

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM