#Help with a Task in Java GUI
17 messages · Page 1 of 1 (latest)
⌛ This post has been reserved for your question.
Hey @upper rover! Please use
/closeor theClose Postbutton above when your problem is solved. Please remember to follow the help guidelines. This post will be automatically closed after 300 minutes of inactivity.
TIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here.
just ask here, try to translate the gist of the task to english
In this task, you are required to enhance the appointment calendar, for which you created a GUI on the last project sheet, with user interaction capabilities for creating and displaying appointments. The prerequisites for this task are:
Then, add two inner classes to the GUI class, implementing the ActionListener interface.
-
First Listener Class:
- Extract values from ComboBoxes for the year, month, etc.
- Convert these values into appropriate integer values.
- Create an instance of the
Terminclass along with the text from the GUI's text field describing the content of the appointment. - Insert this instance into the list of appointments in the surrounding
TerminGUIclass using theaddTerminmethod from theTerminlisteclass. - Display the appointment along with the heading "New Appointment" in the lower text area.
-
Second Listener Class:
- Using the second listener class, display a list of appointments (sorted by date) in the lower text area.
- If the list is empty, output the message "No Appointments."
- Sorting of appointments within the
Terminlisteclass can be triggered by thesortTerminemethod.
-
Complete the
createListenermethod as follows: Add an instance of the first listener class as anActionListenerto one button and an instance of the second listener class to the other button.
Bonus Task (5 Points):
Write two additional listener classes (of type ItemListener) and assign them to the ComboBoxes for the year and month. Ensure that when a year or month is selected, the list of days in the ComboBox for the days is adjusted accordingly. For example, for February 2023 and 2025, the values 1-28 should be selectable, and for February 2024, the values 1-29 should be available in the ComboBox. You may use the provided method fillComboBoxTage in the TerminGUI class for this purpose.
Given Code:
private Termin[] termine;
private int extend; // Anzahl der Elemente, um die das Feld termine
// bei Bedarf vergrößert wird
private int currentIndex = -1;
/* leere Terminliste anlegen mit (initial) Platz fuer 5 Termine */
public Terminliste(){
this(5);
}
/* leere Terminliste anlegen mit (initial) Platz fuer extend Termine */
public Terminliste(int extend){
this.extend = extend;
termine = new Termin[extend];
}
/* gibt den Termin an Position i zurueck: i = 1,2,3, ...
Fuer einen ungueltigen Wert (i <= 0) oder i >= Laenge des Felds termine
wird null zurueckgegeben
*/
public Termin getTermin(int i){
return ((i > 0) && (i <= termine.length)) ? termine[i-1] : null;
}
/* gibt die aktuelle Anzahl in der Liste eingetragener Termine zurueck */
public int getAnzahlTermine() {
return currentIndex+1;
}
private void extendTerminArray(){
Termin[] newTermine = new Termin[termine.length + extend];
for (int i=0;i<termine.length;i++){
newTermine[i] = termine[i];
}
termine = newTermine;
}
/* Termin hinzufuegen: Falls Array voll, wird dieses zunaechst erweitert */
public void addTermin(Termin t){
if ((currentIndex+1) >= termine.length){
extendTerminArray();
}
termine[++currentIndex] = t;
}
/* Sortiert Array mit Terminen aufsteigend nach Datum */
public void sortTermine(){
if (currentIndex >= 1) {
java.util.Arrays.sort(termine,0,currentIndex+1);
}
}
} ```
This message has been formatted automatically. You can disable this using /preferences.
import java.util.*;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
```
public class Termin implements Comparable<Termin>{
LocalDateTime wann;
String was;
private Termin(LocalDateTime ldt, String was){
this.wann = LocalDateTime.of(ldt.toLocalDate(),ldt.toLocalTime());
this.was = was;
}
public Termin(int year, int month, int day, int hour, int minute, String descr){
this(LocalDateTime.of(year,month,day,hour,minute),descr);
}
public String toString(){
DateTimeFormatter dtf =
DateTimeFormatter.ofPattern("dd.MM.YY 'um 'HH':'mm'h '",Locale.GERMAN);
return dtf.format(this.wann) + "--> " + was;
}
public boolean equals(Object other){
return (other instanceof Termin) ?
this.wann.equals(((Termin)other).wann) && this.was.equals(((Termin)other).was) :
false;
}
public int compareTo(Termin other){
return this.wann.compareTo(other.wann);
}
public Termin clone(){
return new Termin(LocalDateTime.of(this.wann.toLocalDate(),
this.wann.toLocalTime()),this.was);
}
} ```
This message has been formatted automatically. You can disable this using /preferences.
I know it is a little bit chaotic you can Dm or call me so we can figure the Soulition out.
What do you need help with exactly ?
Second Listener Class: I dont know how to compare the List and Sort them afterwards, the first Listener Class I somehow managed to get the 1st one
you are not calling your createListener method
and you don't have to convert the tListe to an array. You did define a get method, use that with a traditional for-loop