• 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
  • paul wheaton
  • Liutauras Vilda
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Scott Selikoff
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
  • Frits Walraven
Bartenders:
  • Stephan van Hulst
  • Carey Brown

JPanel refresh problem - new content shows only on mouse action

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello!

I'm trying to change JPanel content after clicking some button in a JFrame.

I use following method to change my panel content:




I used this method in my previous projects and it worked perfectly, but now something strange is happening.
The panel looks as it was empty after clicking the button which should cause the content change. But when I move my mouse over the panel
- the new content (buttons, tables, etc.) is showing (when I move my mouse over the place where the button shoud be, it immediately appears).

I tried revalidate() and repaint() methods but they don't make any difference.

Does anybody know what can possibly cause this effect?
 
Bartender
Posts: 1104
10
Netbeans IDE Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Joan,
Welcome to the Ranch

Why do you want to remove and add panel? There is no need to do that for content change.
Content changes in Swing are be done through the model (which holds the data) and that change will automatically notify the view which can update itself.

Let us know what content is changing in the panel.
 
Joan Krammer
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello!

Maybe I used wrong words to describe what is changing. I have a main window (JFrame) and a panel in it (JPanel).
I also have some options to choose like f.e. books, toys, etc. Each option has its own panel (separate JPanel classes) with the table and buttons.
After choosing some option currentPanel (which is the current panel in main window) shoud change to new panel (f.e books panel, toys panel, etc.).

As I said, it worked in my previous projects that's why I'm so confused right now. It looks like the new panel is refreshing only when I move my mouse over the element of the new panel.
 
Bartender
Posts: 3323
86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Rather than trying to swap panels in and out I suggest you look at using java.awt.CardLayout.
 
Joan Krammer
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Tony Docherty wrote:Rather than trying to swap panels in and out I suggest you look at using java.awt.CardLayout.



I used CardLayout as you suggested, but the problem stays the same. The content of the panel appears only when I move my mouse over it.
So it's not the problem of the layout...
 
Tony Docherty
Bartender
Posts: 3323
86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can you show the code you have used.
 
Joan Krammer
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Tony Docherty wrote:Can you show the code you have used.







I really appreciate your help.
 
Tony Docherty
Bartender
Posts: 3323
86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The method add(String, Component) is obsolete, you should use add(Component, Object). ie:
 
Joan Krammer
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Tony Docherty wrote:The method add(String, Component) is obsolete, you should use add(Component, Object). ie:




Hi again!

I changed the code again just as you suggested but the problem remains the same. The items of my panel appear only when I move my mouse over it.

What can cause this effect? I don't have any mouse listeners added in my project...
 
Tony Docherty
Bartender
Posts: 3323
86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You'll need to post an SSCCE so I can run the code and see what is going on.
 
Good heavens! What have you done! Here, try to fix it with this tiny ad:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic