0

Due to the answers I have received I am now doing this a different way.

Is there any way to override setPreferredSize() and getPreferredSize() so it will actually set the size of the component to a number higher than was actually input, and get a number that is a number lower than it actually is?

Here I am overriding both methods to set the size of a panel to 100 pixels more than what I actually put in setPreferredSize() and if I were to get the preferred size, I would like it to return what I put in setPreferredSize()

I am suspecting that Swing uses getPreferredSize() to set the size, so is there another method I can override to achieve this or am I stuck having to make another method to return the values I want?

import java.awt.*;
import java.beans.Transient;
import javax.swing.*;

public class PreferredSize extends JPanel{

public static void main(String[] args) {
    JPanel background = new JPanel();

    PreferredSize ps = new PreferredSize();
    ps.setPreferredSize(new Dimension(200, 200));
    ps.setBackground(Color.CYAN);

    JPanel panel = new JPanel();
    panel.setBackground(Color.BLUE);
    panel.setPreferredSize(new Dimension(200, 200));


    background.add(ps);
    background.add(panel);

    JFrame f = new JFrame();
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    f.setContentPane(background);
    f.pack();
    f.setLocationRelativeTo(null);
    f.setVisible(true);
}


@Override
public void setPreferredSize(Dimension preferredSize) {
    super.setPreferredSize(new Dimension(preferredSize.width+100, preferredSize.height+100));
}

@Override
@Transient
public Dimension getPreferredSize() {
    return new Dimension(super.getPreferredSize().width-100, super.getPreferredSize().height-100);
}
}

Thanks.

Michael Peterson
  • 348
  • 6
  • 22
  • change logics, ordering, JComponents returns its PreferredSize to the contianer, JPanel to JFrame, JComponents to JPanel – mKorbel Mar 09 '13 at 07:51
  • @mKorbel Can you be a little more specific on that please? – Michael Peterson Mar 09 '13 at 07:53
  • by default JCompoents returns without any issue proper coordinates to the container (excluding empty JPanel, JPanel contains only Graphics, JScrollPane and JComboBox), I miss real reason for this question (event is good described, and inc SSCCE) – mKorbel Mar 09 '13 at 07:57
  • *"Is there any way to.."* Is there any logical *reason* to..? It seems like you want to intentionally shoot the user of this class in the foot. Can you explain it to me in a way that makes sense? – Andrew Thompson Mar 09 '13 at 08:05
  • @Andrew Thompson I'm making a JPanel that you can resize by dragging. I'm trying to draw a border around it, and when the user wants the size of the panel, I don't want them to know about the extra border around it. – Michael Peterson Mar 09 '13 at 08:22

1 Answers1

4

The problem you're facing is the fact that preferred size is only a guide that CAN be used by the layout managers to make decisions about how a component should be laid out. It's perfectly reasonable for a layout manager to ignore these hints.

In the case of ip your example;

  1. The default size of a JPanel is 0x0, so by the time you've added in and subtracted the 100 pixels, its now 0x0 again.
  2. The default layout manager for a JFrame is BorderLayout, which will ignore the preferred size if its available size is larger or smaller then that specified by preferred size

The real question is, what are your actually trying to achieve?

MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
  • @MadProgrammer Same as what I had said to Andrew, I'm making a JPanel that you can resize by dragging. I'm trying to draw a border around it, and when the user wants the size of the panel, I don't want them to know about the extra border around it. – Michael Peterson Mar 09 '13 at 08:24
  • @maptwo3 that's a difficult thing. You could use a combination of borders, but that will always leave space around the panel. You could use something like JLayer or you could move the panel you are reszing to the glass pane and use some custom painting on the glass pane OR a wrapper panel the has the reszie painting and th panel you won't resized on it (all sitting on the glass pane) – MadProgrammer Mar 09 '13 at 09:13
  • +1 for answer. @maptwo3 see [this](http://stackoverflow.com/a/13067244/1133011) similar answer for what you want to achieve – David Kroukamp Mar 09 '13 at 12:06