revalidate vs repaint in Java Swing

0 votes

Can someone tell me which one do I use revalidate() or repaint(), if I want to replace the contents of a JPanel.

I tried using removeAll(), then adding my new content, then calling revalidate(). But it's not working. However, If I add a call to repaint() in addition to revalidate(), then it works fine. It's confusing me. Can someone help?

Sep 20, 2018 in Java by anto.trigg4
• 3,440 points
10,119 views

1 answer to this question.

0 votes

You need to call repaint() and revalidate() both in order to replace all the elements from your JPanel.

  • repaint(): This method tells Swing that an area of the window is dirty.
  • revalidate(): This method tells the layout manager to recalculate the layout that is necessary when adding components. 

Due to this, the children of the panel are repainted.

Hope it helps!!

If not then its recommended to join our Java training class and learn about Java in detail.

Thank You!!

answered Sep 20, 2018 by code.reaper12
• 3,500 points

Related Questions In Java

+1 vote
1 answer

concat() vs “+” operator : In Java for String concatenation

Basically, there are two important differences between ...READ MORE

answered Apr 27, 2018 in Java by Rishabh
• 3,620 points
4,442 views
+1 vote
1 answer

Performance difference of if/else vs switch statement in Java

The thing you are worried about is ...READ MORE

answered Jul 26, 2018 in Java by geek.erkami
• 2,680 points
3,337 views
0 votes
1 answer

How to create text labels in Swing, Java?

In the frame, you can use the ...READ MORE

answered Aug 27, 2019 in Java by Jimmy
767 views
0 votes
1 answer

Create Buttons in Java Swing

First, you will have to create a ...READ MORE

answered Aug 27, 2019 in Java by Esha
884 views
0 votes
1 answer

Fetching screen resolution using Java

You can fetch the screen resolution using the Toolkit.getScreenSize() method ...READ MORE

answered Sep 20, 2018 in Java by anto.trigg4
• 3,440 points
4,278 views
0 votes
1 answer

How to change selection listener in JComboBox?

It should respond to ActionListeners, like this: combo.addActionListener (new ...READ MORE

answered Nov 15, 2018 in Java by Frankie
• 9,830 points
864 views
0 votes
1 answer

Describe Heavy Weight Components Mean In Java Programming?

Heavy weight components like Abstract Window Toolkit ...READ MORE

answered Nov 28, 2018 in Java by Frankie
• 9,830 points
1,535 views
0 votes
1 answer

First panel added to a frame disappears

The default layout of a JFrame (or more specifically ...READ MORE

answered Mar 1, 2019 in Java by developer_1
• 3,320 points
1,157 views
0 votes
1 answer

Comparable vs Comparator in Java

I think you are not alone who is ...READ MORE

answered May 11, 2018 in Java by code.reaper12
• 3,500 points
859 views
0 votes
1 answer

notify() vs. notifyAll() in Java

notify() is used to wake any thread in the wait ...READ MORE

answered Sep 20, 2018 in Java by code.reaper12
• 3,500 points
1,736 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP