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));
      add(lblInput);
      add(txtInput);
      add(btnOk);
      btnOk.addActionListener(this);
      add(btnClr);
      btnClr.addActionListener(this);
      //setLayout(new FlowLayout());
      //add(txt1);
      add(lbl1);
      //lbl1.setVisible(false);
     // txt1.setLayout(new GridLayout(28,15));
      //lbl1.setBackground(Color.YELLOW);
      //lbl1.setPreferredSize(new Dimension(500,500));     
      //lbl1.setEditable(false);

Recommended Answers

All 19 Replies

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[]=
     {

     "\n\n\nJanuary",
    "\n\n\nFebruary",
    "\n\n\nMarch",
    "\n\n\nApril",
    "\n\n\nMay",
    "\n\n\nJune",
    "\n\n\nJuly",
    "\n\n\nAugust",
    "\n\n\nSeptember",
    "\n\n\nOctober",
    "\n\n\nNovember",
    "\n\n\nDecember"
     };


   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()
   {
      add(lblInput);
      add(txtInput);
      add(btnOk);
      btnOk.addActionListener(this);
      //add(btnClr);
      //btnClr.addActionListener(this);


         String s=txtInput.getText();
         iYear=Integer.parseInt(s);
         a=(iYear-1)/4;
         b=(iYear-1)/100;
         c=(iYear-1)/400;
         iStartOfDay=(iYear+a-b+c)%7;

      for(iMonth=1; iMonth<=lbl1.length; iMonth++)
      {
            sHolder+=(iArrMonths[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+=("   " );
            else
                     sHolder+=("\n " );
             }

             // Set position for next month
             iStartOfDay = ( iStartOfDay + iDaysInMonth[iMonth] ) % 7;
             lbl1[iMonth]=new JLabel(sHolder);
             add(lbl1[iMonth]);
             lbl1[iMonth].setVisible(false);
   }//for
 }//init

   public void actionPerformed(ActionEvent ae)
   {

      // if the user pressed OK, the calendar will now be visible
      if(ae.getSource()==btnOk)
      {

         for(int i=1;i<=12;i++)
         {lbl1[i].setVisible(true);}    
       }//if
    }//ap
}//classCalendar

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
hello
everyone
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.

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

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

Just tried your exact code and it worked perfectly. What version of Java are you using?

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?

//sample
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
panel[0].add(label1[0]);
panel[1].add(label1[1]);

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.

so i must put <HTML> before the text?
I am using java 1.7

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.

I already answered you - you have to start with the html tag, eg try this:

"<HTML><H1>january<BR>S M W TH F S<BR>1 2 3 4 5"

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.