Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 198
  • Last Modified:

Panel contents not showing until clicked on

Hi,

An easy one for somebody, this must be a common problem.  What am I doing wrong if the contents of my JPanels within a JFrame do not show up unless you click on where they should be, or cover them up and then reveal them?  Must be something to do with the OS calling paint, but I can't figure it out.  Can post parts of my code if required, but it is quite large and sprawling.

Cheers, Steve
0
steve_bagnall
Asked:
steve_bagnall
  • 8
  • 7
  • 7
2 Solutions
 
zzynxSoftware engineerCommented:
>> What am I doing wrong if the contents of my JPanels within a JFrame do not show up
I think a

yourFrame.getContentPane().validate();

should help

0
 
CEHJCommented:
It suggests that part of your program is blocking the event dispatch thread
0
 
zzynxSoftware engineerCommented:
Or what I often use:

JPanel mainPanel = JPanel();
... // Add whatever you need to add to mainPanel
getContentPane().add(mainPanel);

mainPanel.revalidate();
mainPanel.repaint();
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
zzynxSoftware engineerCommented:
>> part of your program is blocking the event dispatch thread
and de-blocking when the mouse is pressed?
0
 
CEHJCommented:
Also, as a rough rule, if you are using anything other than a JFrame, you should be overriding paintComponent, not paint
0
 
steve_bagnallAuthor Commented:
Maybe not as simple as I though then.  The SOUTH panel shows up, but the WEST and CENTER one don't.  One of them displays a treeNode, and the other inherits from JPanel and uses Graphics2D to render some drawings if that helps.
0
 
steve_bagnallAuthor Commented:
Sorry that last message coresponds to the first response only .. I will check out the others
0
 
CEHJCommented:
>>and the other inherits from JPanel

Are you overriding paintComponent, not paint?
0
 
zzynxSoftware engineerCommented:
>> other inherits from JPanel and uses Graphics2D to render some drawings
and - as CEHJ said - should overwrite paintComponent() not paint(). Is that the case?
0
 
steve_bagnallAuthor Commented:
zzynx,

Your mainPanel suggestion doesn't seem to solve my problem.

Some further information:  Although the SOUTH JPanel is visible if you cover it up with another window, when you reveal it again is is not visible.

Also, when I said that the contents of the JPanels appeared if you covered them up and revealed them I think that must be either very intermitent, or not the case as they stay hidden when revealed now.

The only way to get them to display is to expand a node in the tree in the WEST panel, or select some text in the SOUTH panel.

I will look at the other suggestions
0
 
steve_bagnallAuthor Commented:
CEHJ, Yes I am overriding the paintComponent() method.
0
 
CEHJCommented:
Also, please post the overridden method where you use Graphics2D
0
 
steve_bagnallAuthor Commented:
protected void paintComponent(Graphics g)
      {
            super.paintComponent(g);
            Graphics2D g2 = (Graphics2D) g;
            g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);

            drawImage(g2);


      } // end of constructor

0
 
steve_bagnallAuthor Commented:
ignore reference to constructor ;)
0
 
CEHJCommented:
>> protected void paintComponent(Graphics g)

should be

public void paintComponent(Graphics g)
0
 
CEHJCommented:
>>drawImage(g2);

You need to post that method too
0
 
steve_bagnallAuthor Commented:
Changing to public hasn't worked .. here's the drawImage(g2) method, along with pickFont method which it calls:

protected FontMetrics pickFont(Graphics2D g2,
                                     String longString,
                                     int xSpace) {
            boolean fontFits = false;
            Font font = g2.getFont();
            FontMetrics fontMetrics = g2.getFontMetrics();
            int size = font.getSize();
            String name = font.getName();
            int style = font.getStyle();

            while ( !fontFits ) {
                  if ( (fontMetrics.getHeight() <= maxCharHeight)
                         && (fontMetrics.stringWidth(longString) <= xSpace) ) {
                        fontFits = true;
                  }
                  else {
                        if ( size <= minFontSize ) {
                              fontFits = true;
                        }
                        else {
                              g2.setFont(font = new Font(name,
                                                                     style,
                                                                     --size));
                              fontMetrics = g2.getFontMetrics();
                        }
                  }
            }

            return fontMetrics;
      } // end of pickFont()







