Witam mam problem z uzyskaniem kolorowego przycisku z aktualnym dniem w kalendarzu. Próbuje na różne sposoby uzyskać ten efekt ale nic z tego nie wychodzi.
Myślę,że coś tak mniej wiecej to powinno wygladac.
if (button[x]!=null && mies==month && rok==year && Integer.parseInt(button[x].toString())==today) {
button[x].setBackground(Color.blue);
}
Cały kod pliku klasy poniżej, pliku main nie wklejam bo on za to nie odpowiada.
package lars;
import java.util.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
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 && day==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());
}
}