?
Solved

Repainting a panel...

Posted on 1998-02-09
9
Medium Priority
?
397 Views
Last Modified: 2010-04-16
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?
0
Comment
Question by:bjg
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
9 Comments
 
LVL 16

Expert Comment

by:imladris
ID: 1232948
You need to call invalidate on the main panel. It is part of class Component:

public void invalidate();

0
 

Author Comment

by:bjg
ID: 1232949
It didn't work?  Where, when, and why should I call invalidate()?
0
 
LVL 16

Expert Comment

by:imladris
ID: 1232950
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".

0
Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

 

Author Comment

by:bjg
ID: 1232951
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].setActionCommand("day");
                        daycells[i].addActionListener(this);
                        if ( compareDate(daycells[i].getDate(), today) )
                        {
                              daycells[i].setBackground(Color.black);
                              daycells[i].setForeground(Color.green);
                              daycells[i].requestFocus();
                        }
                        canvas.add(daycells[i]);
                  } else
                  {
                        Panel blank = new Panel();
                        blank.setBackground(Color.white);
                        canvas.add(blank);
                  }
            }
            canvas.invalidate();
      }

0
 
LVL 16

Expert Comment

by:imladris
ID: 1232952
That looks fine. The only other suggestion I can think of making is a call to repaint, i.e.

canvas.invalidate();
canvas.repaint();

0
 

Author Comment

by:bjg
ID: 1232953
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...
0
 
LVL 2

Expert Comment

by:mgk
ID: 1232954
Try calling validate() instead of invalidate().
0
 

Accepted Solution

by:
rans earned 200 total points
ID: 1232955

what I've done in my program was :

canvas.ivalidate();
repaint();

which means that you should not repaint canvas but the container
in which the canvas sits.
0
 

Author Comment

by:bjg
ID: 1232956
I put:

canvas.validate();
repaint();

this worked.  Thanks.
0

Featured Post

Get real performance insights from real users

Key features:
- Total Pages Views and Load times
- Top Pages Viewed and Load Times
- Real Time Site Page Build Performance
- Users’ Browser and Platform Performance
- Geographic User Breakdown
- And more

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
Suggested Courses
Course of the Month10 days, 12 hours left to enroll

764 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question