0

I am trying to fit an image to be placed in a JPanel. I find the width and height of the JPanel and scale the image to have the same width and height as the JPanel. My problem is the image is then slightly outside of the JPanel.

A simple short example is

public class TestJPanel {

/**
 * @param args
 */
public static void main(final String[] args) {
    final JFrame jf = new JFrame();

    final JButton jb = new JButton("100by100");
    jb.setPreferredSize(new Dimension(100,100));

    final JPanel jp = new JPanel();
    jp.setPreferredSize(new Dimension(100, 100));

    System.out.println(jp.getInsets());

    jp.add(jb);

    jf.getContentPane().add(jp);
    jf.pack();

    jf.setVisible(true);
}
}

In this case the button is slightly outside of the JPanel. Is there a different method aside from JPanel.getWidth() and the equivalent height method to find the maximum size of a component to put in a JPanel?

Edit

I think this is an issue with the layout manager. In my application I am using GridBagLayout.

Changing the example above to the following prevents the button being cut off.

public class TestJPanel {

/**
 * @param args
 */
public static void main(final String[] args) {
    final JFrame jf = new JFrame();

    final JButton jb = new JButton("100by100");
    jb.setPreferredSize(new Dimension(100,100));

    final JPanel jp = new JPanel(new FlowLayout(FlowLayout.CENTER, 0, 0));
    jp.setPreferredSize(new Dimension(100, 100));

    System.out.println(jp.getInsets());

    jp.add(jb);

    jf.getContentPane().add(jp);
    jf.pack();

    jf.setVisible(true);
}
}
trashgod
  • 203,806
  • 29
  • 246
  • 1,045
Codey McCodeface
  • 2,988
  • 6
  • 30
  • 55
  • What do you mean the button is outside the JPanel? How do you know this? – Cruncher Dec 05 '12 at 11:22
  • 1
    If you add something to a container, there should be no way for it to use up space outside of the bounds of that container – Cruncher Dec 05 '12 at 11:22
  • Why you do need JPanel to have same size with JButton? In that case you don't need JPanel at all. Consider using `SwingUtilities.invokeLater()` – Nikolay Kuznetsov Dec 05 '12 at 11:23
  • I can see the the bottom of the JButton is cut off. In my application I have to pan to see the rest of the image but It should fit snuggly into the JPanel – Codey McCodeface Dec 05 '12 at 11:25
  • This is a problem of whether or not you can see your whole JPanel in the frame. Not whether or not the button is fitting inside the JPanel. If the button was bigger than the JPanel, pieces of the button would be cut off for good, and not be retrievable by panning. – Cruncher Dec 05 '12 at 11:33
  • @medPhys-pl notice ScalledImage is pretty asynchronous, could be delayed – mKorbel Dec 05 '12 at 11:34
  • [Should I avoid the use of set(Preferred|Maximum|Minimum)Size methods in Java Swing](http://stackoverflow.com/q/7229226/230513)? – trashgod Dec 05 '12 at 18:41

1 Answers1

0

As I say in my question the problem was with the layout manager. In my application this was not the problem because I was using GridBagLayout. I have a series of JComponents one of which was an extension of ImageScrollPane that did not have an empty border.

I found the trick was, similar to this question to set the border on my component to an empty Border.

Community
  • 1
  • 1
Codey McCodeface
  • 2,988
  • 6
  • 30
  • 55