0

I'm just about to implement my first Java project and now at a point where I do not really get further.

The program is relatively simple. There is the possibility to create notes, which are displayed in a JList. You can change the notes, delete them and as a new feature, there should be a button to sort the notes within the JList ascending / descending by creation date.

The requirement for the implementation of the function, however, is the following: "According to the MVC principle I should not change the order of the objects inside my model, but only the entries in the JList". And that confuses me a little bit. I would have ordered the internal database with a Comperator or Decorate Pattern and then display the result, but that's not required. And now I am stuck. I always thought that the model is the basis for the view and that all the changes take place in the model.

As I said, I am a beginner and have searched even on Google and looked at several tutorials, but I lack the experience to know which approach is the right one and how to design the whole functionality. I think it has something to do with Listeners but I am not sure how to implement that. Thanks for your help!

Corovus
  • 38
  • 4
  • *"Thanks for your help!"* Thanks for your question! Oh.. BTW - what **is** the question? Is your question *"How to X?"*? If so, add it as an [edit to the question](http://stackoverflow.com/posts/36764921/edit). If not, think of a specific question and do the same (edit). – Andrew Thompson Apr 21 '16 at 09:28
  • 1
    *"Sort JList entries by creation date"* I'd use a (potentially single column) `JTable` for that, since it includes the [`RowSorter`](http://docs.oracle.com/javase/8/docs/api/javax/swing/RowSorter.html) functionality. See also this [filtered font list](http://stackoverflow.com/a/28621618/418556) which is a working example of using the `RowSorter` as a filter for records. *"..my first Java project.."* Then you should probably avoid trying to create a desktop app. with a GUI. That's advanced stuff! – Andrew Thompson Apr 21 '16 at 09:32
  • Back in the old days before we had a sort/filter API, we use to make proxy models which could sort/filter these types of things, for [example](http://stackoverflow.com/questions/34735325/how-to-sort-a-string-array-within-a-gui/34735734#34735734) – MadProgrammer Apr 21 '16 at 09:43
  • Basically, the model still does it job, but internally, it does the juggling of the indices to make it look sorted – MadProgrammer Apr 21 '16 at 09:45
  • @Andrew I don’t get how my question is unclear?! I want to know how I can sort the entries in a JList without sorting the actual objects in my ListModel. I know that there is a class called RowSorter but this only works for Tables. But the requirement for this project is to use a JList and NOT a JTable. And what is wrong about picking a challenging task for your first coding project? I mean, the only way to extend my knowledge is by trying out new things and deal with stuff I don’t understand, so I get better. – Corovus Apr 21 '16 at 12:56
  • *"And what is wrong about picking a challenging task for your first coding project?"* Congratulations! You have proven you're capable of asking a question. Notably that is the *first* question you've asked (as opposed to describing a requirement or explaining what you don't understand/know - which *aren't* questions). In answer to that question, I never said it was 'bad', just 'better to avoid' at this early stage of understanding Java (or programming?). A GUI'd app. is inherently more complicated than an app. that works from the command line. For starters a GUI has it's own thread model .. – Andrew Thompson Apr 21 '16 at 13:01
  • .. and added to the complexity for a cross platform GUI'd app. is laying out the components in a way that is logical across environments, and resizes in the most sensible manner (e.g. locking some components to original size and giving extra width and height to others). – Andrew Thompson Apr 21 '16 at 13:03
  • @Andrew English is not my native tongue so I can’t express myself as clear as I want but here is one last try: I know how to sort the objects inside my ListModel and then populate the JList with the results. All the content and properties of the notes are stored inside a .properties file. After starting the application these .properties files are loaded inside an array, sorted based on the creation date using the Decorate Pattern and then added to my ListModel... – Corovus Apr 21 '16 at 14:26
  • ..But the requirement for the “Sort Ascending/Descending Button” – function is NOT to sort the actual array inside my ListModel where the Notes-Objects are stored, but instead just sort the View (JList entries) without touching the ListModel. And my question is: How can I sort entries inside my JList without touching the actual Data (Objects stored inside my ListModel) I hope I am clear enough now. – Corovus Apr 21 '16 at 14:26

0 Answers0