Observer Pattern on MVC for specific fields - java

On the MVC pattern, which is the best option for the Model to notify the View (if this is the right approach in the first place) where, from all the fields of data the Model is storing, only a couple of them are updated. Specifically when we only want to update specific fields of the View.
I am currently using a MVC pattern with Observer/Subscriber (JAVA Swing) as described here: https://stackoverflow.com/a/6963529 but when the Model updates, it changes everything in the View when the update() funcion is called, it's impossible to determine which field from the Model changed in order to update only the required field in the View.
I read this topic: https://softwareengineering.stackexchange.com/a/359008 and this as well: https://stackoverflow.com/a/9815189 which I think it's usefull, but for the later, I can't understand very well how can I set a propertyChangeListener on a variale (int, float, etc). Also related to this: https://stackoverflow.com/a/9815189
The Main class where the software start to run:
public class Main {
public static void main(String[] args) {
Model m = new Model();
View v = new View(m);
Controller c = new Controller(m, v);
c.initController();
}
}
So the code that I have on Model is this:
public class Model extends Observable {
//...
private float speed;
private int batteryPercentage;
public float getSpeed() {
return speed;
}
public void setSpeed(float speed) {
this.speed = speed;
setChanged();
notifyObservers();
}
public int getBatteryPercentage() {
return batteryPercentage;
}
public void setBatteryPercentage(int batteryPercentage) {
this.batteryPercentage = batteryPercentage;
setChanged();
notifyObservers();
}
}
The view knows the Model:
public class View implements Observer {
private Model model;
private JTextField txtFldSpeed;
private JTextField txtFldBattery;
private JFrame mainWindow;
public View(Model m) {
this.model = m;
initialize();
}
private void initialize() {
mainWindow = new JFrame();
mainWindow.setTitle("New Window");
mainWindow.setMinimumSize(new Dimension(1280, 720));
mainWindow.setBounds(100, 100, 1280, 720);
mainWindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel tPanel1 = new JPanel();
tPanel1.setBorder(new LineBorder(new Color(0, 0, 0)));
tPanel1.setLayout(null);
mainWindow.getContentPane().add(tPanel1);
mainWindow.getContentPane().add(tPanel1);
txtFldSpeed = new JTextField();
txtFldSpeed.setEditable(false);
txtFldSpeed.setBounds(182, 11, 116, 22);
tPanel1.add(txtFldSpeed);
txtFldBattery = new JTextField();
txtFldBattery.setEditable(false);
txtFldBattery.setBounds(182, 43, 116, 22);
tPanel1.add(txtFldBattery);
mainWindow.setVisible(true);
}
#Override
public void update(Observable o, Object arg) {
txtFldSpeed.setText(Float.toString(model.getSpeed()) + " kn");
txtFldBattery.setText(Integer.toString(model.getBatteryPercentage()) + " %");
}
}
The Controller adds the View as a Observer of the Model:
public class Controller {
private Model model;
private View view;
public Controller(Model m, View v) {
this.model = m;
this.view = v;
}
public void initController() {
model.addObserver(view);
model.setSpeed(10);
}
}
What I am expecting is something that, when the Model is updated, let's say, function setSpeed() is called, the View is told that she needs to update itself on that specific field and not every "changable" field (like the txtFldBattery.
I want to do this because on the View, there are fields being updated a couple of times per second, and because I need to update everything on the view, a JComboBox which doesn't need to update that often, keeps closing when trying to select a option.

