Making a Java Calender
I have big problems making a java calender work as I want. The calender must work in a program. I use "Core Java Fundamentals" and I have program simple java for a half year now. I'm using the program "eclipse" I hope some one can help me. here is the code (I'm a Norwegian so and we have 3 more letters then the English Alfabet, so don't mind the ? ? ?:
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Calendar;
import java.util.GregorianCalendar;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.JTextField;
/*Vi skal pr痸e ?lage et program som viser et vindu med et
* tekstomr耫e i center. I s痳 skal to tekstfelt
* et for m耼ed og et for 聄 samt en knapp som heter Lag
* Ideen er n聄 vi skriver m耼ed og 聄 og velger Lag
* s?skal programmet generer en oversikt i tekstomr耫et
* slik som vi er vant til fra en vanlig kalender
*/
public class datotabell {
public static void main(String[] args) {
//lager et grafisk vindu som viser det grensesnittet
//Lager et objekt som er ("is-a") et vindu mm
MittVindu mv=new MittVindu();
}
}
class MittVindu /*is-a*/ extends JFrame implements ActionListener{
public MittVindu(){
this.setSize(400,300);
this.setLocation(20,20);
// this.setBounds(x, y, width, height)ound Denne kan ogs?brukes
this.lagMeny();
this.lagCenter();
this.lagSor();
this.setVisible(true);
}
private void lagMeny(){
//this er tilgjengelig
JMenuBar jmb=new JMenuBar();//1
JMenu jm=new JMenu("Fil"); //2
jmb.add(jm); //3
this.setJMenuBar(jmb); //4
// 1 m?komme f痳 4, 2 og 1 m?komme f痳 3
}
private void lagCenter(){//kun this tilgjengelig
this.getContentPane().add(jta,BorderLayout.CENTER);
}
MittPanel mp;
private void lagSor(){
MittPanel mp=new MittPanel();
this.getContentPane().add(mp,BorderLayout.SOUTH);
//hvordan koble handlingen trykk knappen "lag" til noe i MittVindu?
JButton knappenfraPanelet=mp.getKnapp();
knappenfraPanelet.addActionListener(this);
}
public void actionPerformed(ActionEvent evt)
{
String year=mp.getYear();
String month=mp.getMonth();
String s=lagOversikt(month, year);
jta.setText(s);
}
private String lagOversikt(String m,String y){
// String s="";
String output ="<html>" +
"<table BORDER=1 CELLSPACING=1 CELLPADDING=1>";
// construct d as current date
GregorianCalendar d = new GregorianCalendar();
int today = d.get(Calendar.DAY_OF_MONTH);
int month = d.get(Calendar.MONTH);
// set d to start date of the month
d.set(Calendar.DAY_OF_MONTH, 1);
int weekday = d.get(Calendar.DAY_OF_WEEK);
// print heading
output += "<TR><TD ALIGN=center>Mon</TD>"
+"<TD ALIGN=center>Tue</TD>"
+"<TD ALIGN=center>Wed</TD>"
+"<TD ALIGN=center>Thu</TD>"
+"<TD ALIGN=center>Fri</TD>"
+"<TD ALIGN=center>Sat</TD>"
+"<TD ALIGN=center>Sun</TD>"
+"</TR>";
// indent first line of calendar
for (int i = Calendar.MONDAY; i < weekday; i++ )
output +="<td align=\"center\"></td>";
do
{
// print day
int day = d.get(Calendar.DAY_OF_MONTH);
if (day==today)
output += "<td align=\"center\"><b>"+day+"</b></td>";
else output += "<td align=\"center\">"+day+"</td>";
// start a new line after every Saturday
if (weekday == Calendar.MONDAY)
output +="</tr><tr>";
// advance d to the next day
d.add(Calendar.DAY_OF_MONTH, 1);
weekday = d.get(Calendar.DAY_OF_WEEK);
}
while (d.get(Calendar.MONTH) == month);
// the loop exits when d is day 1 of the next month
// print final end of line if necessary
if (weekday != Calendar.MONDAY);
output+=("");
output +="</tr></table></html>";
return output;
}
JTextArea jta=new JTextArea();
}
class MittPanel extends JPanel implements ActionListener
{
//deklarasjonene st聄 utenfor handlingene slik at
//MittPanel eier dem "has-a"
private static final long serialVersionUID = 1L;
JTextField jtf1=new JTextField(10);
JTextField jtf2=new JTextField(4);
JButton jb=new JButton("Lag");
//add(LagButton);
//jb.addActionListener(this);
public MittPanel(){
this.add(jtf1);
this.add(jtf2);
this.add(jb);
jb.addActionListener(this);
}
public void actionPerformed(ActionEvent evt){
}
public JButton getKnapp(){
return jb;
}
public String getYear(){
return jtf2.getText();
}
public String getMonth(){
return jtf1.getText();
}
}
[5281 byte] By [
Stian7a] at [2007-11-26 17:29:12]

