Java Utililty Methods JComponent Size

List of utility methods to do JComponent Size

Description

The list of methods to do JComponent Size are organized into topic(s).

Method

JComponentgetComponentOfSameSize(final JComponent c)
get Component Of Same Size
final JPanel p = new JPanel();
p.setBackground(Color.red);
p.setPreferredSize(c.getPreferredSize());
c.addComponentListener(new ComponentListener() {
    private void resize() {
        p.setPreferredSize(c.getSize());
        p.setMaximumSize(c.getSize());
        p.setMinimumSize(c.getSize());
...
DimensiongetLocalScreenSize(Component invoker)
Gets the screen size.
if (invoker != null && !(invoker instanceof JApplet) && invoker.getGraphicsConfiguration() != null) {
    GraphicsConfiguration gc = invoker.getGraphicsConfiguration();
    Rectangle bounds = gc.getBounds();
    Insets insets = Toolkit.getDefaultToolkit().getScreenInsets(gc);
    bounds.width -= insets.left + insets.right;
    bounds.height -= insets.top + insets.bottom;
    return bounds.getSize();
} else {
...
DimensiongetMaxSize(JComponent[] components)
get Max Size
int width = 0;
int height = 0;
for (int i = 0; i < components.length; i++) {
    Dimension size = components[i].getPreferredSize();
    width = Math.max(width, size.width);
    height = Math.max(height, size.height);
return new Dimension(width, height);
...
DimensiongetOptimalScreenSize(Container container, Dimension currentDim)
Calculates the optimal screen size for the given container.
Component[] comps = container.getComponents();
for (int i = 0; i < comps.length; ++i) {
    try {
        currentDim = getOptimalScreenSize((Container) comps[i], currentDim);
        Component comp = comps[i];
        int width = comp.getWidth();
        int height = comp.getHeight();
        if (comp instanceof JScrollPane) {
...
DimensiongetPreferredSize(JComponent component, int width)
Returns the preferred dimensions of a component, using the current height and a custom width.
Dimension dim = component.getPreferredSize();
return new Dimension(width, dim.height);
java.awt.DimensiongetPreferredSize(String html, boolean width, int prefSize)
get Preferred Size
resizer.setText(html);
View view = (View) resizer.getClientProperty(javax.swing.plaf.basic.BasicHTML.propertyKey);
view.setSize(width ? prefSize : 0, width ? 0 : prefSize);
float w = view.getPreferredSpan(View.X_AXIS);
float h = view.getPreferredSpan(View.Y_AXIS);
return new java.awt.Dimension((int) Math.ceil(w), (int) Math.ceil(h));
DimensiongetPreferredSize(Window window)
Gets the preferred size of the given Window .
Dimension pSize = window.getPreferredSize();
Component glass = null;
Component content = null;
if (window instanceof JFrame) {
    JFrame frame = (JFrame) window;
    glass = frame.getGlassPane();
    content = frame.getContentPane();
} else if (window instanceof JDialog) {
...
JComponentgetPreferredSizeComponent(JComponent component)
Wraps a JComponent into a JPanel using a BorderLayout , adding it to WEST.
If using this method for e.g.
JPanel pWrap = new JPanel(new BorderLayout());
pWrap.add(component, BorderLayout.WEST);
return pWrap;
java.awt.DimensiongetScreenSize()
Returns the maximum possible size of a fully visible window.
if (g_screenSize == null) {
    java.awt.Component c = getRefComp();
    java.awt.Toolkit tk = c.getToolkit();
    g_screenSize = tk.getScreenSize();
    try {
        java.awt.Insets in = tk.getScreenInsets(c.getGraphicsConfiguration());
        g_screenSize.width -= in.left + in.right;
        g_screenSize.height -= in.top + in.bottom;
...
DimensiongetScreenSize(Component invoker)
Gets the screen size.
Dimension screenSize = getScreenBounds().getSize();
if (invoker != null && !(invoker instanceof JApplet) && invoker.getGraphicsConfiguration() != null) {
    Insets insets = Toolkit.getDefaultToolkit().getScreenInsets(invoker.getGraphicsConfiguration());
    screenSize.width -= insets.left + insets.right;
    screenSize.height -= insets.top + insets.bottom;
return screenSize;