I'm writing a small Calendar application which lets my manage important dates and events of my study. I'm working with java.awt.
A single month is drawn onto a panel with a 7*7 BorderLayout. When the program is started, the panel is drawn with the following method:
public void drawCalendarP(String dateS){
if(mf != null && calendarP != null){
mf.remove(calendarP);
calendarP.removeAll();
}
calendarP = new Panel();
calendarP.setLayout(new GridLayout(7,7,10,10));
//...
day[0] = new Button("1");
day[0].addActionListener(...);
day[0].setVisible(true);
calendarP.add(day[0]);
/*each day of the month is represented by a button. I've put the
initialization of the 'Day-buttons' in its own method which is called
frome here, but this is basically what it does.*/
//...
mainframe.add(calendarP);
}
}
Now, when I start the program and this method is firstly called, everything works just as intended. However, I included a button which lets you switch between individual months, and this is where the problem arises.
Because in order to draw a new month, I have decided to just call the method drawCalenderP()
again, but with a different String dateS
. And again, if you put this String in manually before the start of the program, it will happily draw you any month you want.
However, if I call the method again with said button, it will do the following:
- It will re-draw the panel, along with its background and everything (as intended)
- It will re-initialize all the components for that panel (as intended)
- But it wont draw the new components back onto the panel (not as intended)
I have no idea why that happens. It should add the new components, just the way it does when you call the method the first time, but the components just won't show up on the panel.
The thing is; if I draw the button without the help of the GridLayout, let's say with setSize(w,x)
and setLocation(y,z)
, everything will work just fine again. So the problem lies somewhere within the GridLayout, but I just can't figure it out.
Is there a way to do this without scrapping the entire layout-thing and doing the positioning of the components manually?