bjg
asked on
Repainting a panel...
I am having a problem with repainting a panel. I have a main panel, and I add other panels onto it. Depending on which selection a user chooses for how many other panels they want added to it, I need to repaint the main panel with the correct number of panels added to it. I have a Choice component, and they select which number of panels to add, then I have a button which gets the selected number of panels to add, and clears the main panel with mainpanel.removeAll() (from Container), and then I add the new amount of panels. It works fine except I don't see the new panel unless I resize the frame or applet first. What is the problem and how can I correct it?
ASKER
It didn't work? Where, when, and why should I call invalidate()?
After the mainpanel.removeAll(), and the addition of the new panels it would be:
mainpanel.invalidate();
This will cause the panels layout manager to be called again which will cause all (new, in this case) subcomponents to be "positioned".
mainpanel.invalidate();
This will cause the panels layout manager to be called again which will cause all (new, in this case) subcomponents to be "positioned".
ASKER
This is the code I have and it didn't work, the new settings only show up after I resize the applet in the appletviewer.
public void setCalendar()
{
currentDate.set(year, month, date);
/*======================== ========== ========== ========== ==
Find out what day of the week the first of the month
falls on.
========================== ========== ========== ========== */
Calendar first = Calendar.getInstance();
first.set(year, month, 1); //Set to first day of month
int first_day = first.get(Calendar.DAY_OF_ WEEK) - 1;
/*======================== ========== ========== ========== ==
Determine total number of days in month, also check for
leap years.
========================== ========== ========== ========== */
int totalDays = 0;
if ((month == FEBRUARY) && (isLeapYear(year)))
totalDays = LEAP_YEAR_DAYS;
else
totalDays = monthDays[month];
/*======== Allocate correct number of DayCells for the month ======*/
DayCell daycells[] = new DayCell[totalDays];
canvas.removeAll();
/*======== Add blank cells until reach first of the month ==========*/
for (int j = 0; j < first_day; j++)
{
Panel blank = new Panel();
blank.setBackground(Color. white);
canvas.add(blank);
}
for (int i = 0; i < 42 - first_day; i++)
{
if (i < totalDays)
{
Calendar temp = Calendar.getInstance();
temp.set(year, month, i+1);
daycells[i] = new DayCell(temp);
daycells[i].setActionComma nd("day");
daycells[i].addActionListe ner(this);
if ( compareDate(daycells[i].ge tDate(), today) )
{
daycells[i].setBackground( Color.blac k);
daycells[i].setForeground( Color.gree n);
daycells[i].requestFocus() ;
}
canvas.add(daycells[i]);
} else
{
Panel blank = new Panel();
blank.setBackground(Color. white);
canvas.add(blank);
}
}
canvas.invalidate();
}
public void setCalendar()
{
currentDate.set(year, month, date);
/*========================
Find out what day of the week the first of the month
falls on.
==========================
Calendar first = Calendar.getInstance();
first.set(year, month, 1); //Set to first day of month
int first_day = first.get(Calendar.DAY_OF_
/*========================
Determine total number of days in month, also check for
leap years.
==========================
int totalDays = 0;
if ((month == FEBRUARY) && (isLeapYear(year)))
totalDays = LEAP_YEAR_DAYS;
else
totalDays = monthDays[month];
/*======== Allocate correct number of DayCells for the month ======*/
DayCell daycells[] = new DayCell[totalDays];
canvas.removeAll();
/*======== Add blank cells until reach first of the month ==========*/
for (int j = 0; j < first_day; j++)
{
Panel blank = new Panel();
blank.setBackground(Color.
canvas.add(blank);
}
for (int i = 0; i < 42 - first_day; i++)
{
if (i < totalDays)
{
Calendar temp = Calendar.getInstance();
temp.set(year, month, i+1);
daycells[i] = new DayCell(temp);
daycells[i].setActionComma
daycells[i].addActionListe
if ( compareDate(daycells[i].ge
{
daycells[i].setBackground(
daycells[i].setForeground(
daycells[i].requestFocus()
}
canvas.add(daycells[i]);
} else
{
Panel blank = new Panel();
blank.setBackground(Color.
canvas.add(blank);
}
}
canvas.invalidate();
}
That looks fine. The only other suggestion I can think of making is a call to repaint, i.e.
canvas.invalidate();
canvas.repaint();
canvas.invalidate();
canvas.repaint();
ASKER
I tried that, and it doesn't work either. I can't figure it out, its stupid that it will only repaint if I resize it...
Try calling validate() instead of invalidate().
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
I put:
canvas.validate();
repaint();
this worked. Thanks.
canvas.validate();
repaint();
this worked. Thanks.
public void invalidate();