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 :(