I would use SwingPropertyChangeSupport, make each of the model's state fields a "bound property" so that each state field can be listened to separately.
For instance, say you have a model that looked like this:
public class MvcModel {
public static final String SPEED = "speed";
public static final String BATTERY = "battery";
public static final int MAX_SPEED = 40;
private float speed;
private int batteryPercentage;
private SwingPropertyChangeSupport pcSupport = new SwingPropertyChangeSupport(this);
public float getSpeed() {
return speed;
}
public void setSpeed(float speed) {
float oldValue = this.speed;
float newValue = speed;
this.speed = speed;
pcSupport.firePropertyChange(SPEED, oldValue, newValue);
}
public int getBatteryPercentage() {
return batteryPercentage;
}
public void setBatteryPercentage(int batteryPercentage) {
int oldValue = this.batteryPercentage;
int newValue = batteryPercentage;
this.batteryPercentage = batteryPercentage;
pcSupport.firePropertyChange(BATTERY, oldValue, newValue);
}
public void addPropertyChangeListener(PropertyChangeListener listener) {
pcSupport.addPropertyChangeListener(listener);
}
public void removePropertyChangeListener(PropertyChangeListener listener) {
pcSupport.removePropertyChangeListener(listener);
}
public void addPropertyChangeListener(String name, PropertyChangeListener listener) {
pcSupport.addPropertyChangeListener(name, listener);
}
public void removePropertyChangeListener(String name, PropertyChangeListener listener) {
pcSupport.removePropertyChangeListener(name, listener);
}
}
Both the speed and the batteryPercent fields are "bound fields" in that any changes to these fields will trigger the property change support object to fire a notification message to any listeners that have registered with the support object, as reflected in the public void setXxxx(...) methods.
This way the controller could register listeners on the model for whatever properties it wants to listen to, and then notify the view of any changes. For example:
class SpeedListener implements PropertyChangeListener {
#Override
public void propertyChange(PropertyChangeEvent evt) {
float speed = model.getSpeed();
view.setSpeed(speed);
}
}
The set up could look something like:
import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.GridLayout;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import javax.swing.*;
import javax.swing.event.SwingPropertyChangeSupport;
public class MVC2 {
private static void createAndShowGui() {
MvcModel model = new MvcModel();
MvcView view = new MvcView();
MvcController controller = new MvcController(model, view);
controller.init();
JFrame frame = new JFrame("MVC2");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(view.getMainDisplay());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(() -> createAndShowGui());
}
}
class MvcView {
private JPanel mainPanel = new JPanel();
private JSlider speedSlider = new JSlider(0, MvcModel.MAX_SPEED);
private JSlider batterySlider = new JSlider(0, 100);
private JProgressBar speedBar = new JProgressBar(0, MvcModel.MAX_SPEED);
private JProgressBar batteryPercentBar = new JProgressBar(0, 100);
public MvcView() {
speedSlider.setMajorTickSpacing(5);
speedSlider.setMinorTickSpacing(1);
speedSlider.setPaintTicks(true);
speedSlider.setPaintLabels(true);
speedSlider.setPaintTrack(true);
batterySlider.setMajorTickSpacing(20);
batterySlider.setMinorTickSpacing(5);
batterySlider.setPaintTicks(true);
batterySlider.setPaintLabels(true);
batterySlider.setPaintTrack(true);
speedBar.setStringPainted(true);
batteryPercentBar.setStringPainted(true);
JPanel inputPanel = new JPanel(new GridLayout(0, 1));
inputPanel.add(createTitledPanel("Speed", speedSlider));
inputPanel.add(createTitledPanel("Battery %", batterySlider));
JPanel displayPanel = new JPanel(new GridLayout(0, 1));
displayPanel.add(createTitledPanel("Speed", speedBar));
displayPanel.add(createTitledPanel("Battery %", batteryPercentBar));
mainPanel.setLayout(new GridLayout(1, 0));
mainPanel.add(createTitledPanel("Input", inputPanel));
mainPanel.add(createTitledPanel("Display", displayPanel));
}
private JComponent createTitledPanel(String title, JComponent component) {
JPanel titledPanel = new JPanel(new BorderLayout());
titledPanel.setBorder(BorderFactory.createTitledBorder(title));
titledPanel.add(component);
return titledPanel;
}
public JComponent getMainDisplay() {
return mainPanel;
}
public void setSpeed(float speed) {
speedBar.setValue((int) speed);
}
public void setBatteryPercent(int batteryPercent) {
batteryPercentBar.setValue(batteryPercent);
}
public JSlider getSpeedSlider() {
return speedSlider;
}
public JSlider getBatterySlider() {
return batterySlider;
}
}
class MvcController {
private MvcModel model;
private MvcView view;
public MvcController(MvcModel model, MvcView view) {
this.model = model;
this.view = view;
model.addPropertyChangeListener(MvcModel.BATTERY, new BatteryListener());
model.addPropertyChangeListener(MvcModel.SPEED, new SpeedListener());
view.getSpeedSlider().addChangeListener(chngEvent -> {
int value = view.getSpeedSlider().getValue();
model.setSpeed(value);
});
view.getBatterySlider().addChangeListener(chngEvent -> {
int value = view.getBatterySlider().getValue();
model.setBatteryPercentage(value);
});
}
public void init() {
view.getSpeedSlider().setValue(10);
view.getBatterySlider().setValue(100);
model.setSpeed(10);
model.setBatteryPercentage(100);
}
class SpeedListener implements PropertyChangeListener {
#Override
public void propertyChange(PropertyChangeEvent evt) {
float speed = model.getSpeed();
view.setSpeed(speed);
}
}
class BatteryListener implements PropertyChangeListener {
#Override
public void propertyChange(PropertyChangeEvent evt) {
int batteryPercent = model.getBatteryPercentage();
view.setBatteryPercent(batteryPercent);
}
}
}
Side note: Observer and Observable have been deprecated in the most recent version of Java and so should their use should probably be avoided.

In your update method implementation you can determine with first argument o which Observable has changed and with second argument arg which value changed when you call: notifyObservers(this.speed);
Note that notifyObservers's signature accepts Object, and float primitive is not a subclass of Object.

Related

Using PropertyChangeListener to refresh JFrame (without triggering infinite loop)

