Results 1 to 6 of 6
Thread: Current Day in Calendar
- 12-14-2011, 09:13 PM #1
Member
- Join Date
- Nov 2011
- Posts
- 8
- Rep Power
- 0
Current Day in Calendar
I have a problem with setting a colorful background in "current day of month" button in Calendar. I used many ways to do this but the result is always the same. It's really irritating. I tried to get an effect by this code below:
Java Code:GregorianCalendar cal = new GregorianCalendar(year, month, 1); int dayOfWeek = cal.get(java.util.GregorianCalendar.DAY_OF_WEEK); int today = cal.get(java.util.GregorianCalendar.DAY_OF_MONTH); int mies = month; int rok = year; int daysInMonth = cal.getActualMaximum(java.util.GregorianCalendar.DAY_OF_MONTH); for (int x = 6 + dayOfWeek, day = 1; day <= daysInMonth; x++, day++) { button[x].setText("" + day); if (button[x]!=null && mies==month && rok==year && Integer.parseInt(button[x].toString())==today) { button[x].setBackground(Color.blue); }}
But there is something wrong. I also tried many many other ways but i cant get what i want - colorful current day button. Could someone give me the solution?
Full class code:
Java Code:class DatePicker { GregorianCalendar cal = new GregorianCalendar(); int month = cal.get(GregorianCalendar.MONTH); int da = cal.get(java.util.GregorianCalendar.DAY_OF_MONTH); int year = java.util.GregorianCalendar.getInstance().get(java.util.GregorianCalendar.YEAR);; JLabel l = new JLabel("", JLabel.CENTER); String day = ""; JDialog d; JButton[] button = new JButton[49]; public DatePicker(JFrame parent) { d = new JDialog(); d.setModal(true); String[] header = { "Sun", "Mon", "Tue", "Wed", "Thur", "Fri", "Sat" }; JPanel p1 = new JPanel(new GridLayout(7, 7)); p1.setPreferredSize(new Dimension(430, 120)); for (int x = 0; x < button.length; x++) { final int selection = x; button[x] = new JButton(); button[x].setFocusPainted(false); button[x].setBackground(Color.white); if (x > 6) { button[x].addActionListener(new ActionListener() { public void actionPerformed(ActionEvent ae) { day = button[selection].getActionCommand(); d.dispose(); } }); } if (x < 7) { button[x].setText(header[x]); button[x].setForeground(Color.red); } p1.add(button[x]); } JPanel p2 = new JPanel(new GridLayout(1, 3)); // Previous month button JButton previous = new JButton("<< Previous"); previous.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent ae) { month--; displayDate(); } }); p2.add(previous); // Current month label between the previous and next buttons p2.add(l); // Next month button JButton next = new JButton("Next >>"); next.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent ae) { month++; displayDate(); } }); p2.add(next); d.add(p1, BorderLayout.CENTER); d.add(p2, BorderLayout.SOUTH); d.pack(); d.setLocationRelativeTo(parent); displayDate(); d.setVisible(true); } public void displayDate() { for (int x = 7; x < button.length; x++) { button[x].setText(""); } java.text.SimpleDateFormat sdf = new java.text.SimpleDateFormat("MMMM yyyy"); GregorianCalendar cal = new GregorianCalendar(year, month, 1); int dayOfWeek = cal.get(java.util.GregorianCalendar.DAY_OF_WEEK); int today = cal.get(java.util.GregorianCalendar.DAY_OF_MONTH); int mies = month; int rok = year; int daysInMonth = cal.getActualMaximum(java.util.GregorianCalendar.DAY_OF_MONTH); for (int x = 6 + dayOfWeek, day = 1; day <= daysInMonth; x++, day++) { button[x].setText("" + day); if (button[x]!=null && mies==month && rok==year && Integer.parseInt(button[x].toString())==today) { button[x].setBackground(Color.blue); }} l.setText(sdf.format(cal.getTime())); d.setTitle("Date Picker"); } public String setPickedDate() { if (day.equals("")) { return day; } // Set the return date format java.text.SimpleDateFormat sdf = new java.text.SimpleDateFormat("dd-MM-yyyy"); GregorianCalendar cal = new GregorianCalendar(); cal.set(year, month, Integer.parseInt(day)); return sdf.format(cal.getTime()); } }
- 12-14-2011, 09:42 PM #2
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,546
- Rep Power
- 11
Re: Current Day in Calendar
So, how do you run that code? And what happens when you run it?i cant get what i want - colorful current day button
- 12-14-2011, 09:53 PM #3
Member
- Join Date
- Nov 2011
- Posts
- 8
- Rep Power
- 0
Re: Current Day in Calendar
I have Main File also. When it runs i have normal calendar and everything is ok, i can change month/year by next/prev buttons . Every day button is created thanks to array button[], but when i want to make "today button" background colorful, then nothing happens. I'm sure that my "IF instruction" isnt correct but i dont know what instruction I should write to have e.g blue "today button".
Last edited by Marcin; 12-14-2011 at 09:56 PM.
- 12-14-2011, 09:57 PM #4
Re: Current Day in Calendar
Is this thread the same question as: How to set current day (today) background color in Calendar?
- 12-14-2011, 10:13 PM #5
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,546
- Rep Power
- 11
Re: Current Day in Calendar
I'll leave the discussion to the other thread.
@OP: You were asked there how the program was being run. It's the same deal if you ask again, "I have a Main File" isn't enough. We really need a precise idea of how this code is being run.
I suggest you post your latest effort there.
But to get the best help describe things precisely and accurately. It just isn't true that "nothing happens" when you run this code. Something rather important happens and it might be the starting point for your finding a solution to the problem.Java Code:if (button[x]!=null && mies==month && rok==year && Integer.parseInt(button[x].toString())==today) { button[x].setBackground(Color.blue); }}Last edited by pbrockway2; 12-14-2011 at 10:16 PM.
- 12-14-2011, 10:16 PM #6
Similar Threads
-
How to set current day (today) background color in Calendar?
By Marcin in forum New To JavaReplies: 6Last Post: 12-12-2011, 01:23 AM -
Calendar now = Calendar.getInstance()
By volkvanmyn25 in forum New To JavaReplies: 3Last Post: 10-31-2011, 10:25 PM -
question about method calendar.get(Calendar.DAY_OF_WEEK))
By elenora in forum Advanced JavaReplies: 4Last Post: 09-21-2011, 04:39 PM -
Applet - get current URL
By Dennis in forum Advanced JavaReplies: 4Last Post: 06-13-2010, 01:26 PM -
calendar with week numbers of a current month
By sridharnr in forum Java ServletReplies: 1Last Post: 03-27-2009, 11:02 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks