Results 1 to 7 of 7
  1. #1
    guadalcanal is offline Member
    Join Date
    Sep 2014
    Posts
    6
    Rep Power
    0

    Default JPanel not repainting

    Hello again!
    I've got another layout related problem, this is my first time doing a lot of work with them.

    This time the issue is that my JPanel (EditorPanel()) only repaints when it's in a BorderLayout. I made an SSCCE to illustrate the problem:
    Java Code:
    import java.awt.BorderLayout;
    import java.awt.Color;
    import java.awt.GridBagLayout;
    
    import javax.swing.BoxLayout;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    
    
    public class Main {
    	static JFrame window;
    	public static JPanel center = new JPanel();
    	public static JPanel centerTop;
    	public static EditorPanel ep;
    public static void main(String[] args){
    		
    		window = new JFrame("Layer Manager 2.0");
    		window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		window.setSize(700,500);
    		window.setVisible(true);
    		window.setLayout(new BorderLayout());
    		
    		createJPanels();
    		
    	}
    
    	public static void createJPanels(){
    				center.setLayout(new BoxLayout(center,BoxLayout.Y_AXIS));
    				centerTop = new JPanel();
    				centerTop.setLayout(new BorderLayout());
    				centerTop.setBackground(Color.ORANGE);
    				centerTop.setVisible(true);
    				ep = new EditorPanel();
    				ep.setVisible(true);
    				centerTop.add(ep);
    				ep.repaint();
    				center.add(centerTop);
    				window.add(center);
    	}
    }
    Java Code:
    import java.awt.Graphics;
    import javax.swing.JPanel;
    
    
    public class EditorPanel extends JPanel{
    
    	/**
    	 * 
    	 */
    	private static final long serialVersionUID = 1L;
    	
    	public EditorPanel(){
    		System.out.println("Editor Panel Created!");
    		this.repaint();
    	}
    	@Override
    	public void paintComponent(Graphics g){
    		super.paintComponent(g);
    		System.out.println("Editor Panel Repainted!");
    		g.fillRect(0, 0, this.getWidth(), this.getHeight());
    	}
    When you run this, it will say "Editor Panel Repainted!" every time you resize the window. However, it doesn't draw the orange background anywhere. If you switch to a GridBagLayout() the orange background shows up, but the EditorPanel doesn't repaint. I would like the editorPanel to be centered inside the centerTop panel, what should I do?

    Thank you!

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    12,059
    Rep Power
    25

    Default Re: JPanel not repainting

    Hint: where you are printing out "Editor Panel Repainted" also print the Dimension returned by getSize().

    And invoking repaint() from within the constructor of a Component is entirely pointless.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  3. #3
    guadalcanal is offline Member
    Join Date
    Sep 2014
    Posts
    6
    Rep Power
    0

    Default Re: JPanel not repainting

    I see... I guess I'm a bit of an idiot. Now I have it working pretty well, but I still have a problem: when I try to use setPreferredSize() on the EditorPanel, it stops repainting! The size just goes as big as the centerTop panel when I use BorderLayout(), but when I try to use GridBagLayout() it's only 10x10 pixels... How can I manually set the size?
    Thank you!

  4. #4
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    12,059
    Rep Power
    25

    Default Re: JPanel not repainting

    You usually don't even try to set the size of a JPanel. Rather, you use an appropriate LayoutManager and let it size itself according to the components added.

    In case you're perjuring a JPanel to act like a JLabel with an Icon (that is to say, you're painting an Image and want the JPanel to size to the Image) you should override getPreferredSize() to return the dimensions of the Image.

    Since your class is named EditorPanel, I would guess that you intend to add some components to provide editing functions. Here's a resource that will help you choose the right LayoutManager for the job: Lesson: Laying Out Components Within a Container (The Java™ Tutorials > Creating a GUI With JFC/Swing)

    And no, you're not an idiot.
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  5. #5
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: JPanel not repainting

    Quote Originally Posted by DarrylBurke View Post
    You usually don't even try to set the size of a JPanel. Rather, you use an appropriate LayoutManager and let it size itself according to the components added.

    In case you're perjuring a JPanel to act like a JLabel with an Icon (that is to say, you're painting an Image and want the JPanel to size to the Image) you should override getPreferredSize() to return the dimensions of the Image.
    Partially my fault as the OP was doing something similar to your second paragraph in a separate thread and I told him to use
    setPreferredSize since painting beyond the boundaries does not cause a proper resizing of the JPanel (which was a bottom
    level component in the hierarchy).

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  6. #6
    guadalcanal is offline Member
    Join Date
    Sep 2014
    Posts
    6
    Rep Power
    0

    Default Re: JPanel not repainting

    Not your fault Jim :) I'm just still trying to get a grasp of how these Layout Managers work. Your solution worked perfectly for my other problem, but apparently it doesn't work for everything.
    I'll do my best to remember both of these methods for formatting JPanels and hopefully I won't need to ask next time :)
    Regards!

  7. #7
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    12,059
    Rep Power
    25

    Default Re: JPanel not repainting

    [QUOTE=jim829;396705I told him to use setPreferredSize since painting beyond the boundaries does not cause a proper resizing of the JPanel[/QUOTE]
    IMHO a better way to do that would be to override getPreferredSize().

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

Similar Threads

  1. Repainting on JPanel
    By sunde887 in forum AWT / Swing
    Replies: 8
    Last Post: 09-20-2011, 03:10 PM
  2. Replies: 4
    Last Post: 06-11-2011, 11:59 PM
  3. Question about repainting a JPanel
    By Nosrettap in forum Advanced Java
    Replies: 5
    Last Post: 01-02-2011, 08:29 PM
  4. JPanel and JLabels Not Repainting
    By phosphide in forum AWT / Swing
    Replies: 6
    Last Post: 11-13-2010, 03:17 AM
  5. Repainting From Another Class
    By habester in forum New To Java
    Replies: 1
    Last Post: 11-13-2009, 02:29 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •