开发者

Help with java calendar

Need help to finish my java calendar.

Can anyone see why the text that is written on the date today are not saved?

And how can I ensure that written text is saved in OnDeleteEvent method when the window is closed?

My Code:

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.Date;
import java.text.SimpleDateFormat;

import java.beans.PropertyChangeListener;
import java.beans.PropertyChangeEvent;

import com.toedter.calendar.JCalendar;

public class Diary extends JPanel
{
    private JCalendar calendar;
    private JTextArea text;
    private SimpleDateFormat sdf;
    private JScrollPane scroll;

    private Value days = null;
    private String date = null;

    public Diary()
    {
        calendar = new JCalendar();
        sdf = new SimpleDateFormat("dd-MM-yyyy");
        text = new JTextArea(5,15);
        scroll = new JScrollPane(text);
        scroll.setVerticalScrollBarPolicy(
                scroll.VERTICAL_SCROLLBAR_ALWAYS);
        scroll.setPreferredSize(new Dimension(100, 100));

        date = GetCalendarDate();
        days = Load();

        calendar.addPropertyChangeListener(new OnDaySelected());
        calendar.addPropertyChangeListener(new OnDeleteEvent());

        setLayout (new Bo开发者_高级运维rderLayout());
            add(calendar, BorderLayout.NORTH);
            add(scroll, BorderLayout.SOUTH);
    }

    private static Value Load()
    {
        Value days;
        try
        {
            days = Value.loadFile("Diary.dat");
        }
        catch( Exception e )
        {
            days = Value.makeTable();
        }
        return days;
    }

    private static void Save( Value days )
    {
            try
            {
                Value.saveFile(days,"Diary.dat");
            }
            catch( Exception e)
            {
                Value.makeTable();
            }

    }

    private String GetCalendarDate()
    {
        Date d = calendar.getDate();
        String dat = sdf.format(d);
                return dat;
    }

    private String GetDayText( String date )
    {
              try
              {
                  Value v = Value.makeString(date);
                  String texti = days.get(v).asString();
                  return texti;
              }
              catch( Exception e)
              {
                  return "";
              }
    }

    private void SetDayText( String date, String text )
    {
        Value v = Value.makeString(date);
                if(text == null)
                    days.put(v,null);
                else
                    days.put(v, Value.makeString(text));

    }

    public void SetDate( String newdate )
    {
        if( newdate==date ) return;
        if( date!=null && GetDayText( date )!= text.getText() )
        {
            SetDayText(date,text.getText());
            Save(days);
        }
        date = newdate;
        text.setText(GetDayText(date));
    }

    private class OnDaySelected implements java.beans.PropertyChangeListener
    {
        public void propertyChange(PropertyChangeEvent evt)
        {
            SetDate(GetCalendarDate());
        }
    }

    private class OnDeleteEvent implements java.beans.PropertyChangeListener
    {
        public void propertyChange(PropertyChangeEvent evt)
        {
            if(GetDayText(date) != text.getText())
            {
                SetDayText(date, text.getText());
                Save(days);
            }
            // how to close the program and ensure that unsaved text is saved
            // before the application closed? 


        }
    }

    public static void main(String[] args)
    {
        try
        {
            UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
        }
        catch (Exception e) {}

        JFrame frame = new JFrame("Diary");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setContentPane(new Diary());
        frame.pack();
        frame.setVisible(true);
    }
}


if( date!=null && GetDayText( date )!= text.getText() )

You mean if( newdate!=null && GetDayText( newdate)!= text.getText() ) don't you?

And how can I ensure that written text is saved in OnDeleteEvent method when the window is closed?

Register a WindowListener on the window.

Edit:

Note, that this might not necessarily return true for equal texts: newdate==date
This depends on newdate being exactly the same object as date (which is quite probable since strings are immutable and might come from the JVM's string pool). However, you'd better use (newdate == null && date == null) || (newdate != null && newdate.equals(date)), i.e. if both strings are null or they have the same content they're equal.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