Skip to main content

BackgroundPanel.java cannot be placed in BorderLayout.NORTH

2 replies [Last post]
chopficaro
Offline
Joined: 2010-05-01

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.*;<br />
import javax.swing.*;<br />
import java.awt.event.WindowEvent;</p>
<p>public class JPanel1 extends JFrame<br />
{<br />
    public static void main(String[] args)<br />
    {<br />
        new JPanel1();<br />
    }</p>
<p>    public JPanel1()<br />
    {<br />
        enableEvents(AWTEvent.WINDOW_EVENT_MASK);<br />
        add(mainJPanel());<br />
        pack();<br />
        setVisible(true);<br />
    }</p>
<p>    //debug space center to streatch<br />
    private JPanel mainJPanel()<br />
    {<br />
        JPanel JPanel = new JPanel();<br />
        BorderLayout layout = new BorderLayout();<br />
        this.setMinimumSize( new Dimension( 224, 224 ) );<br />
        JPanel.setLayout(layout);<br />
        JPanel.add(mainJPanel2(),BorderLayout.NORTH);<br />
        return(JPanel);<br />
    }</p>
<p>    //debug space center to streatch<br />
    private JPanel mainJPanel2()<br />
    {<br />
        JPanel JPanel = new JPanel();<br />
        BorderLayout layout = new BorderLayout();<br />
        JPanel.setLayout(layout);<br />
        JPanel.add(JPanel2(),BorderLayout.CENTER);<br />
        return(JPanel);<br />
    }</p>
<p>    private JPanel JPanel2()<br />
    {<br />
        BorderLayout layout = new BorderLayout();<br />
        this.setLayout(layout);<br />
        Toolkit toolkit = Toolkit.getDefaultToolkit();<br />
        Image duke = toolkit.getImage("sun.jpg");<br />
        BackgroundPanel panel = new BackgroundPanel(duke, BackgroundPanel.SCALED);<br />
        //panel.setMinimumSize( new Dimension( 224, 224 ) );<br />
        this.setMinimumSize( new Dimension( 224, 224 ) );<br />
        GradientPaint paint = new GradientPaint(0, 0, Color.BLUE, 600, 0, Color.RED)<br />
;<br />
        panel.setPaint(paint);<br />
        return panel;<br />
    }</p>
<p>    //exit when closed<br />
   public void processWindowEvent(WindowEvent event)<br />
    {<br />
        if(event.getID() == WindowEvent.WINDOW_CLOSING)<br />
        System.exit(0);<br />
   }</p>
<p>}

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
walterln
Offline
Joined: 2007-04-17

BorderLayout ignores minimum size, use preferred size instead.

chopficaro
Offline
Joined: 2010-05-01

tyvm! your suggestion fixed my problem!