• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Changing a border size

 
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
perhaps setting the border in ComponentListener's componentResized() might suit
 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic