In a Swing project, I have used a JFrame and divided it into three parts: a menu pane, a context pane, and a status pane.
The status pane shows to the user whether his actions was successful or not, along with other information that the user might want to know, upon taking an action. For example, if the user clicks a delete button in the context menu, the status panel indicates whether the operation was done successfully.
The problem with the status panel is that it needs to update its status after a while. For instance, if the user clicks on another button or navigates through other pages in the context pane, the status pane shouldn’t be still showing the results of the last delete operation.
There are two workarounds for this, that I can think of:
- clear the status after a few seconds.
- add an mouse or keyboard action listener to all of the components(
JPanels,JTextFields, and …) so that they call the status panel to clear the status, upon an activity from the mouse or keyboard.
Although implementing the first option is relatively simple, but it’s not enough. I think I need a combination of the two. However the problem is that it would be a very dirty approach if I want to add these action listeners to all the components. Maybe I can add it to the main frame. I did (the code is given below), but if works only if the mouse is moving on the frame’s own panel (not on the context pane, for example)
Do you have any suggestions?
JFrame.this.addMouseMotionListener(new MouseMotionListener() {
@Override
public void mouseDragged(MouseEvent me) {
}
@Override
public void mouseMoved(MouseEvent me) {
Config.statusPanel.clearStatus();
}
});
No, using a MouseListener for this is kind of crazy, and you yourself say that it’s a dirty solution. Instead I think you’re much better off using a PropertyChangeListener to listen to the states of your classes, most likely the Model class, and none of the view classes.
For example, the user pushes a JButton (a component in the view class), the control class reacts to this button push and tells the model to change its state. Your status pane’s PropertyChangeListener will then respond to the model’s state change and update its own view. It’s the cleanest most OOPs way to solve this problem.
For this to work well, your program should be based on one of the variants of the model-view-controller pattern, but then it should be doing this anyway.