Using java, I have a JFrame containing several JPanels which include various JComboBoxes, JTextFields, etc... which connect to entries in xml files, organized and viewed by date. Everything syncs up and is working but I've been having trouble getting the JFrame to update/refresh when changes are being made to the entries (i.e. adding/removing etc), although it does refresh when I change the date. I've got to the point where I have a PropertyChangeListener that gets triggered (prints to console) but when I try to use that Listener to refresh the frame I think I'm only revalidating the listener?
Here's the code from the JFrame (I've used comments to indicate the failed segments):
package interfaceComponents;
import java.beans.*;
import javax.swing.*;
import org.jdesktop.swingx.JXDatePicker;
import java.awt.*;
import java.awt.event.*;
import java.text.*;
import java.io.IOException;
import java.util.*;
import java.time.*;
import java.time.format.*;
public class DailyView extends Frame {
private static final long serialVersionUID = 7827570917642254745L;
private final JXDatePicker calendar = new JXDatePicker();
private JLabel focusPoint;
public DailyView(LocalDate d) throws IOException {
DefaultDateModel model = new DefaultDateModel(d);
OperatorMenus menus = new OperatorMenus();
setJMenuBar(menus);
JPanel body = new JPanel();
body.setLayout(new BoxLayout(body, BoxLayout.Y_AXIS));
DayView anchorDay = new DayView(0);
anchorDay.setModel(model);
DayView nextDay = new DayView(1);
nextDay.setModel(model);
body.addPropertyChangeListener(new PropertyChangeListener() {
public void propertyChange(PropertyChangeEvent evt) {
System.out.println("OperatorView.propertyChange");
//***This is where I'm trying to TRIGGER the REFRESH***
refreshFrame(); }
});
body.add(anchorDay);
body.add(nextDay);
add(new JScrollPane(body), BorderLayout.CENTER);
JPanel footer = new JPanel();
NavButtons navPanel = new NavButtons(model);
focusPoint = new JLabel(DateTimeFormatter.ofPattern("E, dd MMM yyyy").format(model.getDate()));
focusPoint.setForeground(Color.RED);
footer.setLayout(new BorderLayout());
footer.add(focusPoint, BorderLayout.CENTER);
footer.add(navPanel, BorderLayout.EAST);
footer.setBackground(Color.BLACK);
add(footer, BorderLayout.SOUTH);
pack(); }
public DailyView() throws IOException { this(LocalDate.now()); }
//interfaces
public interface DateModel {
public LocalDate getDate();
public void addObserver(Observer o);
public void removeObserver(Observer o); }
public interface MutableDateModel extends DateModel {
public void setDate(LocalDate date); }
//methods
public void refreshFrame() { //***This is where I'm trying to TRIGGER the REFRESH***
this.revalidate();
this.repaint(); }
//inner classes
public class DefaultDateModel extends Observable implements MutableDateModel {
private LocalDate date;
public DefaultDateModel(LocalDate d) { date = d; }
#Override
public void setDate(LocalDate d) {
date = d;
setChanged();
notifyObservers(); }
#Override
public LocalDate getDate() {
return date; }
#Override
public void removeObserver(Observer o) {
deleteObserver(o); }
}
public class ShiftFocus extends AbstractAction implements Observer {
private static final long serialVersionUID = 680383526965967229L;
private MutableDateModel model;
private int shift;
public ShiftFocus(MutableDateModel m, int i) {
setModel(m);
shift = i; }
public void actionPerformed(ActionEvent event) {
MutableDateModel model = getModel();
if (model != null) {
model.setDate(model.getDate().plusDays(shift));
calendar.setDate(Date.from(model.getDate().atStartOfDay().atZone(ZoneId.systemDefault()).toInstant()));
pack(); }
}
public void setModel(MutableDateModel value) {
if (model != null) {
model.removeObserver(this); }
this.model = value;
if (model != null) {
model.addObserver(this); }
}
public MutableDateModel getModel() {
return model; }
#Override
public void update(Observable o, Object arg) {
focusPoint.setText(DateTimeFormatter.ofPattern("E, dd MMM yyyy").format(model.getDate())); }
}
class NavButtons extends JPanel implements Observer {
private static final long serialVersionUID = 914087518688373731L;
//instance variables
private JToolBar toolBar = new JToolBar("Navigation");
private JButton weekBack = new JButton("<<");
private JButton dayBack = new JButton("<");
private JButton returnToday = new JButton("Today");
private JButton nextDay = new JButton(">");
private JButton nextWeek = new JButton(">>");
private MutableDateModel model;
//constructor
public NavButtons(MutableDateModel model) {
weekBack.addActionListener(new ShiftFocus(model, -7));
dayBack.addActionListener(new ShiftFocus(model, -1));
returnToday.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event) {
MutableDateModel m = getModel();
m.setDate(LocalDate.now());
setModel(m);
pack(); }
});
nextDay.addActionListener(new ShiftFocus(model, 1));
nextWeek.addActionListener(new ShiftFocus(model, 7));
toolBar.add(weekBack);
toolBar.add(dayBack);
toolBar.add(returnToday);
toolBar.add(nextDay);
toolBar.add(nextWeek);
calendar.setEditable(true);
calendar.setFormats("E, dd MMM yyyy");
calendar.setDate(Date.from(model.getDate().atStartOfDay().atZone(ZoneId.systemDefault()).toInstant()));
calendar.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event) {
MutableDateModel model = getModel();
if (model != null) {
model.setDate(LocalDate.parse(new SimpleDateFormat("yyyy-MM-dd").format(calendar.getDate())));
pack(); }
}
});
toolBar.add(calendar);
toolBar.add(new GalileoMode());
add(toolBar);
setModel(model); }
public void setModel (MutableDateModel value) {
if (model != null) {
model.removeObserver(this); }
this.model = value;
if (model != null) {
model.addObserver(this); }
}
public MutableDateModel getModel() {
return model; }
#Override
public void update(Observable o, Object arg) {/* models data changes */}
}
}
EDIT
After some feedback, have tried this so far:
thisFrame = this;
body.addPropertyChangeListener(new PropertyChangeListener() {
public void propertyChange(PropertyChangeEvent evt) {
System.out.println("OperatorView.propertyChange");
// thisFrame.removeAll();
// thisFrame.dispose();
// thisFrame.remove(body);
// thisFrame.add(new JScrollPane(body), BorderLayout.CENTER);
thisFrame.getContentPane().validate();
// thisFrame.revalidate();
thisFrame.getContentPane().repaint();
// thisFrame.refreshFrame();
}
});
with DailyView thisFrame; declared as an instance variable at the top.
EDIT
In case there's anyone out there reading this, I am getting the propertyChangeListener to trigger certain events that would refresh the underlying panels, but that causes an infinite loop as the propertyChangeListener gets called again. (re)validate() and repaint() don't seem to have this issue but they're not refreshing the panels' content...anyone who could point me in the right direction/link to a similar question, etc would be greatly appreciated.
When you use the 'this' pointer, it always refers to the class you are in. I believe your problem is coming because the 'this' pointer does refer to your listener and not the JPanel. Perhaps your problem can be solved by
Frame thisFrame = this;
body.addPropertyChangeListener(new PropertyChangeListener() {
public void propertyChange(PropertyChangeEvent evt) {
System.out.println("OperatorView.propertyChange");
//***This is where I'm trying to TRIGGER the REFRESH***
thisFrame.refreshFrame(); }
});

Windows alignment in SWING-based GUI

