hi my name is hiroshi.. im new with java programming.. my prof gave us an assignment to make a calendar.. i don't know what to do.. my head going crazy i dont know what is wrong with my code..

please help me T_T your big help is much appreciated..

import java.awt.*;
import java.applet.Applet;
import javax.swing.*;
import java.awt.event.*;
public class ClsCalendar extends Applet implements ActionListener

   JLabel lblInput= new JLabel("Input year:");

   JTextField txtInput= new JTextField(10);
   JLabel lbl1= new JLabel("");
   //TextField txt1= new JTextField(100);

   JButton btnOk=new JButton("OK");
   JButton btnClr=new JButton("Clear");

   public void init()
      //setLayout(new GridLayout(28,15));
      //setLayout(new FlowLayout());
     // txt1.setLayout(new GridLayout(28,15));
      //lbl1.setPreferredSize(new Dimension(500,500));     
4 Years
Discussion Span
Last Post by JamesCherrill

Yeah... I have no idea what you're trying to show here...

Why don't you start by explaining what you are trying to do, give some code of problem areas, explain what it is doing wrong and what is should be doing, and any unexpected exceptions you are getting (and where they're coming from)?


You're not ready to start any coding yet because you don't know what the code is supposed to do! I suggest you get a paper and pencil and try drawing a calendar or two - with real dates. That way you will learn what's needed in your program.


This is a revised version of my calendar code.
This is a java applet which asks the user to input any year and press OK to show the whole calendar for that year.
I’m putting the output on a JLabel. When I compiled it, it doesn’t have any errors but when I tried to run it on an appletviewer, it says “applet start not initialized”. And it has ArrayIndexOutOfBoundsException 12. I’m guessing it has something to do with my array of JLabel. How do I fix that problem then. Any help would be much appreciated. Sorry, just a beginner in java.

import java.awt.*;
import java.applet.Applet;
import javax.swing.*;
import java.awt.event.*;
public class ClsCalendar extends Applet implements ActionListener

      int iYear=0;
      int a=0;
      int b=0, c=0;
      int iStartOfDay=0;
      int iMonth=0;
      int iDay=0;
      String sHolder="";

     int iDaysInMonth[]={0,31,28,31,30,31,30,31,31,30,31,30,31};
     String iArrMonths[]=


   JLabel lblInput= new JLabel("Input year:");
   JTextField txtInput= new JTextField("2012",4);
**   JLabel[] lbl1= new JLabel[12];  **
   JButton btnOk=new JButton("OK");
   //JButton btnClr=new JButton("Clear");

   public void init()

         String s=txtInput.getText();

      for(iMonth=1; iMonth<=lbl1.length; iMonth++)
            sHolder+=("<html><br><br>S  M  T  W  Th  F  S<br></html>" );

            // Correct the position for the first date
            for ( iDay = 1; iDay <= 1 + iStartOfDay * 5; iDay++ )
                sHolder+=(" ");

            // Print all the dates for one month
            for ( iDay = 1; iDay <= iDaysInMonth[iMonth]; iDay++ )
               sHolder+=( iDay );

            // Is day before Sat? Else start next line Sun.
            if ( ( iDay + iStartOfDay ) % 7 > 0 )
                     sHolder+=("   " );
                     sHolder+=("\n " );

             // Set position for next month
             iStartOfDay = ( iStartOfDay + iDaysInMonth[iMonth] ) % 7;
             lbl1[iMonth]=new JLabel(sHolder);

   public void actionPerformed(ActionEvent ae)

      // if the user pressed OK, the calendar will now be visible

         for(int i=1;i<=12;i++)

The indexes for arrays can have values from 0 to the length of the array -1.
Check that the maximum value for an index is less than the length of the array.
Use the .length attribute of the array to test against. Don't hard code a number:

for(int i=0; i < theArray.length; i++) { 

thank you for that but I tried what you said but it's still not running. Same error is showing.


What changes did you make to the code? Can you post the lines of source code where the error is happening?


You have no idea what NormR1 said, haven't you? If you literally copy what NormR1 said, you will get an error. What you need to apply is the number range to all of your for-loop condition (lines 58, 64, 60, 75, etc)... For example...

for(iMonth=0; iMonth<lbl1.length; iMonth++) {

I changed for( int i=0; i<=12; i++) to for(int i=0; i<lbl1.length;i++) . Keep getting array index out of bounds exception 12 when i tried to run it on appletviewer.


Please post the full text of the error message that shows what line the error happens on.
You need to fix ALL the places where the index goes past the end of an array.


i've fixed the error and my program is now working :) thanks a lot :)
but I have a follow up question though, is it possible to put a new line character or "\n" in a JLabel? because it is not showing as I want it to be.
for example:
I want the string "hello everyone" to be
is this possible?

str="hello \n everyone";
JLabel labelhello=new JLabel(str);

No, not like that. But JLabels support simple HTML, so you can do things like

   JLabel text1 = new JLabel("<HTML><H1>This is a demo of some of the effects"
         + "<BR>that can be achieved with HTML</H1>"
         + "<BR>(in Java)");

... or, if you don't want any formatting, you can just use a JTextArea instead.

Edited by JamesCherrill


i've already tried that before but the output is also showing the words "<HTML><BR...

JLabel text=new JLabel("<HTML><H1>Hello" + "<BR>Everyone");
// the output is:
<HTML><H1>Hello <BR>Everyone

uhm wait. does it have any difference if my JLabel is in the form of array?
and if that certain label is to be put in a panel?

JPanel panel1=new JPanel[2];
JLabel label1=new JLabel[2];
//then i have an array of string to be put on the JLabel and i Want it to be separated by line.
String[] months={"January", "February"};

for( int i=0; i<2; i++)
   label1=new JLabel(months[i] + "<HTML><H1><BR>");

// add label to the first panel

No, no difference at all. What verion of Java are you using?
ps Your code on line 10 creates invalid HTML becuase it has text before the html tag. If the HTML is invalid then the JLable just displays the whole string as text.


Yes. As in new JLabel("<HTML><H1>Hello" + "<BR>Everyone")
You may want to look at a simple intro to HTML if you're not already familiar with it.
Was that really the exact code that you tested earlier????
Java 1.7 is correct.


No, actually I am just making my example simplier than my real code I am really testing.
It's still my calendar code (above) but its output is wrong.
Its output on one panel is on one line only..

as in "january<HTML><H1><BR>S M W TH F S<HTML><H1><BR>1 2 3 4 5.. and so on.

That is why I am asking how to use new line in JLabel.

This article has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.