0

Most posts I've seen about this seem to be related to the user resizing it, however I want the program itself to resize it.

I'm trying to make a titled border in a program, and I only want it to be a certain size. However it seems to be the size of the whole window, which is not what I want. I tried setting the size and the preferred size, however it doesn't seem to be affecting anything.

Here's the code, by the way:

private JFrame Frame;
private JPanel Main;
private TitledBorder Border;

Frame = new JFrame("Window Title");
Frame.setSize(600,400);
Frame.setResizable(false);

Main = new JPanel();
Main.setSize(new Dimension(30,30)); // I also tried using ints to set the size
Border = BorderFactory.createTitledBorder("Border Title");
Main.setBorder(Border);

Frame.add(Main);
Frame.setVisible(true);
fixylol
  • 13
  • 3
  • 1
    `"I tried setting the size and the preferred size, however it doesn't seem to be affecting anything."` -- meaning that you're doing something wrong in code not shown. Why make it hard to guess what and instead show your code, preferably as a [mcve] (please read the link) – Hovercraft Full Of Eels Sep 23 '16 at 17:42
  • 1
    You're forgetting that you're using layout managers and most respect preferred size (and you're not showing that attempt). Best to override `getPreferredSize()`, and then let the layout managers do their thing. [For example](http://stackoverflow.com/questions/25229505/how-to-set-the-size-of-jpanel-in-java). The example also shows a proper [mcve], something you've not yet posted, as it compiles and runs. – Hovercraft Full Of Eels Sep 23 '16 at 17:47
  • 1
    Also see [this link](http://stackoverflow.com/questions/10866762/use-of-overriding-getpreferredsize-instead-of-using-setpreferredsize-for-fix). Also, you're forgetting to `pack()` your JFrame before displaying it. – Hovercraft Full Of Eels Sep 23 '16 at 17:52
  • Variable names should NOT start with an upper case character. Note the Border is not considered when the preferred size of a panel is calculated. So you need to add components to the panel before a reasonable size can be calculated. If you want the TitledBorder to be included in the preferred size calculation then you can check out: http://stackoverflow.com/questions/36405080/why-is-my-titled-border-panel-so-small/36405695#36405695 – camickr Sep 23 '16 at 19:06

0 Answers0