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.