• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Changing a border size

 
Tom Josephits
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have an object that I want to be be a perfect square no matter how large the window gets, but I don't want it to be restricted to a certain size (aka, as big as it can get within the window). The problem is, I have an empty border for padding around it that I want to also adjust in proportion with the size. The problem is, every time I use setBorder() in the paint method (where I've been putting all my re-size code), it doesn't work. I think it's creating an infinite loop but I'm not sure. Anyone know what the problem is and how I can fix this?
 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
setBorder() is likely to call repaint() when set, similar to setIcon() etc,
so an infinite loop seems probable.

if you want your squareObject to remain the same size, regardless of window size,
add it to a GridBagLayout JPanel, with default GridBagConstraints. The squareObject
will be located in the center of the panel, with the panel taking up all additional space.
 
Tom Josephits
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But I don't. I want it to always stay the largest it can, just with a border that changes in proportion with the window size.
 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
perhaps setting the border in ComponentListener's componentResized() might suit
 
Shtilmark Pollo
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think you could do something like this. Create Square class with the following setBounds method:

Then create dynamic border. For example size of this border will be 10% of component size.

And then install this border to the square.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic