2

I have a Object called menuBarObject. Its class hides a boolean called "changed". It returns true, if a submenu is clicked. In my Mainclass I built the menuBarObject. I can get the value of changed, by a method called "hasChanged". If the main Method has done its job, it will all a method called "contentIsBuild" which sets changed to false again. Could I write a Listener who gets active, if changed returns true? I could do that in the menuBar Class. But I dont like that solution.

My Code to put in that listener:

if (newCustomerIsSelected) {
    //Content Area   
    JPanel contentArea = new JPanel();
    //Selection from Sub Menu
    String selectedName = menuBarObject.getSelectedCustomer();
    //Selected Customer Object / Instanze of selectecCustomer Class!
    SelectedCustomer selectedCustomerObject = new SelectedCustomer(selectedName);
    //The seleted Customer
    Customer selectedCustomer = selectedCustomerObject.getSelectedCustomer();
    //Name of selected Customer
    String dataOfSelectedCustomer = selectedCustomer.toString();
    //Creating the content Area
    setCustomerContentArea(dataOfSelectedCustomer, contentArea);
    //Call Menu Bar that content is build
    menuBarObject.contentIsBuild(true);
}
deHaar
  • 17,687
  • 10
  • 38
  • 51
  • 2
    why would you do that? just add that in the listener that handles the action when you click the item – Stultuske Jan 25 '19 at 08:29
  • Have that in menuBar Class to set changed. But a Menu Bar should not be responsible for building content. Would be easier, i know. ^^ –  Jan 25 '19 at 08:31
  • make your own thread, with a while(!changed) loop on it, constantly checking the "changed" boolean var's value – aran Jan 25 '19 at 08:37
  • 1
    I would suggest maybe having a look at the `ChangeListener`, which is already available in the API - [for example](https://stackoverflow.com/questions/20153868/using-changelistener-to-fire-changes-in-java-swing/20158587#20158587) – MadProgrammer Jan 25 '19 at 08:52
  • What do you mean by "a submenu is clicked"? Is that when a button is clicked which spawns a submenu or a window where any click on it sets the `changed` field true? Or something else? – James Birch Jan 25 '19 at 09:38

1 Answers1

0

Instead of using changed to record when a submenu is clicked you can use a MouseListener (https://docs.oracle.com/javase/tutorial/uiswing/events/mouselistener.html) to listen for clicks (assuming that your submenu extends the Component class e.g. is a JPanel or something similar).

In the class where you instantiate your submenu you would then do:

submenu.addMouseListener(new MouseListener()
        {

            @Override
            public void mouseReleased(MouseEvent e)
            {
                // Do nothing
            }

            @Override
            public void mousePressed(MouseEvent e)
            {
                // Do nothing
            }

            @Override
            public void mouseExited(MouseEvent e)
            {
                // Do nothing
            }

            @Override
            public void mouseEntered(MouseEvent e)
            {
                // Do nothing
            }

            @Override
            public void mouseClicked(MouseEvent e)
            {
                if (newCustomerIsSelected) {
                    //Content Area   
                    JPanel contentArea = new JPanel();
                    //Selection from Sub Menu
                    String selectedName = menuBarObject.getSelectedCustomer();
                    //Selected Customer Object / Instanze of selectecCustomer Class!
                    SelectedCustomer selectedCustomerObject = new SelectedCustomer(selectedName);
                    //The seleted Customer
                    Customer selectedCustomer = selectedCustomerObject.getSelectedCustomer();
                    //Name of selected Customer
                    String dataOfSelectedCustomer = selectedCustomer.toString();
                    //Creating the content Area
                    setCustomerContentArea(dataOfSelectedCustomer, contentArea);
                    //Call Menu Bar that content is build
                    menuBarObject.contentIsBuild(true); // N.B. This would no longer need to set the changed variable to false.
                }
            }
        });

If changed is important to your application then you could look at using a PropertyChangeSupport (https://docs.oracle.com/javase/tutorial/uiswing/events/propertychangelistener.html) to fire your own events. This would probably best be placed in the same class that sets changed = true.

This would look like:

private final PropertyChangeSupport pcs = new PropertyChangeSupport(this);

... // Other code in class

public void addChangeListener(PropertyChangeListener listener) {
    this.pcs.addPropertyChangeListener(listener);
}

...

// In the method which sets changed = true
this.pcs.firePropertyChange(null, null, null) // Args are: Property name, old value and new value - set as necessary

Then in the method where you want your listener code to go you would add:

// object is the name of the instantiation of the class which sets changed = true
object.addChangeListener(new PropertyChangeListener()
        {

            @Override
            public void propertyChange(PropertyChangeEvent evt)
            {
                if (newCustomerIsSelected) {
                    //Content Area   
                    JPanel contentArea = new JPanel();
                    //Selection from Sub Menu
                    String selectedName = menuBarObject.getSelectedCustomer();
                    //Selected Customer Object / Instanze of selectecCustomer Class!
                    SelectedCustomer selectedCustomerObject = new SelectedCustomer(selectedName);
                    //The seleted Customer
                    Customer selectedCustomer = selectedCustomerObject.getSelectedCustomer();
                    //Name of selected Customer
                    String dataOfSelectedCustomer = selectedCustomer.toString();
                    //Creating the content Area
                    setCustomerContentArea(dataOfSelectedCustomer, contentArea);
                    //Call Menu Bar that content is build
                    menuBarObject.contentIsBuild(true);
                }
            }
        });

Edit: Fixed incorrect method name.

James Birch
  • 321
  • 1
  • 7
  • Thanks for your response. But when when I write: "this.pcs.firePropertyChange(changed, false, true)", I get a error, cause I cant use a boolean on this method. –  Jan 28 '19 at 09:33
  • That's because the first argument of firePropertyChange is a String which represents the name of the property event. You can store an attribute which stores the name e.g. `final String CHANGED_EVENT = "CHANGED_EVENT";`. Then firePropertyChange would take arguments (CHANGED_EVENT, false, true). You can then access the PropertyChangeEvent `evt` for the property name and check that if it equals CHANGED_EVENT. If it does then you know it relates to your `changed` variable being updated. – James Birch Jan 28 '19 at 09:58