Java help please - GT-R Register - Official Nissan Skyline and GTR Owners Club forum

Want to buy a banner ad? Find out more here.

Go Back   GT-R Register - Official Nissan Skyline and GTR Owners Club forum > Non-Skyline/GTR sub forums > Non-Skyline/GTR related chat



Reply
 
LinkBack Thread Tools Rate Thread Display Modes
Old 31st October 2008, 08:05 AM   #1 (permalink)
hyrev is dog tired
GTR Register Member
 
hyrev's Avatar
 
Join Date: Apr 2004
Location: Not in Japan anymore - :(
Cars owned: Top Secret built R34 GT-R
Posts: 4,234
Java help please

ok, I am nearly finished but cannot get the last part of the code to work.
I need to display the days of the selected month and year in my text area

like

Su Mo Tu We Th Fr Sa
1 2 3 4 5 6
7 8 9 10 ..........

I need help with this, please. Here is the code so far.
Everything else is working, but perhaps someone can tell
me why the month is being displayed twice in the pull down
list? I have been at this too long and am starting to see
double and triple, growing grey hairs, etc...

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;

public class Calendar4 implements ActionListener
{
private JFrame outputFrame;
private Container container;
private JLabel yearLabel, monthLabel;
private JTextField inputYear;
private JButton myButton, calcButton;
private ButtonGroup myButtonGroup;
private JTextArea myTextArea;
private JComboBox inputMonth;

String days[] = {"Su ", " Mo ", " Tu ", " We ", " Th ", " Fr ", " Sa "};

String months[] = {"January", "February", "March", "April",
"May", "June", "July", "August", "September",
"October", "November", "December"};

int DaysInMonth[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};

public static void main( String [] args )
{

Calendar4 myFrameDemo = new Calendar4();

}
int userMonth;
int userYear;
int firstDay;

public Calendar4()
{
outputFrame = new JFrame();
outputFrame.setSize(500,400);
outputFrame.setDefaultCloseOperation(JFrame.EXIT_O N_CLOSE);

container = outputFrame.getContentPane();

container.setLayout( new FlowLayout() );

container.setBackground(Color.RED);

yearLabel = new JLabel( "Enter Year (After 1900)" );
container.add( yearLabel );

inputYear = new JTextField( "", 10 );
container.add( inputYear );
inputYear.addActionListener( this );

monthLabel = new JLabel( "Select Month" );
container.add( monthLabel );

inputMonth = new JComboBox(months);
for (int i = 0; i < 12; i++)
inputMonth.addItem(months[i]);
container.add( inputMonth );
inputMonth.setSelectedIndex(0);
inputMonth.addActionListener(this);

calcButton = new JButton( "Press To Display Calendar" );
container.add( calcButton);
calcButton.addActionListener( this );


myTextArea = new JTextArea(10,40);
container.add( new JScrollPane(myTextArea));

outputFrame.setVisible(true);
}

public void actionPerformed( ActionEvent event )
{

if ( event.getSource() == calcButton )
{
{
userYear = Integer.parseInt(inputYear.getText(), 10);
{
if (isLeapYear(userYear))
myTextArea.append("Is a leap year\n");
else
myTextArea.append("Is not a leap year\n");
}
if (userYear > 1899)
{
userYear = userYear;
}
else
myTextArea.append("\nYear must be 1900 or after\n");
}
userMonth = inputMonth.getSelectedIndex();

for (int i = 0; i < 7; i++)
myTextArea.append(" " + days[i]);

}
}

public boolean isLeapYear( int userYear ) {
if (userYear%4==0 && userYear%100!=0 || userYear%400==0)
return true;
else
return false;
}

int NumberRowsNeeded(int userYear, int userMonth)

{

int numCells;

firstDay = CalcFirstOfMonth(userYear, userMonth);

if ((userMonth == 1) && (firstDay == 0) && !IsLeapYear(userYear))
return (4);

numCells = firstDay + DaysInMonth[userMonth];

if ((userMonth == 1) && (IsLeapYear(userYear))) numCells++;

return ((numCells <= 35) ? 5 : 6);
}

int CalcFirstOfMonth(int userYear, int userMonth)

{
int firstDay;
int i;

firstDay = CalcJanuaryFirst(userYear);

for (i = 0; i < userMonth; i++)
firstDay += DaysInMonth[i];

if ((userMonth > 1) && IsLeapYear(userYear)) firstDay++;

return (firstDay % 7);

}

boolean IsLeapYear(int userYear)

{

if ((userYear % 100) == 0) return((userYear % 400) == 0);

return ((userYear % 4) == 0);
}

int CalcJanuaryFirst(int userYear)

{

return (((userYear - 1900) + 365));
}
}
__________________
hyrev is offline   Reply With Quote
Old 31st October 2008, 08:41 AM   #2 (permalink)
moz
moz has no status.
GTR Register Member
 
Join Date: Mar 2005
Location: Finland
Cars owned: R32 GT-R
Posts: 281
First, the months are displayed twice because of this:

The combobox is created with a string[] parameter of values.
Code:
inputMonth = new JComboBox(months);
Then more values are put in the list (duplicating the entries).
Code:
for (int i = 0; i < 12; i++)
{
     inputMonth.addItem(months[i]);
}
JComboBox has one constructor like this:

Code:
JComboBox(Object[] items) 
Creates a JComboBox that contains the elements in the specified array
So you can either do this:

Code:
inputMonth = new JComboBox(); // No parameter
for (int i = 0; i < 12; i++) // Fill in values
{
     inputMonth.addItem(months[i]);
}
or simply this, as you have done and lose the for loop:
Code:
inputMonth = new JComboBox(months);
I'll look at the rest if I get time
__________________
moz is offline   Reply With Quote
Old 31st October 2008, 01:00 PM   #3 (permalink)
moz
moz has no status.
GTR Register Member
 
Join Date: Mar 2005
Location: Finland
Cars owned: R32 GT-R
Posts: 281
This works, just the formatting of the output needs sorting from the DisplayCalendar() method, I'm not to great with formatting. I'm sure you'll manage

Code:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class Calendar4 implements ActionListener
{
	private JFrame outputFrame;
	private Container container;
	private JLabel yearLabel,  monthLabel;
	private JTextField inputYear;
	private JButton myButton,  calcButton;
	private ButtonGroup myButtonGroup;
	private JTextArea myTextArea;
	private JComboBox inputMonth;
	String days[] =
	{
		"Su ", " Mo ", " Tu ", " We ", " Th ", " Fr ", " Sa "
	};
	String months[] =
	{
		"January", "February", "March", "April",
		"May", "June", "July", "August", "September",
		"October", "November", "December"
	};
	int DaysInMonth[] =
	{
		31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31
	};
	
	protected String[][] calendar = 
	{ 
		{ " ", " ", " ", " ", " ", " ", " " },
		{ " ", " ", " ", " ", " ", " ", " " },
		{ " ", " ", " ", " ", " ", " ", " "	},
		{ " ", " ", " ", " ", " ", " ", " "	},
		{ " ", " ", " ", " ", " ", " ", " "	},
		{ " ", " ", " ", " ", " ", " ", " "	}
	};
	
	public static void main(String[] args)
	{
		new Calendar4();
	}

	public Calendar4()
	{
		outputFrame = new JFrame();
		outputFrame.setSize(500, 400);
		outputFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

		container = outputFrame.getContentPane();

		container.setLayout(new FlowLayout());

		container.setBackground(Color.RED);

		yearLabel = new JLabel("Enter Year (After 1900)");
		container.add(yearLabel);

		inputYear = new JTextField("", 10);
		container.add(inputYear);
		inputYear.addActionListener(this);

		monthLabel = new JLabel("Select Month");
		container.add(monthLabel);

		inputMonth = new JComboBox(months);

		container.add(inputMonth);
		inputMonth.setSelectedIndex(0);
		inputMonth.addActionListener(this);

		calcButton = new JButton("Press To Display Calendar");
		container.add(calcButton);
		calcButton.addActionListener(this);

		myTextArea = new JTextArea(10, 40);
		container.add(new JScrollPane(myTextArea));

		inputYear.setText("2008");

		outputFrame.setVisible(true);
	}

	public void actionPerformed(ActionEvent event)
	{
		if (event.getSource() == calcButton)
		{
			myTextArea.setText("");

			if (!YearInputIsValid()) return;

			int userYear = Integer.parseInt(inputYear.getText(), 10);

			myTextArea.append(isLeapYear(userYear) ? "Is a leap year.\n" : "Is not a leap year.\n");
			fillCalendar(userYear, inputMonth.getSelectedIndex());
			DisplayCalendar();
		}
	}

	private void DisplayCalendar()
	{
		for (int i = 0; i < calendar.length; i++)
		{
			for (int j = 0; j < calendar[i].length; j++)
				myTextArea.append(calendar[i][j] + " ");
			myTextArea.append("\n");
		}
	}

	private boolean YearInputIsValid()
	{

		if (inputYear.getText().equals(""))
		{
			myTextArea.append("Please enter a valid year value.\n");
			return false;
		}

		try
		{
			Integer.parseInt(inputYear.getText(), 10);
		}
		catch (Exception e)
		{
			myTextArea.append("The entered values must consist only of numbers,\n");
			return false;
		}

		if (Integer.parseInt(inputYear.getText(), 10) < 1899)
		{
			myTextArea.append("Year must be 1900 or after\n");
			return false;
		}
		return true;
	}

	private void InitialiseCalendar()
	{
		for (int i = 0; i < calendar.length; i++)
		{
			for (int j = 0; j < calendar[i].length; j++)
				calendar[i][j] = " ";
		}
	}

	private void fillCalendar(int userYear, int userMonth)
	{
		InitialiseCalendar();
		int xNum = (CalcFirstOfMonth(userYear, userMonth));

		int yNum = 0;

		int numDays = DaysInMonth[userMonth] + ((isLeapYear(userYear) && (userMonth == 1)) ? 1 : 0);
		int day = 1;
		do
		{
			calendar[yNum][xNum] = String.valueOf(day);
			xNum++;
			if (xNum > 6)
			{
				xNum = 0;
				yNum += 1;
			}
			day++;
		} while (day <= numDays);
	}

	private int CalcFirstOfMonth(int userYear, int userMonth)
	{
		int startDay;

		if ((userMonth < 0) || (userMonth > 11))
		{
			return (-1);
		}
		startDay = CalcJanuaryFirst(userYear);

		for (int i = 0; i < userMonth; i++)
		{
			startDay += DaysInMonth[i];
		}
		if ((userMonth > 1) && isLeapYear(userYear))
		{
			startDay++;
		}
		return (startDay % 7);
	}

	private int CalcJanuaryFirst(int userYear)
	{
		return ((5 + (userYear - 1582) + CalcLeapYears(userYear)) % 7);
	}

	private int CalcLeapYears(int userYear)
	{
		int leapYears;
		int hundreds;
		int fourHundreds;

		leapYears = (userYear - 1581) / 4;
		hundreds = (userYear - 1501) / 100;
		leapYears -= hundreds;
		fourHundreds = (userYear - 1201) / 400;
		leapYears += fourHundreds;
		return (leapYears);
	}

	private boolean isLeapYear(int userYear)
	{
		if ((userYear % 4 == 0) && (userYear % 100 != 0) || (userYear % 400 == 0))
		{
			return true;
		}
		return false;
	}
}
Shamelessly butchered a little bit from here:
CalendarTable

Hope you don't mind the changes to some of your code, I get carried away sometimes Maybe you can just take what you need to fix the original code.
__________________
moz is offline   Reply With Quote
Old 1st November 2008, 03:13 AM   #4 (permalink)
hyrev is dog tired
GTR Register Member
 
hyrev's Avatar
 
Join Date: Apr 2004
Location: Not in Japan anymore - :(
Cars owned: Top Secret built R34 GT-R
Posts: 4,234
moz, thank you. I will try it out in a bit. Domo!
__________________
hyrev is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump


All times are GMT. The time now is 04:25 AM.


Powered by vBulletin® Version 3.8.4
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
SEO by vBSEO 3.2.0
© 2001-2009 Cem Kocu