I have this problem, that whenever I set the frame size using the setSize () method the size inside the frame is actually smaller, because the frame size is included in this dimension. My question is how would I make it so that the inside of the frame is the size that I specify?

JComponents must returns ScreenSize, not JFrame#setSize();

1/ don't use setSize()

2/ don't usee setBounds()

3/ JFrame#setSize() is including "blueColoredToolbar" + EtechedBorder that came from Native OS

4/ set preferredSize ...

5/ I talking about that in your thread ...

6/ setSize() and setBounds() multiplayed spend time needed for start -> to display Container on the Monitor

A JFrame consists of a Content Pane surrounded by borders etc and, as you have observed, its size includes the (system-dependent) borders.
To achieve your desired result, set the (preferred) size of the JFrame's Content Pane (access it via myFrame.getContentPane()) rather than the JFrame itself.

is useless build something based on getContentPane(), that was modified long time ago as JPanel and inc. BorderLayout for that,

is useless call in Java6 inherits methods from java.awt.Frame (ContentPane, RootPane)

is useless as mistake and against all Swing's Rulles to call setPreferredSize to the JFrame, that's same as JFrame#setSize, plus by using setPreferredSize to the JFrame and with required JFrame#pack() then same code runs twice, to recalculate all childs from this Container

all JComponens have got by default method(s) how to returns own PreferredSize to the parent JTextAres(50, 20), JComboBox#setPrototypeVelue("XXXXXXXX"), jTextField(10) ...., or by setting for GridBagLayout, BoxLayout by using PreferredSize for required JCompoents

and so on :-)

Sorry mKorbel, I don't think describing another's post as "useless" three times meets this forum's standards for courtesy, but I'm not going to hijack the OPs thread for an argument on this.

@sirliknk99: Frankly I don't understand mKorbel's post. but I stand by what I said. Good luck.

I'm outta here now.
J

Member Avatar for hfx642

Make your "size"s the size you want PLUS the size of the Frame itself.

Thanks for the replies, but the size of the frame itself could vary from OS to OS

Thanks for the replies, but the size of the frame itself could vary from OS to OS

Yes, that's why you size the content pane - the content pane excludes the system-dependent frame that surrounds it.

... if still in doubt, just run this...

import java.awt.Dimension;
import java.awt.Graphics;
import javax.swing.JFrame;

public class Demo extends JFrame {

   public static void main(String[] args) {
      new Demo();
   }

   Demo() {
      getContentPane().setPreferredSize(new Dimension(150, 150));
      pack();
      setLocationRelativeTo(null);
      setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      setVisible(true);
   }

   @Override
   public void paint(Graphics g) {
      super.paint(g);
      // just draw a simple x,y 20 pixel grid to confirm size
      for (int i = 0; i < 500; i += 20) {
         g.drawLine(i, 0, i, 500);
         g.drawLine(0, i, 500, i);
      }
   }

}

minor changes for todays Swing

import java.awt.Dimension;
import java.awt.Graphics;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;

public class Demo extends JFrame {

    private static final long serialVersionUID = 1L;

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {

            @Override
            public void run() {
                Demo demo = new Demo();
            }
        });
    }

    public Demo() {
        setPreferredSize(new Dimension(150, 150));
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        pack();
        setLocationRelativeTo(null);
        setVisible(true);
    }

    @Override
    public void paint(Graphics g) {
        super.paint(g);
        // just draw a simple x,y 20 pixel grid to confirm size
        for (int i = 0; i < 500; i += 20) {
            g.drawLine(i, 0, i, 500);
            g.drawLine(0, i, 500, i);
        }
    }
}

Yes, the serial ID and runnable would be used in a real app- I left them out because they weren't essential to the demo point.
But, by removing the getContentPane on your line 21 you have broken it. It's now 150x150 including the frame. The whole point of this is that the OP wanted to specify the size of the content area, excluding the frame, which is what my code does.
J

accepted your ContentPane, then I can see there only this way

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

public class MainComponentPaint extends JFrame {

    private static final long serialVersionUID = 1L;

    public MainComponentPaint() {
        setTitle("Customize Preffered Size Test");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }

    public void display() {
        add(new CustomComponent());
        pack();
        setMinimumSize(getSize());
        setPreferredSize(getSize());
        SwingUtilities.invokeLater(new Runnable() {

            @Override
            public void run() {
                setVisible(true);
            }
        });
    }

    public static void main(String[] args) {
        MainComponentPaint main = new MainComponentPaint();
        main.display();
    }
}

class CustomComponent extends JComponent {

    private static final long serialVersionUID = 1L;

    @Override
    public Dimension getMinimumSize() {
        return new Dimension(150, 150);
    }

    @Override
    public Dimension getPreferredSize() {
        return new Dimension(300, 300);
    }

    @Override
    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        for (int i = 0; i < 500; i += 20) {
            g.drawLine(i, 0, i, 500);
            g.drawLine(0, i, 500, i);
        }
    }
}

I really don't understand why you are complicating this so much, but that's my problem. Never mind.
@sirlink99: You are free to use my approach if you want, or mKorbel's if you prefer. Maybe just run both, see which work, then go with the simplest solution?
I really do not want to argue about this.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.