• 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

How can I lock one dimension of a JFrame?

 
Bartender
Posts: 1464
32
Netbeans IDE C++ Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
JFrame.setResizable(false) allows me to create a JFrame the user cannot resize. But I need a JFrame that the user can widen or narrow, while keeping a fixed height. Scouring the Web produced numerous variations on the following:

However, if this ever worked, it does not work under Java SE8 and Windows 10. Dragging the bottom edge of the JFrame's window up and down shows it snapping back into place (most of the time), but the final height is left where the edge was when I released the mouse button. In fact, no "mouse released" event is received by the JFrame, which suggests to me that either Windows gets the event and Java doesn't, or Java gets it but it isn't sent to the JFrame.

Anyone have a way to let the user change the width, but not the height, of a JFrame?
 
Ranch Hand
Posts: 4716
9
Scala Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
not sure this will help, but maybe using a scrollpane will help/work.
 
Rancher
Posts: 3324
32
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Frist of all, updates to Swing components should be done on the Event Dispatch Thread, not just the setVisible(...) method.

Anyone have a way to let the user change the width, but not the height, of a JFrame?



Make the frame not resizable and then handle the resizing of the frame yourself:


You will need the code for the Component Resizer
 
Stevens Miller
Bartender
Posts: 1464
32
Netbeans IDE C++ Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks, Rob. I was working on something similar when you posted this. Both of us are handling the mouse events and adjusting the cursor, ultimately calling setBounds() to change the size and location of the Component. This works, but I notice that, when the Component is the outermost JFrame, resizing the left edge (which requires changing both the size and location of the JFrame) creates noticeable "jitter" of the right edge while the mouse is dragging. I stepped through the code and, as far as I can tell, this is Windows's fault. You can even see it in any number of apps that come with the Windows OS (Calculator, Notepad, etc). Odd that I never noticed it before, but at least it's not a problem in the approach we're using.

Appreciate your posting a link to that code. It's more generalized than what I was doing. My strategy was to use an undecorated JFrame with a Border layout manager. The border holds subclasses of JPanel I created that react to the mouse events and do the necessary cursor/resizing/relocating calls. The center is where I'd put my actual JPanel with the GUI. Looks like doing our own mouse processing is about the best option there is. Danged shame there's no way to turn off resizing in just one dimension or the other, though.
 
Author
Posts: 986
3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can get halfway there with frame.setMinimumSize(new Dimension(0, 300)). This prevents the user from making the height shorter than 300, but doesn't prevent the user from making it taller.

It's possible to also call setMaximumSize() but unfortunately that doesn't seem to do anything. The code in java.awt.Window.reshape() has a check for minimum and not for maximum, but I don't know for sure that's why.

It seems like it should be possible to trace exactly what where setMinimumSize() is having its effect and emulate it (or not; some of the methods are private) but it does seem like the native peer is involved.
 
Stevens Miller
Bartender
Posts: 1464
32
Netbeans IDE C++ Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I noticed that one-sided check in the code too, Brian. Wonder why they did it that way...
 
You can thank my dental hygienist for my untimely aliveness. So tiny:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic