hey
i have a little problem with this app, and i hope somebody can help me. i have to create an application that watch over your appointments.
i have the JFrame up and running, but i have some issues with the buttons:

when "New appointment" clicks, the data for the new event - Description, Content and Time- get written in.

the Description and Time get shown in the window. the appointments get shown with a JList that have a DefaultListModel.
here is a class-diagram:

Appointment:
-description
-content
-time
*Appointment() constructor
*Appointment() constructor
*setDescription()
*setContent()
*setTime()
*getDescription()
*getContent()
*getTime()
*toString()
*equals()
*compareTo()

AppointmentList:
-serialVersionUD
-list
-listData
*AppoinmentList() constructor
*valueChanged()
*main()
+ButtonPanel

AppointmentData
-serialVersionUD
*AppointmentData() constructor
*newAppointment()
*newAppointment()
*deletAppointment()
*getAppointment()

in the class AppointmentList is list a JList and listData is an instance off the class AppointmentData that inherit from DefaultListModel. list is connected to listData the normal way.

when "Delete appointment" get clicked, the marked choice get removed, but if no choice is marked, it should ask for the description.

when "Show appointment" get clicked, it shows all the data of the chosen appointment, but if no choice is marked it should ask for the description.

when "Changed appointment" get clicked, it ask about any updates or changes for the marked appointment, but if there is no marked appointment it should ask for the description. second it should ask for the changes. if its given a description for a new appointment, but this appointment already exist, the new appointment should not get registered.
if there is given a description in current with delete, show or changed of an appointment, and this appointment does not exist it would show a message of this.

when "Quit" get clicked the app shouts down.
the "Cancel" button in the dialog-box should be up and running, and give a message if clicked.

i hope somebody can help me with this...
i have written some of it, i can post it if you ask for it..

public class Appointment implements Comparable<Appointment> {

	private String description;
	private String content;
	private String time;

	public Appointment() {
		this(null, null, null);
	}

	public Appointment(String description, String content, String time) {

		setDescription(description);
		setContent(content);
		setTime(time);
	}

	public void setDescription(String description) {
		this.description = description;
	}

	public String getDescription() {
		return description;
	}

	public void setContent(String content) {
		this.content = content;
	}

	public String getContent() {
		return content;
	}

	public void setTime(String time) {
		this.time = time;
	}

	public String getTime() {
		return time;

	}
	public String toString(){
		return getDescription() + " - " + getTime();
	}
	public boolean equals(Object obj){
		if(!(obj instanceof Appointment))
			return false;
		if(obj == this)
			return true;
		
		Appointment appointment = (Appointment) obj;
		return getDescription().equals(appointment.getDescription());
	}
	public int compareTo(Appointment obj){
		return getDescription().compareTo(obj.getDescription());
	}
}

import java.awt.BorderLayout;

import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.*;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

public class AppointmentList extends JFrame implements ListSelectionListener 
{
	private JList list;
	private AppointmentData listdata;

	public AppointmentList() 
	{
		super("My appointments");
		listdata = new AppointmentData();
		list = new JList(listdata);
		list.addListSelectionListener(this);
		add(new JScrollPane(list), BorderLayout.CENTER);
		add(new ButtonPanel(), BorderLayout.EAST);
		setDefaultCloseOperation(EXIT_ON_CLOSE);
		setSize(400, 300);
		setVisible(true);
	}

	private class ButtonPanel extends JPanel implements ActionListener 
	{
		private JButton[] buttons;
		private final String[] text = { "New appointment", "Remove appointment",
				"Show appointment", "Change appointment", "Quit" };

		public ButtonPanel() 
		{
			setLayout(new GridLayout(5, 1));
			buttons = new JButton[5];

			for (int i = 0; i < buttons.length; i++) 
			{
				buttons[i] = new JButton(text[i]);
				buttons[i].addActionListener(this);
				add(buttons[i]);
			}
		}

		public void actionPerformed(ActionEvent event) 
		{
			String text1 = event.getActionCommand();
			if (text1.equals(text[0])) 
			{
				String description = JOptionPane.showInputDialog(
						AppointmentList.this, "Description");
				String content = JOptionPane.showInputDialog(
						AppointmentList.this,"Content");
				String time = JOptionPane.showInputDialog(
						AppointmentList.this, "Time");
				if (!listdata.newAppointment(description, content, time)) 
				{
					JOptionPane.showMessageDialog(AppointmentList.this,
							"The appointment did not get saved");
				}
			} 
			else if (text1.equals(text[1])) 
			{
				int index = list.getSelectedIndex();
				if (index >= 0) 
				{
					listdata.removeElementAt(index);
				} 
				else 
				{
					String description = JOptionPane.showInputDialog(
							AppointmentList.this, "Description");
					if (!listdata.deletAppointment(description)) 
					{
						JOptionPane.showMessageDialog(AppointmentList.this,
								"The appointment did not get deleted");
					}
				}
			}
				System.exit(0);
		}
	}

	public void valueChanged(ListSelectionEvent e) 
	{
		if (!list.getValueIsAdjusting()) 
		{
			Appointment choosenAppointment = (Appointment) list.getSelectedValue();
			if (choosenAppointment != null) 
			{
				JOptionPane.showMessageDialog(this, choosenAppointment.getContent());
			}
		}
	}

	public static void main(String[] args) 
	{
		new AppointmentList();
	}
}

import javax.swing.DefaultListModel;

public class AppointmentData extends DefaultListModel {



	public AppointmentData() {

	}

	public String newAppointment() {
		return null;

	}

	public boolean newAppointment(String description, String content, String time) {
		boolean input = false;
		Appointment newAppointment = new Appointment(description, content, time);

		if (!contains(newAppointment)) {
			addElement(newAppointment);
			input = true;
		}
		return input;
	}

	public boolean deletAppointment(String description) {
		boolean delete = removeElement(new Appointment(description, null, null));
		return delete;
	}

	public String getAppointment(String description) {
		return getAppointment(description);
	}
}
String text1 = event.getActionCommand();
if (text1.equals(text[0]))

you forgot to add setActionCommand for JButtons separatelly .... or change to

Object btn = event.getSource();
if (btn ==buttons[0]) {

why here is System.exit(0);, this colde line closed aplication without ...

Edited 5 Years Ago by mKorbel: n/a

the System.exit(0); is in the appointmentlist class

are you saying that all the 5 buttons should have a setActionCommand?

and how can you test that in ActionListener ==

public void actionPerformed(ActionEvent event)

better would be look at 2nd. option about returns Object

This article has been dead for over six months. Start a new discussion instead.