Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 8 of 8

Thread: BackgroundPanel.java cannot be placed in BorderLayout.NORTH

  1. #1
    Member
    Join Date
    May 2010
    Posts
    37
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default BackgroundPanel.java cannot be placed in BorderLayout.NORTH

    i am trying to put a picture on the top of my swing GUI that stretches horizontally to fit the width of the window as it resizes

    http://www.camick.com/java/source/BackgroundPanel.java
    this is the code for a commonly used function that scales images to their components size. it works by overloading the paintcontainer class, which is called every time the window resizes, which then calls the paint class. they simply grab the size of the container and draw the image to those dimensions

    the image that needs to be resized must be placed in BorderLayout.NORTH, because the rest of my GUI is in BorderLayout.CENTER, but BackgroundPanel cant be placed in NORTH, the image disappears, even with a minimum size set. it cant be placed inside a panel inside a panel inside NORTH either. ive been reading over the code for BackgroundPanel and its driving me crazy i cant figure out why

    here is some short example code, not my program but it simplifies the problem:
    import java.awt.*;
    import javax.swing.*;
    import java.awt.event.WindowEvent;
     
    public class JPanel1 extends JFrame 
    {
    	public static void main(String[] args) 
    	{
    		new JPanel1();
    	}
     
    	public JPanel1() 
    	{
    		enableEvents(AWTEvent.WINDOW_EVENT_MASK);
    		add(mainJPanel());
    		pack();
    		setVisible(true);
    	}
     
     
    	//debug space center to streatch
    	private JPanel mainJPanel()
    	{
    		JPanel JPanel = new JPanel();
    		BorderLayout layout = new BorderLayout();
    		this.setMinimumSize( new Dimension( 224, 224 ) );
    		JPanel.setLayout(layout);
    		JPanel.add(mainJPanel2(),BorderLayout.NORTH);
    		return(JPanel);
    	}
     
     
    	//debug space center to streatch
    	private JPanel mainJPanel2()
    	{
    		JPanel JPanel = new JPanel();
    		BorderLayout layout = new BorderLayout();
    		JPanel.setLayout(layout);
    		JPanel.add(JPanel2(),BorderLayout.CENTER);
    		return(JPanel);
    	}
     
     
    	private JPanel JPanel2() 
    	{
    		BorderLayout layout = new BorderLayout();
    		this.setLayout(layout);
    		Toolkit toolkit = Toolkit.getDefaultToolkit(); 
    		Image duke = toolkit.getImage("sun.jpg");
    		BackgroundPanel panel = new BackgroundPanel(duke, BackgroundPanel.SCALED);
    		//panel.setMinimumSize( new Dimension( 224, 224 ) );
    		this.setMinimumSize( new Dimension( 224, 224 ) );
    		GradientPaint paint = new GradientPaint(0, 0, Color.BLUE, 600, 0, Color.RED);
    		panel.setPaint(paint);
    		return panel;
    	}
     
    	//exit when closed
        public void processWindowEvent(WindowEvent event)
    	{
    		if(event.getID() == WindowEvent.WINDOW_CLOSING)
    		System.exit(0);
        }
     
    }

  2. #2
    Member
    Join Date
    Jun 2010
    Posts
    48
    Thanks
    12
    Thanked 2 Times in 2 Posts

    Default Re: BackgroundPanel.java cannot be placed in BorderLayout.NORTH

    I don't know why right know, but your paintComponent method in BackgroundPanel class is not working. Try to add System.out.println there, you will see.

  3. #3
    Member Darryl.Burke's Avatar
    Join Date
    Mar 2010
    Location
    Madgaon, Goa, India
    Posts
    494
    Thanks
    8
    Thanked 48 Times in 46 Posts

    Default Re: BackgroundPanel.java cannot be placed in BorderLayout.NORTH


  4. #4

  5. #5
    Member
    Join Date
    May 2010
    Posts
    37
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: BackgroundPanel.java cannot be placed in BorderLayout.NORTH

    try changing NORTH to CENTER, it works perfectly if u do that. but i need it in NORTH

  6. #6
    Member
    Join Date
    May 2010
    Posts
    37
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: BackgroundPanel.java cannot be placed in BorderLayout.NORTH

    the problem has been solved, i needed prefered size not minimym size,
    thank you very much for your efforts

  7. #7
    Member
    Join Date
    Jun 2010
    Posts
    48
    Thanks
    12
    Thanked 2 Times in 2 Posts

    Default Re: BackgroundPanel.java cannot be placed in BorderLayout.NORTH

    Ah, I feel pity that I didn't suggest you that before. I remember I was thinking that you may need both, preferred and the minimum sizes set. Next time I will try to be more confident with the knowledge I have

  8. #8
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 294 Times in 227 Posts
    Blog Entries
    4

    Default Re: BackgroundPanel.java cannot be placed in BorderLayout.NORTH

    Moved to - AWT / Java Swing - Java Programming Forums
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.