JComponent problem
I am trying to create a custom calendar in Java. For this I am expanding javax.swing.JComponent
.
public class GMCalendar extends JComponent { ... }
In my constructor, I do basic setup and load an image:
calendarDay = Toolkit.getDefaultToolkit().createImage(getClass().getResource("/resources/images/calendar_day.png"));
And in paintComponent
he has to draw my calendar, but he will not draw anything that looks very strange.
public void paintComponent(Graphics g) {
super.paintComponent(g);
Calendar calendar = Calendar.getInstance();
calendar.add(Calendar.MONTH, currentMonth);
int numberOfWeeks = calendar.getActualMaximum(Calendar.WEEK_OF_MONTH);
for (int i = 0; i < 7; i++) {
for (int j = 0; j < numberOfWeeks; j++) {
int x = i * (DAY_SIZE + DAY_SPACE);
int y = j * (DAY_SIZE + DAY_SPACE);
g.drawImage(calendarDay, x, y, null);
}
}
}
The result is nothing or something like this:
It seems to paintComponent
get called before the component size is greater than 1x1.
This is in my main()
:
GMContainerFrame cf = new GMContainerFrame();
cf.setMinimumSize(new Dimension(800,600));
cf.setVisible(true);
This is from the constructor GMContainerFrame
(which it doesn't use LayoutManager
!):
calendarFrame = new GMMiniFrame("Kalender", new GMCalendar(), 230);
GMMiniFrame
extends JSplitPane
.
source to share
-
do not reinvent the wheel, use
JPanels
, better withJLabels
, installedGridLayout
-
using
JLabels
(JPanel
nested multipleJComponents
) there is no reason forpaintComponents
-
The notification
JLabel
is transparent, non_opaque, -
use
JCalendar
/JDatePicker
fromSwingX
, my favorite JCalendar by Kai Toedter (no problem with renderer, editor, special days, min and max date)
source to share