read [url= http://forum.java.sun.com/help.jspa?sec=formatting]this.[/url] Try again.
Legg kildekoden mellom [ code ]..[ /code ] s?det er lettere ?se. Og ikke vis hel programmet. Hvis du har gjort et fors鴎 p邋 finne problemet vet du sikkert s錸n omtrent hvor det er i programmet. Ingen her f錼 betalt for ?hjelpe deg, i motsetning til din l鎟er, s?her er det lurt ?gj鴕e det s?attraktiv for folk ?svare som mulig.
Instead of pasting a load of code, why not ask us a specific question that we can be arsed to answer?Ted.
> Legg kildekoden mellomCareful, You'll get arrested for that in most of the developed world. :-)
This is my first time on this Forum so I don't know how put things here. My problem is that the button that should make the calender wount work. The code shows no erro messages, so I miss something.
L鎟eren min Har alltid 2-3 versioner av oppgaven fordi han her ennormt med skrive feil. Han har problemer med ?l鎟e fra seg.
Edit: I'm sorry about the codething, but I'm not abel to edit it now. The forum wount let me.
The forum should let you... you should see an edit button on the top right handside of each of your posts... but not the one right down the bottom of the page
Please take the appropriate code for your button and its ActionEvent and put it between [ code ] [ /code ]. If your button does not work it is either because you have not associated it with an ActionEvent, or your ActionEvent is not properly written.
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Calendar;
import java.util.GregorianCalendar;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.JTextField;
/*Vi skal pr痸e ?lage et program som viser et vindu med et
* tekstomr耫e i center. I s痳 skal to tekstfelt
* et for m耼ed og et for 聄 samt en knapp som heter Lag
* Ideen er n聄 vi skriver m耼ed og 聄 og velger Lag
* s?skal programmet generer en oversikt i tekstomr耫et
* slik som vi er vant til fra en vanlig kalender
*/
public class datotabell {
public static void main(String[] args) {
//lager et grafisk vindu som viser det grensesnittet
//Lager et objekt som er ("is-a") et vindu mm
MittVindu mv=new MittVindu();
}
}
class MittVindu /*is-a*/ extends JFrame implements ActionListener{
public MittVindu(){
this.setSize(400,300);
this.setLocation(20,20);
// this.setBounds(x, y, width, height)ound Denne kan ogs?brukes
this.lagMeny();
this.lagCenter();
this.lagSor();
this.setVisible(true);
}
private void lagMeny(){
//this er tilgjengelig
JMenuBar jmb=new JMenuBar();//1
JMenu jm=new JMenu("Fil"); //2
jmb.add(jm); //3
this.setJMenuBar(jmb); //4
// 1 m?komme f痳 4, 2 og 1 m?komme f痳 3
}
private void lagCenter(){//kun this tilgjengelig
this.getContentPane().add(jta,BorderLayout.CENTER);
}
MittPanel mp;
private void lagSor(){
MittPanel mp=new MittPanel();
this.getContentPane().add(mp,BorderLayout.SOUTH);
//hvordan koble handlingen trykk knappen "lag" til noe i MittVindu?
JButton knappenfraPanelet=mp.getKnapp();
knappenfraPanelet.addActionListener(this);
}
public void actionPerformed(ActionEvent evt)
{
String year=mp.getYear();
String month=mp.getMonth();
String s=lagOversikt(month, year);
jta.setText(s);
}
private String lagOversikt(String m,String y){
// String s="";
String output ="<html>" +
"<table BORDER=1 CELLSPACING=1 CELLPADDING=1>";
// construct d as current date
GregorianCalendar d = new GregorianCalendar();
int today = d.get(Calendar.DAY_OF_MONTH);
int month = d.get(Calendar.MONTH);
// set d to start date of the month
d.set(Calendar.DAY_OF_MONTH, 1);
int weekday = d.get(Calendar.DAY_OF_WEEK);
// print heading
output += "<TR><TD ALIGN=center>Mon</TD>"
+"<TD ALIGN=center>Tue</TD>"
+"<TD ALIGN=center>Wed</TD>"
+"<TD ALIGN=center>Thu</TD>"
+"<TD ALIGN=center>Fri</TD>"
+"<TD ALIGN=center>Sat</TD>"
+"<TD ALIGN=center>Sun</TD>"
+"</TR>";
// indent first line of calendar
for (int i = Calendar.MONDAY; i < weekday; i++ )
output +="<td align=\"center\"></td>";
do
{
// print day
int day = d.get(Calendar.DAY_OF_MONTH);
if (day==today)
output += "<td align=\"center\"><b>"+day+"</b></td>";
else output += "<td align=\"center\">"+day+"</td>";
// start a new line after every Saturday
if (weekday == Calendar.MONDAY)
output +="</tr><tr>";
// advance d to the next day
d.add(Calendar.DAY_OF_MONTH, 1);
weekday = d.get(Calendar.DAY_OF_WEEK);
}
while (d.get(Calendar.MONTH) == month);
// the loop exits when d is day 1 of the next month
// print final end of line if necessary
if (weekday != Calendar.MONDAY);
output+=("");
output +="</tr></table></html>";
return output;
}
JTextArea jta=new JTextArea();
}
class MittPanel extends JPanel implements ActionListener
{
//deklarasjonene st聄 utenfor handlingene slik at
//MittPanel eier dem "has-a"
private static final long serialVersionUID = 1;
JTextField jtf1=new JTextField(10);
JTextField jtf2=new JTextField(4);
JButton jb=new JButton("Lag");
//add(LagButton);
//jb.addActionListener(this);
public MittPanel(){
this.add(jtf1);
this.add(jtf2);
this.add(jb);
jb.addActionListener(this);
}
public void actionPerformed(ActionEvent evt){
}
public JButton getKnapp(){
return jb;
}
public String getYear(){
return jtf2.getText();
}
public String getMonth(){
return jtf1.getText();
}
}
STOP IT
STOP IT
STOP IT
STOP IT
STOP IT
STOP IT
STOP IT
STOP IT
STOP IT
STOP IT
STOP IT
STOP IT
STOP IT
STOP IT
STOP IT
STOP IT
STOP IT
STOP IT
STOP IT
STOP IT
STOP IT
STOP IT
STOP IT
STOP IT
STOP IT
STOP IT
STOP IT
STOP IT
STOP IT
STOP IT
STOP IT
STOP IT
STOP IT
STOP IT
STOP IT
STOP IT
STOP IT
STOP IT
STOP IT
STOP IT
STOP IT
STOP IT
STOP IT
STOP IT
STOP IT
STOP IT
STOP IT
STOP IT
STOP IT
STOP IT
STOP IT
STOP IT
STOP IT
STOP IT
STOP IT
STOP IT
STOP IT
STOP IT
STOP IT
STOP IT
STOP IT
STOP IT
STOP IT
STOP IT
STOP IT
STOP IT
STOP IT
STOP IT
STOP IT
STOP IT
STOP IT
STOP IT
STOP IT
STOP IT
STOP IT
STOP IT
STOP IT
Stian7, please go away or actually listen to peoples request for you to stop just posting a load of code without a question.Ted.
I concur. His involvement in this seem somewhat lacking.
I disagree. He stated that when he clicks the button, the calendar should be constructed, and nothing happens. He also responded to the request to use code tags, by reposting the code in tags. While this was not the optiimal solution, I am willing to forgive that transgression.
@OP, the problem is you are declaring MittPanel mp at the instance level, but in you code where you instantiate mp, you are declaring a local MittPanel mp instead. One guess what happens after you exit that method.
Times up, mp is null.
~Tim
We should allow some leverage for new comers here. For some of us, it takes some time to get used to. We should encourage the learning process.We have people asking for help from different back grounds, and we should not forget that.
Praise to all the people working here and helping others.
God bless.
I certainly agree with that. It is also helpful to inform that code should be placed inside tags, and that the more concrete one is, the more likely it is to get answers. To me it seemed that the OP only recognised he had a problem, but could not be bothered finding where in his code this could be located. This is a very important step in the learningprocess. Simply placing any code within tags does not constitute an effort in my book. This is just me. If the OP felt offended I am sorry.
> The forum should let you... you should see an edit> button on the top right handside of each of your> posts... but not the one right down the bottom of the> pageYou can't edit a post which someone has replied to.Kaj
kajbja at 2007-7-21 17:05:07 >

Thanks for the help! And thanks for a longer answer then "Stop it" ;)
Yes that was the problem, I can't edit the first post I made, so I had to put it after.
@ted_trippin
I do listen and I do try. This is my first time, don't give me a hard time mate.
Message was edited by:
Stian7