java - JPanel taking up more space than it needs. How can I shrink it? -


update: have received justified criticism posting non working code. i've taken heart , updating post complete working example. i'm updating description accordingly:

i have simple java swing gui components take looks equal amount of vertical (y) space used largest y extent component, unnecessarily so. have tried shrink components don't need vertical space using preferredsize hints no avail.

the basic layout simple: there's main window , 3 vertical panels. layout simple gridlayout (and prefer keep way, unless shows me need cannot done gridlayout). 3 panels seem occupying same amount of vertical space, though in case of sliders, massive waste of space. how can each of sub-panes use space each need? i.e. 2 slider windows tall sliders , description need be.

the code:

import java.awt.*; import java.awt.event.*; import javax.swing.*;  class test {   public static void main(string[] arg) {     jframe mainwindow = new jframe();     jslider slider1 = new jslider(0,100,50);     jslider slider2 = new jslider(0,100,50);      jpanel pnlslider1 = new jpanel();     pnlslider1.setlayout(new gridlayout(1,1));  // 1 row, 1 column     pnlslider1.add(new jlabel("description slider1"));     pnlslider1.add(slider1);      jpanel pnlslider2 = new jpanel();     pnlslider2.setlayout(new gridlayout(1,1));  // 1 row, 1 column     pnlslider2.add(new jlabel("description slider2"));     pnlslider2.add(slider2);      // label should left of slider      string content = "<html>some rather long winded html content</html>";     jeditorpane ep = new jeditorpane("text/html", content);      // main window panel     jpanel panel = new jpanel();     panel.setlayout(new gridlayout(3,1)); // 3 rows, 1 column     panel.add(ep);     panel.add(pnlslider1);     panel.add(pnlslider2);      // tie , display window         mainwindow.setpreferredsize(new dimension(300, 600));         mainwindow.setlocation(100, 100);         mainwindow.getcontentpane().add(panel);         mainwindow.pack();         mainwindow.addwindowlistener(new windowadapter() {             public void windowclosing(windowevent e) {                 system.exit(0);             }         });         mainwindow.setvisible(true);    } } 

(removed rant not having seen gui coding advances in 30 years that's not pertinent problem , won't solved in post either)

..components take looks equal amount of vertical (y) space used largest y extent component, unnecessarily so.

yes, is way gridlayout designed work.

use gridbaglayout or boxlayout or grouplayout instead, each of can single column or row of components of variable size (width , height).


Comments

Popular posts from this blog

powershell Start-Process exit code -1073741502 when used with Credential from a windows service environment -

twig - Using Twigbridge in a Laravel 5.1 Package -

c# - LINQ join Entities from HashSet's, Join vs Dictionary vs HashSet performance -