Results 1 to 5 of 5
  1. #1
    loopsnhoops is offline Member
    Join Date
    Feb 2011
    Posts
    83
    Rep Power
    0

    Unhappy Calendar Help Please

    Hi,

    I need to write to a file every time I click on the forward button and I need the date to be accurate. It is hard for me to explain so take a look at the broken code.

    Java Code:
    package HouseHockey;
    
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.io.BufferedWriter;
    import java.io.FileWriter;
    import java.util.Date;
    import java.util.GregorianCalendar;
    
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import javax.swing.JScrollPane;
    import javax.swing.JTable;
    import javax.swing.table.DefaultTableModel;
    
    public class GameSheet {
    
    	GregorianCalendar cal;
    	int a = 0;
    
    	public GameSheet() {
    
    		JPanel panel1 = new JPanel();
    
    		JTable table;
    		DefaultTableModel mtable = new DefaultTableModel();
    		table = new JTable(mtable);
    		JScrollPane stbl = new JScrollPane(table);
    
    		table.setRowHeight(120);
    		mtable.setColumnCount(7);
    		mtable.setRowCount(6);
    
    		final int nod = cal.getActualMaximum(GregorianCalendar.DAY_OF_MONTH);
    
    		int day = GregorianCalendar.DAY_OF_MONTH;
    		int month = GregorianCalendar.MONTH;
    		int year = GregorianCalendar.YEAR;
    
    		JButton forward = new JButton("Forward");
    
    		JButton backward = new JButton("Backward");
    		
    		
    	
    
    		if (day == nod) {
    
    			day = 0;
    			month = month + 1;
    
    		}
    		if (month == 12) {
    
    			month = 0;
    			year = year + 1;
    
    		}
    		
    
    		try {
    			// Create file
    			FileWriter fstream = new FileWriter(";");
    			BufferedWriter out = new BufferedWriter(fstream);
    
    			out.write(day + "//" + month + "//" + year + "//");
    
    			// Close the output stream
    
    			out.close();
    
    		} catch (Exception e) {// Catch exception if any
    
    			System.err.println("Error: " + e.getMessage());
    
    		}
    
    		forward.addActionListener(new ActionListener() {
    			public void actionPerformed(ActionEvent actionEvent) {
    
    			day = day + 1;
    		
    
    			}
    
    		});
    	}
    }

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,949
    Rep Power
    19

    Default

    You haven't told us what the problem is.

  3. #3
    loopsnhoops is offline Member
    Join Date
    Feb 2011
    Posts
    83
    Rep Power
    0

    Default

    I am really not doing anything right here.
    Also the day variable in action listener for forward won't work because it wants to be set to final and when I do the other code won't work.

  4. #4
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,949
    Rep Power
    19

    Default

    Posting tip.
    You should provide any compilation errors...that is the text of them, and point out the line they occur on (a comment on the line will do, as bolding doesn't show up too well inside code tags).
    You should also give any (should the code compile) exception messages in full, including the line they occur on.

    Right, that aside...
    Java Code:
    new ActionListener() {
        public void actionPerformed(ActionEvent actionEvent) {
            day = day + 1;
        }
    }
    This is an anonymous inner class. Variables that are external to this class (ie day in your case) have to be declared final because of the nature of how these things work. By the time the above code is run the day variable will have vanished (since its scope is the method it's in, not the above anonymous class). The only way the compiler can work with that situation is if the variable is declared final so that the compiler knows it isn't going to change. You can think of it as nailing the variable down so the anonymous class can use it.

    So, how do we sort your code so that day is only set once?
    Java Code:
    final int day;  // note do not give it a value here.
    // Now compare nod against GregorianCalendar.DAY_OF_MONTH, since that is what you are currently doing.
    // If equal then set day = 0, else set day = GregorianCalendar.DAY_OF_MONTH.
    Of course this does bring up the question of what you are actually trying to do, since that logic does not really make sense. But we can tackle that bit after you've got this bit compiling.

  5. #5
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,197
    Rep Power
    19

Similar Threads

  1. event calendar
    By jagjit82 in forum Advanced Java
    Replies: 2
    Last Post: 02-25-2010, 04:53 AM
  2. How to add a calendar????
    By zifis in forum New To Java
    Replies: 5
    Last Post: 04-07-2009, 04:04 PM
  3. Calendar bug?
    By Stigvig in forum Advanced Java
    Replies: 14
    Last Post: 02-08-2009, 09:56 AM
  4. calendar
    By John in forum SWT / JFace
    Replies: 12
    Last Post: 08-07-2008, 10:54 PM
  5. Web calendar
    By Daniel in forum Enterprise JavaBeans (EJB)
    Replies: 1
    Last Post: 06-27-2007, 05:36 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •