1

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?

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
hensing1
  • 187
  • 13
  • 1) For better help sooner, post a [MCVE] or [Short, Self Contained, Correct Example](http://www.sscce.org/). 2) Why use AWT? See [this answer](http://stackoverflow.com/questions/6255106/java-gui-listeners-without-awt/6255978#6255978) for many good reasons to abandon AWT components in favor of Swing. 3) *"A single month is drawn onto a panel with a 7*7 BorderLayout."* Which month has 49 days? 4) **I would not create an entire new panel, but simply update the state of the existing panel. Creating new components after the GUI is visible requires some special tricks, & is usually unnecessary.** – Andrew Thompson Oct 26 '17 at 03:03
  • The weekday labels are drawn onto the panel as well, hence the 7*7 Layout. – hensing1 Nov 06 '17 at 09:00

0 Answers0