• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

Problem with Graphics and Screen Flicker when setting size of jpanel within jframe

 
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello,

I have jframe and am invoking a jpanel within it.  I am using setSize and setBounds.  When I invoke the jpanel (caseNewPanel), the screen and graphics flicker.  It is not a smooth transition between the smaller size of the jframe and the larger size of the jpanel.  Here is a snippet of my code:

private void jMenuItem_caseOpenActionPerformed(java.awt.event.ActionEvent evt) {                                                  
       caseNewPanel = new CaseNewPanel();
       this.setContentPane(caseNewPanel);
       this.setTitle("Case Menu");
       this.setSize(1183, 908);
       this.setBounds(5, 5, 1183, 908);
       this.invalidate(); this.validate();
       this.repaint();

I am new to this, so any help would be much appreciated.  Thank you.
 
Saloon Keeper
Posts: 10930
87
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Johnny Stuart wrote:


A few comments:
  • Call setSize() or setBounds(), not both.
  • Calling setSize() or setBounds() already invalidates the container.
  • I don't think you need validate() but I might be wrong.
  • Don't use the keyword "this" in this instance.
  • When this ActionPerformed() medthod is called, are you really changing the size?
  •  
    Sheriff
    Posts: 28333
    97
    Eclipse IDE Firefox Browser MySQL Database
    • Likes 1
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    I'm not surprised you get flickering, since you're actually creating a new component and inserting it (I think) into the GUI. I can see how this would lead to major redrawing. Couldn't you just use the existing component?
     
    Paul Clapham
    Sheriff
    Posts: 28333
    97
    Eclipse IDE Firefox Browser MySQL Database
    • Likes 1
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    And, welcome to the Ranch!
     
    Johnny Stuart
    Ranch Hand
    Posts: 30
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Basically, I have several .java JPanel forms and I want to invoke them through a custom menu bar I made on the main JFrame.  I thought I had to invoke a new instance of them because I tried cardlayout method and it would not show the JPanels in the JFrame.  I am not sure why.  I want to ensure I maintain the menu bar at the top of the JFrame in each JPanel I invoke.  How do I invoke them without creating a new instance.  Here is some additional code from the main frame:

     
    Johnny Stuart
    Ranch Hand
    Posts: 30
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Oh, and I will call setbounds instead of calling both setsize and setbounds......when I rewrite.
     
    Paul Clapham
    Sheriff
    Posts: 28333
    97
    Eclipse IDE Firefox Browser MySQL Database
    • Likes 1
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Johnny Stuart wrote:I tried cardlayout method and it would not show the JPanels in the JFrame.  I am not sure why.



    It sounds to me like CardLayout would be the right thing to use. So I would suggest experimenting with CardLayout a bit more until you get it working. Here's a link to the CardLayout tutorial: How to Use CardLayout.

    But don't try to use your existing code for that experimenting. There's way too much irrelevant stuff in it, and it's just going to get in the way. So put that code aside and start afresh with a small program. You'll only need two JPanel objects to make sure you know how to alternate between displaying the two of them. Once you get the idea of how to work with CardLayout, then go back to your existing code and modify it to use a CardLayout.
     
    Johnny Stuart
    Ranch Hand
    Posts: 30
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Ok, thank you for the suggestion.
     
    rubbery bacon. rubbery tiny ad:
    Gift giving made easy with the permaculture playing cards
    https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
    reply
      Bookmark Topic Watch Topic
    • New Topic