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 19, 2018 in Java by anto.trigg4
• 3,420 points
602 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 this helps!

answered Sep 19, 2018 by code.reaper12
• 3,450 points

Related Questions In Java

0 votes
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,540 points
924 views
0 votes
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,320 points
402 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 in Java by Jimmy
16 views
0 votes
1 answer

Create Buttons in Java Swing

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

answered Aug 27 in Java by Esha
10 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,420 points
521 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,810 points
66 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,810 points
40 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 in Java by developer_1
• 3,300 points
27 views
0 votes
1 answer

Comparable vs Comparator in Java

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

answered May 10, 2018 in Java by code.reaper12
• 3,450 points
125 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,450 points
79 views