1

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

SargeOrona
  • 90
  • 1
  • 1
  • 8

2 Answers2

6

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
    }

}
Madhawa Priyashantha
  • 9,633
  • 7
  • 33
  • 60
  • 1
    And one more thing, `setVisible(true)`should always set as the last call. First add the components then show the UI. – Kami Jul 29 '15 at 18:34
  • Thanks @Kami, your solution seems to be working, although now that I have changed the code as you suggested, when I click the secondUI button a third frame shows up. How can I prevent this, and just update the old one? – SargeOrona Jul 29 '15 at 18:40
  • 2
    @SargeOrona when you call your `SecondUI` then pass the reference of your `OneUI` as a parameter to the constructor of `SecondUI` – Kami Jul 29 '15 at 18:48
  • 1
    @SargeOrona `public void actionPerformed(ActionEvent e) { new SecondUI(this); }` and change the constructor of `SecondUI` to `SecondUI(OneUI oneUI)` to so the reference of OneUI is passed to SecondUI then you can access OneUI instance within SecondUI without creating a new one. – Kami Jul 29 '15 at 18:52
  • 2
    @LuxxMiner thanks i hate copy paste :D :D you are right it should not be `new SecondUI(this);` I wanted to write `new SecondUI(OneUI.this);` – Kami Jul 29 '15 at 18:58
  • @LuxxMiner for information about `.this` Syntax, you can read [this](http://stackoverflow.com/a/5666168/3102124) – Kami Jul 29 '15 at 19:01
  • @LuxxMiner dont I need to declare the OneUi oneui object as final in the constructor of SecondUI?? – SargeOrona Jul 29 '15 at 19:13
  • @SargeOrona nope . you can use my updated code i have commented important things – Madhawa Priyashantha Jul 29 '15 at 19:15
  • 1
    It works now!! Thank you all for helping me on this, It was driving me crazy! I had to declare the object as final though. – SargeOrona Jul 29 '15 at 19:17
  • @Fast Snail plus one, but setVisible should be last code line or wrapped into invokeLater – mKorbel Jul 30 '15 at 06:46
0

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

}