I am currently writing a program which is having a lot of code duplication between the GUI aspect and the data storage aspect, and I was wondering if my proposed solution follows acceptable design principles.
Basically, I have some Data objects, which have a List holding some more information. Then the GUI displays the object, and a JList which displays all the information in the Data object's List. In the GUI, users can add or remove information to the object's list, and this is visually reflected in the JList.
However, this results in me having to maintain the GUI JList and the object List separately:
private void addInformation(String s) {
this.listmodel.addElement(s);
this.dataObject.getList().add(s);
}
(Obviously that is a very simple example but you get the idea). It also means that I'm using up twice as much memory holding both lists. Would it be acceptable for me to create a class which implemented both the ListModel and the List interfaces and then only need to update the single combined list/listmodel? I'm still studying at uni and I want to make this project the best I possibly can in terms of maintainability by others and adherence to best practices.
JList is part of Java Swing package . JList is a component that displays a set of Objects and allows the user to select one or more items . JList inherits JComponent class. JList is a easy way to display an array of Vectors .
public interface ListModel<E> This interface defines the methods components like JList use to get the value of each cell in a list and the length of the list. Logically the model is a vector, indices vary from 0 to ListDataModel.
Absolutely yes. Legacy swing applications are still supported and enhanced. There is no alternative for that. And if you are making applications like IDE's, SWING is still preferred.
Your GUI should not hold information about the state of the data model. It would be better to separate it up into a Model-View-Controller (MVC) design. Where the controller is responsible for object creation and updating the model and view accordingly. Your actual view should just be responsible for screen display by passing arguments to it to display. This way the view and model don't need to know anything about each other and there will only be one data model.
If your having to keep both lists consistent then something is wrong with your design and will probably introduce more bugs into the code.
See here for MVC details.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With