My goal is to display several views of one object. For each view I create a thread. Also, I have a class which controls those views, e.g. send a command to align them. However, it is not always I get correct alignment. So there is a data races, and I cannot understand what I am doing wrong.
Here there is a piece of code showing the problem I have. It has a simple idea: create a main view window, and then align the second window of the same size near its right border.
First, I have an abstract class to create a thread:
public abstract class ViewWindow implements Runnable{
private Thread thread;
private boolean terminate = false ;
private Controller controller;
private UpdateTask currentUpdateTask = null;
private class UpdateTask {
boolean alignWindows = true;
}
public ViewWindow(Controller controller, String title) {
this.title = title;
this.controller = controller;
}
public void startThread() {
thread = new Thread(this);
thread.start();
}
#Override
public void run() {
UpdateTask updateTask = null;
synchronized (thread) {
while (terminate == false) {
try {
thread.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
updateTask = currentUpdateTask;
currentUpdateTask = null;
if(updateTask.alignWindows) {
controller.getLock().lock();
setLocationRelativeTo(controller.getMainWindow());
controller.getLock().unlock();
}
}
}
}
public void alignWindowsUsingThread() {
synchronized (thread) {
currentUpdateTask = new UpdateTask();
thread.notify();
}
}
public abstract void setLocationRelativeTo(ImageViewWindow imageWindow);
}
Then I extend it to create an abstraction for the window views:
public abstract class ImageViewWindow extends ViewWindow {
private JFrame frame;
public ImageViewWindow(Controller controller, String title) {
super(controller, title);
frame = new JFrame(title);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JLabel label = new JLabel(title);
frame.getContentPane().add(label, BorderLayout.CENTER);
frame.setPreferredSize(new Dimension(300,500));
frame.pack();
frame.setVisible(true);
}
public JFrame getFrame() {
return frame;
}
synchronized public void setLocation(int x, int y) {
frame.setLocation(x, y);
}
synchronized public Point getLocation() {
return frame.getLocation();
}
}
Finally, I override a function to set relative location for each window:
public class FirstWindow extends ImageViewWindow {
public FirstWindow(Controller controller, String title) {
super(controller, title);
this.setLocation(50, 50);
this.startThread();
}
#Override
public void setLocationRelativeTo(ImageViewWindow imageWindow) { }
}
public class SecondWindow extends ImageViewWindow {
public SecondWindow(Controller controller, String title) {
super(controller, title);
this.startThread();
}
#Override
public void setLocationRelativeTo(ImageViewWindow imageWindow) {
Point location = imageWindow.getLocation();
int xOffSet = imageWindow.getFrame().getWidth();
int yOffSet = 0;
this.setLocation(xOffSet + location.x, yOffSet + location.y);
}
}
Here there is a class which is responsible for the control:
public class Controller {
private Lock controlLock;
private List<ImageViewWindow> windows = new ArrayList<ImageViewWindow>();
private ImageViewWindow mainWindow;
public Controller() {
controlLock = new ReentrantLock();
}
public ImageViewWindow getMainWindow() {
return mainWindow;
}
public Lock getLock() {
return controlLock;
}
public void addMainWindow(ImageViewWindow mainViewWindow) {
this.mainWindow = mainViewWindow;
this.addWindow(mainViewWindow);
}
public void addWindow(ImageViewWindow imageWindow) {
windows.add(imageWindow);
}
public void updateWindowPositions() {
for(ImageViewWindow window : windows) {
window.alignWindowsUsingThread();
}
}
}
And do run everything:
public class Start {
public static void main(String[] args) {
Controller controller = new Controller();
ImageViewWindow window1 = new FirstWindow(controller, "FirstWindow");
controller.addMainWindow(window1);
ImageViewWindow window2 = new SecondWindow(controller, "SecondWindow");
controller.addWindow(window2);
controller.updateWindowPositions();
}
}
UPD: I updated the code based on the answer below, but the problem still remains!
You don’t specify how you want to “align” your windows but from your code
public void setLocationRelativeTo(ImageViewWindow imageWindow) {
Point location = imageWindow.getLocation();
int xOffSet = this.getFrame().getWidth();
int yOffSet = 0;
this.setLocation(xOffSet + location.x, yOffSet + location.y);
}
I suppose you want this to be placed to the right of imageWindow. In this case you have to use imageWindow.x + imageWindow.width rather than imageWindow.x + this.width:
| imageWindow | this
x ← width → x ← width →
↳=imageWindow.x+imageWindow.width
So the correct method would be:
public void setLocationRelativeTo(ImageViewWindow imageWindow) {
Point location = imageWindow.getLocation();
int xOffSet = imageWindow.getFrame().getWidth();
int yOffSet = 0;
this.setLocation(xOffSet + location.x, yOffSet + location.y);
}
By the way, I don’t get why you are making such a simple task that complicated and even multi-threaded. There’s no benefit from multi-threading here, only a complication that obstructs the view on the simplest things…

Modifying independent JPanels from the JFrame

I've got a JFrame with two separate JPanels. One of the JPanels is filled with JButtons while the other has a couple of text fields. I added mouse listeners to the buttons via the JFrame and I want to make it so that when an event is fired from the first JPanel, the text fields in the second JPanel are changed. The two panels have their own classes. How would I go about doing this?
Use MVC, Model-View-Control, separation of concerns.
Have the Control, which holds your listeners, change the state of the model.
The Views -- your GUI's, have listeners added to them by the control, so that user input is transmitted to the control and thereby to the model.
The View can also either directly add listeners to the model so that they can change their display if the model changes, or often this is done indirectly through the control.
Don't add MouseListeners to JButtons. Use ActionListeners as that's what they're for. For example, if you disable a JButton, any ActionListeners attached to it won't work -- which is correct behavior. The same is not true for MouseListeners.
For more specific help, consider creating and posting a minimal example program.
Edit
For example:
import java.awt.GridLayout;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import javax.swing.AbstractAction;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
import javax.swing.event.SwingPropertyChangeSupport;
public class MvcMain {
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
MvcView view = new MvcView();
MvcModel model = new MvcModel();
MvcControl control = new MvcControl(view, model);
view.createAndDisplay();
}
});
}
}
class MvcView {
private MvcModel model;
private ButtonPanel buttonPanel = new ButtonPanel();
private TextFieldPanel textFieldPanel = new TextFieldPanel();
private JPanel mainPanel = new JPanel();
public MvcModel getModel() {
return model;
}
public ButtonPanel getButtonPanel() {
return buttonPanel;
}
public TextFieldPanel getTextFieldPanel() {
return textFieldPanel;
}
public MvcView() {
mainPanel.setLayout(new GridLayout(0, 1));
mainPanel.add(textFieldPanel);
mainPanel.add(buttonPanel);
}
public void setModel(MvcModel model) {
this.model = model;
model.addPropertyChangeListener(new ModelListener());
}
public void createAndDisplay() {
JFrame frame = new JFrame("MVC Test");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(mainPanel);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
private class ModelListener implements PropertyChangeListener {
#Override
public void propertyChange(PropertyChangeEvent evt) {
if (ButtonTitle.class.getCanonicalName().equals(evt.getPropertyName())) {
ButtonTitle newValue = model.getButtonTitle();
textFieldPanel.textFieldSetText(newValue.getTitle());
}
}
}
}
enum ButtonTitle {
START("Start"), STOP("Stop"), PAUSE("Pause");
private String title;
private ButtonTitle(String title) {
this.title = title;
}
public String getTitle() {
return title;
}
}
#SuppressWarnings("serial")
class ButtonPanel extends JPanel {
public ButtonPanel() {
setBorder(BorderFactory.createTitledBorder("Button Panel"));
setLayout(new GridLayout(1, 0, 5, 0));
for (ButtonTitle btnTitle : ButtonTitle.values()) {
add(new JButton(new ButtonAction(btnTitle)));
}
}
private class ButtonAction extends AbstractAction {
private ButtonTitle btnTitle;
public ButtonAction(ButtonTitle btnTitle) {
super(btnTitle.getTitle());
this.btnTitle = btnTitle;
}
public void actionPerformed(java.awt.event.ActionEvent e) {
Object oldValue = null;
ButtonTitle newValue = btnTitle;
ButtonPanel.this.firePropertyChange(
ButtonTitle.class.getCanonicalName(), oldValue, newValue);
};
}
}
#SuppressWarnings("serial")
class TextFieldPanel extends JPanel {
private JTextField textField = new JTextField(15);
public TextFieldPanel() {
setBorder(BorderFactory.createTitledBorder("TextField Panel"));
add(textField);
}
public void textFieldSetText(String text) {
textField.setText(text);
}
}
class MvcControl {
private MvcView view;
private MvcModel model;
public MvcControl(MvcView view, MvcModel model) {
this.view = view;
this.model = model;
view.setModel(model);
view.getButtonPanel().addPropertyChangeListener(new ButtonPanelListener());
}
private class ButtonPanelListener implements PropertyChangeListener {
#Override
public void propertyChange(PropertyChangeEvent evt) {
if (ButtonTitle.class.getCanonicalName().equals(evt.getPropertyName())) {
ButtonTitle newValue = (ButtonTitle) evt.getNewValue();
model.setButtonTitle(newValue);
}
}
}
}
class MvcModel {
private ButtonTitle buttonTitle;
private SwingPropertyChangeSupport pcSupport = new SwingPropertyChangeSupport(
this);
public void addPropertyChangeListener(PropertyChangeListener listener) {
pcSupport.addPropertyChangeListener(listener);
}
public void removePropertyChangeListener(PropertyChangeListener listener) {
pcSupport.removePropertyChangeListener(listener);
}
public ButtonTitle getButtonTitle() {
return buttonTitle;
}
public void setButtonTitle(ButtonTitle buttonTitle) {
ButtonTitle oldValue = this.buttonTitle;
ButtonTitle newValue = buttonTitle;
this.buttonTitle = buttonTitle;
pcSupport.firePropertyChange(ButtonTitle.class.getCanonicalName(),
oldValue, newValue);
}
}
The example is lacking in use of interfaces which would allow for a further separation of concerns resulting in looser coupling (a good thing).

Clearing a Sudoku table

I'm working on this piece of code which can be found at
http://pastebin.com/7bCFtUHL
Basically, I want to add a clear method (button) which clears the sudoku after having it solved.
I've tried making a loop that goes through every cell and puts it to null but I'm not completely sure how to connect it exactly. Nor am I sure in which class I'd have to create it so it can be connected to the GUI where I have the other button.
EDIT:
This is the clear method I currently got
public void clearCells(){
for (int y = 0; y < 9; y++) {
for (int x = 0; x < 9; x++) {
cells[y][x] = null;
cells[y][x].setText("");
}
}
}
Now I need to attach that to the JButton in another class, how would that be possible?
My clear button looks like this
JButton clear = new JButton("Clear");
clear.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e)
{
//Code
}
}
);
What code would I need to add in the actionPerformed method to connect it with my clearCells method?
Again, I would put the "meat" of the clear method in the model itself. The general form of a solution would be to do this:
clear.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
myModel.clearCells();
}
});
Where the Model class would have a public void clearCells() method that iterates through the cells and clears them.
Edit 1
Note: yeah I did look at your pastebin code link and one big problem I see is that your SwingSudokuBoard class extends the SudokuBoard class, and this is misuse of inheritance where you should be using composition instead. The SwingSudokuBoard class should hold an instance of a SudokuBoard object and call methods on it.
Edit 2
You ask:
I'm not sure that I can completely understand you. You want me to have the clear method in the same class as I got the button, but then I cant call the cells. I added x.clearCells(); while x being what? My main class like, SwingSudokuBoard.clearCells(); ? Eitherway, if I add what you say the program complaints that it want the clearCells method and cells to be static. But if I put them to static, I get a NullPointerException.
I think that you need to use the Model-View-Control (MVC) pattern or an abbreviated version of it, perhaps one where you combine the view with the control since your program is small. I suggest that you have a separate model class, here this would likely be the SudokuBoard class, and then a view class, here probably the SwingSudokuBoard class. Your view's control methods (the ActionListeners) would call the model's clearCells() method. And don't use static anything here.
Edit 3
You ask:
I assume something along with these lines. Model: SudokuBoard; View: SwingSudokuBoard; Control: SwingSudoKiller. How would that go about? I'd have the actionListener posted above in the control. How would the other classes look like? Since I assume the clear method lays in the Model which you want to be in SudokuBoard but it cant connect with the cells there.
I'm not a professional, nor have I received formal programming training, so theory is one of my weak points, but my interpretation of MVC is that the view listens to the model and updates itself when the model notifies it of changes and that the control listens to the view and responds to view changes by notifying the model. This precise pattern has variations and does not need to be followed exactly to the letter, but the key in all of this is to separate out in your code the separate concerns as much as possible so that "coupling" (the number of direct connections between classes) is low or "loose" and "cohesion" (code that deals with the same concerns) is high or "tight".
In your program, again I'd combine the view and control by using anonymous inner listeners just as you're doing. I'd have the view/control, which is the SwingSudokuBoard class, hold an instance of the SudokuBoard class as a class field, and have the view/control's anonymous listeners call methods on the SudokuBoard field. When I've done this sort of thing before, I've given the model support for being observed by giving it a SwingPropertyChangeSupport object as well as public addPropertyChangeListener(...) and removePropertyChangeListener(...) methods. Then the view could respond easily to changes in the model.
You state:
Since I assume the clear method lays in the Model which you want to be in SudokuBoard but it cant connect with the cells there.
I'm not sure what you mean by this. The model holds the cells. Perhaps you don't mean the logical cells held by the model but rather the displayed cells held by the view. The view would add a listener to the model, and when notified of changes to the model, would ask the model for its data and use that to update the visualized cells.
Edit 4
For example:
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import javax.swing.*;
import javax.swing.event.SwingPropertyChangeSupport;
public class OverlySimpleModelView {
private static void createAndShowGui() {
Model model = new Model();
ViewControl viewControl = new ViewControl(model);
JFrame frame = new JFrame("OverlySimpleModelView");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(viewControl.getMainComponent());
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGui();
}
});
}
}
class ViewControl {
private JPanel mainPanel = new JPanel();
private JTextField number1Field = new JTextField(5);
private JTextField number2Field = new JTextField(5);
private JTextField productField = new JTextField(5);
private Model model;
public ViewControl(Model model) {
this.model = model;
model.addPropertyChangeListener(new MyPropChngListener());
productField.setEditable(false);
productField.setFocusable(false);
mainPanel.add(number1Field);
mainPanel.add(new JLabel(" * "));
mainPanel.add(number2Field);
mainPanel.add(new JLabel(" = "));
mainPanel.add(productField);
CalculateAction calculateAction = new CalculateAction("Calculate", KeyEvent.VK_C);
mainPanel.add(new JButton(calculateAction));
number1Field.addActionListener(calculateAction);
number2Field.addActionListener(calculateAction);
mainPanel.add(new JButton(new ClearAction("Clear", KeyEvent.VK_L)));
}
public JComponent getMainComponent() {
return mainPanel;
}
private class MyPropChngListener implements PropertyChangeListener {
#Override
public void propertyChange(PropertyChangeEvent evt) {
number1Field.setText(String.valueOf(model.getNumber1()));
number2Field.setText(String.valueOf(model.getNumber2()));
productField.setText(String.valueOf(model.calculateProduct()));
}
}
private class CalculateAction extends AbstractAction {
public CalculateAction(String text, int keyCode) {
super(text);
putValue(MNEMONIC_KEY, keyCode);
}
#Override
public void actionPerformed(ActionEvent evt) {
try {
double number1 = Double.parseDouble(number1Field.getText());
double number2 = Double.parseDouble(number2Field.getText());
model.setNumber1(number1);
model.setNumber2(number2);
} catch (NumberFormatException e) {
e.printStackTrace();
}
}
}
private class ClearAction extends AbstractAction {
public ClearAction(String text, int keyCode) {
super(text);
putValue(MNEMONIC_KEY, keyCode); // to allow buttons a mnemonic letter
}
#Override
public void actionPerformed(ActionEvent evt) {
model.clear();
}
}
}
class Model {
public static final String NUMBERS_CHANGED = "numbers changed";
private double number1 = 0.0;
private double number2 = 0.0;
private SwingPropertyChangeSupport propChngSupport =
new SwingPropertyChangeSupport(this);
public double getNumber1() {
return number1;
}
public double getNumber2() {
return number2;
}
public void clear() {
setNumber1(0.0);
setNumber2(0.0);
}
// make number1 field a "bound" property, one that notifies listeners if it is changed.
public void setNumber1(double number1) {
Double oldValue = this.number1;
Double newValue = number1;
this.number1 = number1;
propChngSupport.firePropertyChange(NUMBERS_CHANGED, oldValue , newValue);
}
// ditto for the number2 field
public void setNumber2(double number2) {
Double oldValue = this.number2;
Double newValue = number2;
this.number2 = number2;
propChngSupport.firePropertyChange(NUMBERS_CHANGED, oldValue , newValue);
}
public double calculateProduct() {
return number1 * number2;
}
public void addPropertyChangeListener(PropertyChangeListener listener) {
propChngSupport.addPropertyChangeListener(listener);
}
public void removePropertyChangeListener(PropertyChangeListener listener) {
propChngSupport.removePropertyChangeListener(listener);
}
}
Or maybe better since it uses an array of numbers:
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import javax.swing.*;
import javax.swing.event.SwingPropertyChangeSupport;
public class OverlySimpleModelView {
private static void createAndShowGui() {
Model model = new Model(5);
ViewControl viewControl = new ViewControl(model);
JFrame frame = new JFrame("OverlySimpleModelView");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(viewControl.getMainComponent());
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGui();
}
});
}
}
class ViewControl {
private JPanel mainPanel = new JPanel();
private JTextField[] numberFields;
private JTextField productField = new JTextField(5);
private Model model;
public ViewControl(Model model) {
this.model = model;
model.addPropertyChangeListener(new MyPropChngListener());
productField.setEditable(false);
productField.setFocusable(false);
CalculateAction calculateAction = new CalculateAction("Calculate", KeyEvent.VK_C);
numberFields = new JTextField[model.getNumberFieldsLength()];
for (int i = 0; i < numberFields.length; i++) {
numberFields[i] = new JTextField("0.0", 5);
mainPanel.add(numberFields[i]);
numberFields[i].addActionListener(calculateAction);
if (i < numberFields.length - 1) {
mainPanel.add(new JLabel(" + "));
} else {
mainPanel.add(new JLabel(" = "));
}
}
mainPanel.add(productField);
mainPanel.add(new JButton(calculateAction));
mainPanel.add(new JButton(new ClearAction("Clear", KeyEvent.VK_L)));
}
public JComponent getMainComponent() {
return mainPanel;
}
private class MyPropChngListener implements PropertyChangeListener {
#Override
public void propertyChange(PropertyChangeEvent evt) {
for (int i = 0; i < numberFields.length; i++) {
numberFields[i].setText(String.valueOf(model.getNumber(i)));
}
productField.setText(String.valueOf(model.calculateSum()));
}
}
private class CalculateAction extends AbstractAction {
public CalculateAction(String text, int keyCode) {
super(text);
putValue(MNEMONIC_KEY, keyCode);
}
#Override
public void actionPerformed(ActionEvent evt) {
try {
double[] numbers = new double[numberFields.length];
for (int i = 0; i < numbers.length; i++) {
numbers[i] = Double.parseDouble(numberFields[i].getText());
}
model.setNumbers(numbers);
} catch (NumberFormatException e) {
e.printStackTrace();
}
}
}
private class ClearAction extends AbstractAction {
public ClearAction(String text, int keyCode) {
super(text);
putValue(MNEMONIC_KEY, keyCode); // to allow buttons a mnemonic letter
}
#Override
public void actionPerformed(ActionEvent evt) {
model.clear();
}
}
}
class Model {
public static final String NUMBERS_CHANGED = "numbers changed";
private double[] numbers;
private SwingPropertyChangeSupport propChngSupport =
new SwingPropertyChangeSupport(this);
public Model(int length) {
numbers = new double[length];
}
public void setNumbers(double[] numbers) {
double[] oldValue = this.numbers;
double[] newValue = numbers;
this.numbers = numbers;
propChngSupport.firePropertyChange(NUMBERS_CHANGED, oldValue , newValue);
}
public double calculateSum() {
double sum = 0.0;
for (double number : numbers) {
sum += number;
}
return sum;
}
public double getNumber(int i) {
return numbers[i];
}
public int getNumberFieldsLength() {
return numbers.length;
}
public void clear() {
double[] newNumbers = new double[numbers.length];
setNumbers(newNumbers);
}
public void addPropertyChangeListener(PropertyChangeListener listener) {
propChngSupport.addPropertyChangeListener(listener);
}
public void removePropertyChangeListener(PropertyChangeListener listener) {
propChngSupport.removePropertyChangeListener(listener);
}
}

