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.