• 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
  • Ron McLeod
  • Paul Clapham
  • Jeanne Boyarsky
  • Liutauras Vilda
Sheriffs:
  • Rob Spoor
  • Bear Bibeault
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:
  • Frits Walraven
  • Himai Minh

JLabel on top of JTextArea with GridBagLayout shrinks JTextArea's size

 
Greenhorn
Posts: 25
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So, I have a panel with GridBagLayout  and I want a Jlabel to be placed on top of JTextArea. The problem is, if I put jlabel on top or bottom of jtextarea, the jtextarea will shrink. I tried to put jbutton instead of jlabel and the jtextarea wasn't shrinking. Here are some illustrations:

with jbutton


with jlabel


Code snippet

Is there a solution for this problem? or should I just use other layouts?
 
Saloon Keeper
Posts: 8241
71
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
After playing around with your code I came up with this. It happens to use a "GBC" utility class which I'll add in the next post.
 
Carey Brown
Saloon Keeper
Posts: 8241
71
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Use of the GBC class will clean up your Swing GridBagConstraint code immensely. The use of "GBCConsts" as an "implements" is strictly for convenience so you can just say "HORIZONTAL" for example.



 
Elya Matsunomi
Greenhorn
Posts: 25
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for this. This is a nice solution. Could you explain to me how this codes work

mainPanelLayout.rowWeights = new double[]{ 0.0, 0.0, 0.0, 0.1, 0.0 };
mainPanelLayout.rowHeights = new int[]{ 7, 7, 7, 20, 7 };
mainPanelLayout.columnWeights = new double[]{ 0.0, 1.0, 0.0 };
mainPanelLayout.columnWidths = new int[]{ 7, 300, 7 };

I presume these codes override row and column wildth and height.However, this looks like an advance way to adjust width and height. The javadoc regarding this fields is lacking in details.
 
Carey Brown
Saloon Keeper
Posts: 8241
71
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Elya Matsunomi wrote:Thanks for this. This is a nice solution. Could you explain to me how this codes work

mainPanelLayout.rowWeights = new double[]{ 0.0, 0.0, 0.0, 0.1, 0.0 };
mainPanelLayout.rowHeights = new int[]{ 7, 7, 7, 20, 7 };
mainPanelLayout.columnWeights = new double[]{ 0.0, 1.0, 0.0 };
mainPanelLayout.columnWidths = new int[]{ 7, 300, 7 };

I presume these codes override row and column wildth and height.However, this looks like an advance way to adjust width and height. The javadoc regarding this fields is lacking in details.

Heights and Widths set up the minimum row and column sizes in pixels. The weights determine that, IF an item will not fit in those dimensions, how to proportionally allocate more space amongst various rows/columns. These provide a more controlled way of maintaining "grid" dimensions than working with height, width, and insets at a cellular level.

Note that I included "insets", and that in the example code I specifically did NOT include GCB.setInsets(), and that when I created a new GCB(x,y) that my x,y was one-off of yours. All this is because of a little trick with rowHeights and columnWeights of setting some of them to '7' pixels. This size is the number of pixels between rows or columns for spacing, including before the first row/col and after the last row/col. The '7' appears for every other row/col because you have a regular grid. In some layouts it may be necessary for two columns to butt up against each other with no space (for instance), and for that, there would be no '7' inserted. The '7' itself is arbitrary but seems to work nicely. This approach mostly eliminates the need to hand tweak the insets for each cell, though that's still possible.
 
Rancher
Posts: 3242
30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Note the code order should be:


The resizable method can affect the size of the decorations of the frame which in turn will affect the pack() calculation.
 
Marshal
Posts: 73019
330
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can use static import instead of implements so those constants appear to be private fields of the GBC class.

EM: Please don't quote the whole of the preceding post; that doesn't add anything new and is liable to removal.
 
Elya Matsunomi
Greenhorn
Posts: 25
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I examined the code earlier and found out that I just need to assign weights to my textarea and set the fill mode of textarea's constraints to GridBagConstraints.BOTH to achieve the result that I wanted.

 
Carey Brown
Saloon Keeper
Posts: 8241
71
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Very nice.

Using GBC it would look like:
Edit: setAnchor(Center) could be removed because it is the default.
 
Crusading Chameleon likes the size of this ad:
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
reply
    Bookmark Topic Watch Topic
  • New Topic