Is MVC in Swing Thread Safe

I'm trying to touch limits of MVC architecture in Swing, but as I tried everything all (from SwingWorker or Runnable#Thread) are done on EDT
my questions:
is there some limits or strictly depends by order of the implementations
(wrapped into SwingWorker or Runnable#Thread) ?
limited is if is JComponent#method Thread Safe or not ?
essential characteristic of an MVC architecture in Swing, ?
inc. Container Re-Layout ?
note: for my SSCCE I take one of great examples by HFOE, and maybe by holding this principes strictly isn't possible to create any EDT lack or GUI freeze
import java.awt.BorderLayout;
import java.awt.event.*;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.beans.PropertyChangeSupport;
import java.util.LinkedList;
import java.util.Queue;
import javax.swing.*;
public class MVC_ProgressBarThread {
private MVC_ProgressBarThread() {
MVC_View view = new MVC_View();
MVC_Model model = new MVC_Model();
MVC_Control control = new MVC_Control(view, model);
view.setControl(control);
JFrame frame = new JFrame("MVC_ProgressBarThread");
frame.getContentPane().add(view);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
public static void main(String[] args) {
java.awt.EventQueue.invokeLater(new Runnable() {
#Override
public void run() {
MVC_ProgressBarThread mVC_ProgressBarThread = new MVC_ProgressBarThread();
}
});
}
}
class MVC_View extends JPanel {
private static final long serialVersionUID = 1L;
private MVC_Control control;
private JProgressBar progressBar = new JProgressBar();
private JButton startActionButton = new JButton("Press Me and Run this Madness");
private JLabel myLabel = new JLabel("Nothing Special");
public MVC_View() {
startActionButton.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
buttonActionPerformed();
}
});
JPanel buttonPanel = new JPanel();
startActionButton.setFocusPainted(false);
buttonPanel.add(startActionButton);
setLayout(new BorderLayout(10, 10));
add(buttonPanel, BorderLayout.NORTH);
progressBar.setStringPainted(true);
add(progressBar, BorderLayout.CENTER);
myLabel.setIcon(UIManager.getIcon("OptionPane.questionIcon"));
myLabel.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
add(myLabel, BorderLayout.SOUTH);
}
public void setControl(MVC_Control control) {
this.control = control;
}
private void buttonActionPerformed() {
if (control != null) {
control.doButtonAction();
}
}
public void setProgress(int progress) {
progressBar.setValue(progress);
}
public void setProgressLabel(String label) {
progressBar.setString(label);
}
public void setIconLabel(Icon icon) {
myLabel.setIcon(icon);
}
public void start() {
startActionButton.setEnabled(false);
}
public void done() {
startActionButton.setEnabled(true);
setProgress(100);
setProgressLabel(" Done !!! ");
setIconLabel(null);
}
}
class MVC_Control {
private MVC_View view;
private MVC_Model model;
public MVC_Control(final MVC_View view, final MVC_Model model) {
this.view = view;
this.model = model;
model.addPropertyChangeListener(new PropertyChangeListener() {
#Override
public void propertyChange(PropertyChangeEvent pce) {
if (MVC_Model.PROGRESS.equals(pce.getPropertyName())) {
view.setProgress((Integer) pce.getNewValue());
}
if (MVC_Model.PROGRESS1.equals(pce.getPropertyName())) {
view.setProgressLabel((String) pce.getNewValue());
}
if (MVC_Model.PROGRESS2.equals(pce.getPropertyName())) {
view.setIconLabel((Icon) pce.getNewValue());
}
}
});
}
public void doButtonAction() {
view.start();
SwingWorker<Void, Void> swingworker = new SwingWorker<Void, Void>() {
#Override
protected Void doInBackground() throws Exception {
model.reset();
model.startSearch();
return null;
}
#Override
protected void done() {
view.done();
}
};
swingworker.execute();
}
}
class MVC_Model {
public static final String PROGRESS = "progress";
public static final String PROGRESS1 = "progress1";
public static final String PROGRESS2 = "progress2";
private static final int MAX = 11;
private static final long SLEEP_DELAY = 1000;
private int progress = 0;
private String label = "Start";
private PropertyChangeSupport pcs = new PropertyChangeSupport(this);
private PropertyChangeSupport pcs1 = new PropertyChangeSupport(this);
private PropertyChangeSupport pcs2 = new PropertyChangeSupport(this);
private final String[] petStrings = {"Bird", "Cat", "Dog",
"Rabbit", "Pig", "Fish", "Horse", "Cow", "Bee", "Skunk"};
private int index = 1;
private Queue<Icon> iconQueue = new LinkedList<Icon>();
private Icon icon = (UIManager.getIcon("OptionPane.questionIcon"));
public void setProgress(int progress) {
int oldProgress = this.progress;
this.progress = progress;
PropertyChangeEvent evt = new PropertyChangeEvent(this, PROGRESS,
oldProgress, progress);
pcs.firePropertyChange(evt);
}
public void setProgressLabel(String label) {
String oldString = this.label;
this.label = label;
PropertyChangeEvent evt = new PropertyChangeEvent(this, PROGRESS1,
oldString, label);
pcs1.firePropertyChange(evt);
}
public void setIconLabel(Icon icon) {
Icon oldIcon = this.icon;
this.icon = icon;
PropertyChangeEvent evt = new PropertyChangeEvent(this, PROGRESS2,
oldIcon, icon);
pcs2.firePropertyChange(evt);
}
public void reset() {
setProgress(0);
}
public void addPropertyChangeListener(PropertyChangeListener listener) {
pcs.addPropertyChangeListener(listener);
pcs1.addPropertyChangeListener(listener);
pcs2.addPropertyChangeListener(listener);
}
public void startSearch() {
iconQueue.add(UIManager.getIcon("OptionPane.errorIcon"));
iconQueue.add(UIManager.getIcon("OptionPane.informationIcon"));
iconQueue.add(UIManager.getIcon("OptionPane.warningIcon"));
iconQueue.add(UIManager.getIcon("OptionPane.questionIcon"));
for (int i = 0; i < MAX; i++) {
int newValue = (100 * i) / MAX;
setProgress(newValue);
setProgressLabel(petStrings[index]);
index = (index + 1) % petStrings.length;
setIconLabel(nextIcon());
try {
Thread.sleep(SLEEP_DELAY);
} catch (InterruptedException e) {
}
}
}
private Icon nextIcon() {
Icon icon1 = iconQueue.peek();
iconQueue.add(iconQueue.remove());
return icon1;
}
}
This is too long for a comment...
First and this is unrelated to the rest of this answer: there are many different MVCs out there and the one you used in that piece of code you posted here is not the same as the one used in the article you linked to: http://www.oracle.com/technetwork/articles/javase/mvc-136693.html
The article correctly points out that it's just "A common MVC implementation" (one where the view registers a listener listening to model changes). Your implementation is a different type of MVC, where the controller registers a listener listening to model changes and then updates the view.
Not that there's anything wrong with that: there are a lot of different types of MVCs out there (*).
(Another little caveat... Your view is aware of your controller in your example, which is a bit weird: there are other ways to do what you're doing without needing to "feed" the controller to the view like you do with your setControl(...) inside your MVCView.)
But anyway... You're basically nearly always modifying the GUI from outside the EDT (which you shouldn't be doing):
public void setIconLabel(final Icon icon) {
myLabel.setIcon(icon);
}
You can check it by adding this:
System.out.println("Are we on the EDT? " + SwingUtilities.isEventDispatchThread());
This is because you're eventually doing these updates from your SwingWorker thread (the SwingWorker thread is run outside the EDT: it's basically the point of a Swing worker).
I'd rather update the GUI from the EDT, doing something like this:
public void setIconLabel(final Icon icon) {
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
myLabel.setIcon(icon);
}
});
}

Categories