This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Swing / AWT / SWT and the fly likes Changing a border size Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Changing a border size" Watch "Changing a border size" New topic
Author

Changing a border size

Tom Josephits
Ranch Hand

Joined: Mar 11, 2012
Posts: 31
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

Joined: Jun 09, 2003
Posts: 4632
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

Joined: Mar 11, 2012
Posts: 31
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

Joined: Jun 09, 2003
Posts: 4632
perhaps setting the border in ComponentListener's componentResized() might suit
Shtilmark Pollo
Greenhorn

Joined: Jun 14, 2012
Posts: 4
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.
 
jQuery in Action, 2nd edition
 
subject: Changing a border size
 
Similar Threads
new windows
Opening a Browser Window
html page adjustment whilw window resize
ToolBar size increase
making the command prompt window smaller