private void drawImage(Graphics2D g2) {

            int gridWidth = d.width / 6;
            int gridHeight = d.height / 2;

            fontMetrics = pickFont(g2, "Filled and Stroked GeneralPath",
                                             gridWidth);

            Color fg3D = Color.lightGray;

            g2.setPaint(fg3D);
            g2.draw3DRect(0, 0, d.width - 1, d.height - 1, true);
            g2.draw3DRect(3, 3, d.width - 7, d.height - 7, false);
            g2.setPaint(fg);

            int x = 5;
            int y = 7;
            int rectWidth = gridWidth - 2*x;
            int stringY = gridHeight - 3 - fontMetrics.getDescent();
            int rectHeight = stringY - fontMetrics.getMaxAscent() - y - 2;

            // draw Line2D.Double
            g2.draw(new Line2D.Double(x, y+rectHeight-1, x + rectWidth, y));
            g2.drawString("Line2D", x, stringY);
            x += gridWidth;

            // draw Rectangle2D.Double
            g2.setStroke(stroke);
            g2.draw(new Rectangle2D.Double(x, y, rectWidth, rectHeight));
            g2.drawString("Rectangle2D", x, stringY);
            x += gridWidth;

            // draw  RoundRectangle2D.Double
            g2.setStroke(dashed);
            g2.draw(new RoundRectangle2D.Double(x, y, rectWidth,
                                                                  rectHeight, 10, 10));
            g2.drawString("RoundRectangle2D", x, stringY);
            x += gridWidth;

            // draw Arc2D.Double
            g2.setStroke(wideStroke);
            g2.draw(new Arc2D.Double(x, y, rectWidth, rectHeight, 90,
                                                 135, Arc2D.OPEN));
            g2.drawString("Arc2D", x, stringY);
            x += gridWidth;

            // draw Ellipse2D.Double
            g2.setStroke(stroke);
            g2.draw(new Ellipse2D.Double(x, y, rectWidth, rectHeight));
            g2.drawString("Ellipse2D", x, stringY);
            x += gridWidth;

            // draw GeneralPath (polygon)
            int x1Points[] = {x, x+rectWidth, x, x+rectWidth};
            int y1Points[] = {y, y+rectHeight, y+rectHeight, y};
            GeneralPath polygon = new GeneralPath(GeneralPath.WIND_EVEN_ODD,
                                                                    x1Points.length);
            polygon.moveTo(x1Points[0], y1Points[0]);
            for ( int index = 1; index < x1Points.length; index++ ) {
                  polygon.lineTo(x1Points[index], y1Points[index]);
            };
            polygon.closePath();

            g2.draw(polygon);
            g2.drawString("GeneralPath", x, stringY);

            // NEW ROW
            x = 5;
            y += gridHeight;
            stringY += gridHeight;

            // draw GeneralPath (polyline)

            int x2Points[] = {x, x+rectWidth, x, x+rectWidth};
            int y2Points[] = {y, y+rectHeight, y+rectHeight, y};
            GeneralPath polyline = new GeneralPath(GeneralPath.WIND_EVEN_ODD,
                                                                     x2Points.length);
            polyline.moveTo (x2Points[0], y2Points[0]);
            for ( int index = 1; index < x2Points.length; index++ ) {
                  polyline.lineTo(x2Points[index], y2Points[index]);
            };

            g2.draw(polyline);
            g2.drawString("GeneralPath (open)", x, stringY);
            x += gridWidth;

            // fill Rectangle2D.Double (red)
            g2.setPaint(red);
            g2.fill(new Rectangle2D.Double(x, y, rectWidth, rectHeight));
            g2.setPaint(fg);
            g2.drawString("Filled Rectangle2D", x, stringY);
            x += gridWidth;

            // fill RoundRectangle2D.Double
            GradientPaint redtowhite = new GradientPaint(x,y,red,x+rectWidth, y,white);
            g2.setPaint(redtowhite);
            g2.fill(new RoundRectangle2D.Double(x, y, rectWidth,
                                                                  rectHeight, 10, 10));
            g2.setPaint(fg);
            g2.drawString("Filled RoundRectangle2D", x, stringY);
            x += gridWidth;

            // fill Arc2D
            g2.setPaint(red);
            g2.fill(new Arc2D.Double(x, y, rectWidth, rectHeight, 90,
                                                 135, Arc2D.OPEN));
            g2.setPaint(fg);
            g2.drawString("Filled Arc2D", x, stringY);
            x += gridWidth;

            // fill Ellipse2D.Double
            redtowhite = new GradientPaint(x,y,red,x+rectWidth, y,white);
            g2.setPaint(redtowhite);
            g2.fill (new Ellipse2D.Double(x, y, rectWidth, rectHeight));
            g2.setPaint(fg);
            g2.drawString("Filled Ellipse2D", x, stringY);
            x += gridWidth;



            // fill and stroke GeneralPath
            int x3Points[] = {x, x+rectWidth, x, x+rectWidth};
            int y3Points[] = {y, y+rectHeight, y+rectHeight, y};
            GeneralPath filledPolygon = new GeneralPath(GeneralPath.WIND_EVEN_ODD,
                                                                              x3Points.length);
            filledPolygon.moveTo(x3Points[0], y3Points[0]);
            for ( int index = 1; index < x3Points.length; index++ ) {
                  filledPolygon.lineTo(x3Points[index], y3Points[index]);
            };
            filledPolygon.closePath();
            g2.setPaint(red);
            g2.fill(filledPolygon);
            g2.setPaint(fg);
            g2.draw(filledPolygon);
            g2.drawString("Filled and Stroked GeneralPath", x, stringY);


      } // end of drawImage
0
 
zzynxSoftware engineerCommented:
>> >> protected void paintComponent(Graphics g)
>> should be
>> public void paintComponent(Graphics g)
Why is that, CEHJ?
0
 
steve_bagnallAuthor Commented:
I'm glad I'm not within punching range of you 2 ... I'd overidden the paint() method in the JFrame with no implementation!  Sorry :)
0
 
zzynxSoftware engineerCommented:
Why did you close this?
Have you found it?
0
 
zzynxSoftware engineerCommented:
>> I'd overidden the paint() method in the JFrame with no implementation!  Sorry :)
I see
It happens to the best ;°)
0
 
CEHJCommented:
:-)

>>Why is that, CEHJ?

Ignore that - i thought that method was public. Of course if it had been, it wouldn't have compiled anyway ;-)
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

  • 8
  • 7
  • 7
Tackle projects and never again get stuck behind a technical roadblock.
Join Now