java - Need a GridBagLayout tutor for dummies please... with working example -


i'm trying create gui has jxtaskpane, within html editor pane showing lengthy text, below 2 sliders. 1 should have full window width divided 50/50 between label , slider, next slider should have 1/3rd of width label , 2/3 slider.

in order example run, need swingx, download jar here.

there several problems code:

  1. on first startup, elements not drawn correctly. window needs resized, or task pane closed , reopened. can recommend way fix this?
  2. even though gridbagconstraint fill parameter set use full horizontal width, components using half of window width , drawn in center. how can make them use full width , height of jxtaskpane?
  3. even though slider2 has gridwidth property set 2 (the label has set 1) in fact drawn narrower label! why that? tried playing weightx parameter, changes appearance randomly little wider or little narrower, in seems random , rather unpredictable fashion. how make label 1/3rd width of slider?

import java.awt.*; import java.awt.event.*; import javax.swing.*; import org.jdesktop.swingx.jxtaskpane;  class test {   public static void main(string[] arg) {     jframe mainwindow = new jframe();     jxtaskpane taskpane = new jxtaskpane();     jslider slider1 = new jslider(0,100,50);     jslider slider2 = new jslider(0,100,50);      taskpane.setcollapsed(false);      // slider 1 label in simple gridlayout     jpanel pnlslider1 = new jpanel();     pnlslider1.setlayout(new gridlayout(1,1));  // 1 row, 1 column     pnlslider1.add(new jlabel("description slider1"));     pnlslider1.add(slider1);      // slider 2 label in gridbaglayout, label should 1/3 of width of slider     jpanel pnlslider2 = new jpanel();     pnlslider2.setlayout(new gridbaglayout());     gridbagconstraints c2 = new gridbagconstraints();     c2.fill = gridbagconstraints.horizontal;     c2.gridx = 0;     c2.gridy = 0;     c2.gridwidth = 1;     c2.weighty = 1;     c2.weightx = 1;     pnlslider2.add(new jlabel("description slider2"), c2);     c2.gridx = 1;     c2.gridwidth = 2;     c2.weightx = 1;     pnlslider2.add(slider2, c2);      // label should left of slider      string content = "<html>some rather long winded html content rather long winded html content rather long winded html content rather long winded html content ";     content += "some rather long winded html content rather long winded html content rather long winded html content rather long winded html content rather long winded html content";     content += "some rather long winded html content rather long winded html content rather long winded html content </html>";     jeditorpane ep = new jeditorpane("text/html", content);      // main window panel     //jpanel panel = new jpanel();     taskpane.setlayout(new gridbaglayout());     gridbagconstraints c = new gridbagconstraints();     c.fill = gridbagconstraints.horizontal;     c.gridx = 0;     c.gridy = 0;     taskpane.add(ep, c);     c.gridy = 1;     taskpane.add(pnlslider1, c);     c.gridy = 2;     c.insets = new insets(10,0,0,0);     taskpane.add(pnlslider2, c);      // tie , display window         mainwindow.setpreferredsize(new dimension(600, 600));         mainwindow.setlocation(100, 100);         mainwindow.getcontentpane().add(taskpane);         mainwindow.pack();         mainwindow.addwindowlistener(new windowadapter() {             public void windowclosing(windowevent e) {                 system.exit(0);             }         });         mainwindow.setvisible(true);    } } 

borderlayout easier design when compared gridbaglayout. hope you've asked for.i've used netbeans create layout , added jxtaskpane

import org.jdesktop.swingx.jxtaskpane; import org.jdesktop.swingx.jxtaskpanecontainer;   public class newjframe extends javax.swing.jframe {  /**  * creates new form newjframe  */ public newjframe() {     initcomponents();     inittaskpane();     setlocationrelativeto(null); }  private void inittaskpane() {     jxtaskpanecontainer taskpanecontainer = new jxtaskpanecontainer();     jxtaskpane taskpane = new jxtaskpane();     taskpane.setlayout(new java.awt.borderlayout());     taskpane.add(contentpanel, java.awt.borderlayout.center);     taskpane.setcollapsed(false);     taskpanecontainer.add(taskpane);     this.setcontentpane(taskpanecontainer); }  /**  * method called within constructor initialize form.  * warning: not modify code. content of method  * regenerated form editor.  */ @suppresswarnings("unchecked") // <editor-fold defaultstate="collapsed" desc="generated code">                           private void initcomponents() {      contentpanel = new javax.swing.jpanel();     jscrollpane1 = new javax.swing.jscrollpane();     jeditorpane1 = new javax.swing.jeditorpane();     jpanel1 = new javax.swing.jpanel();     jlabel1 = new javax.swing.jlabel();     jslider1 = new javax.swing.jslider();     jlabel2 = new javax.swing.jlabel();     jslider2 = new javax.swing.jslider();      setdefaultcloseoperation(javax.swing.windowconstants.exit_on_close);     setpreferredsize(new java.awt.dimension(400, 500));      contentpanel.setlayout(new java.awt.borderlayout());      jscrollpane1.setpreferredsize(new java.awt.dimension(108, 400));      jeditorpane1.setcontenttype("text/html"); // noi18n     jeditorpane1.settext("<html>\r\n rather long winded html content rather long winded html content rather long winded html content rather long winded html content.some rather long winded html content rather long winded html content rather long winded html content rather long winded html content.some rather long winded html content rather long winded html content rather long winded html content rather long winded html content.some rather long winded html content rather long winded html content rather long winded html content rather long winded html content.some rather long winded html content rather long winded html content rather long winded html content rather long winded html content.some rather long winded html content rather long winded html content rather long winded html content rather long winded html content.\n</html>\r\n");     jscrollpane1.setviewportview(jeditorpane1);      contentpanel.add(jscrollpane1, java.awt.borderlayout.center);      jlabel1.settext("description slider1");      jlabel2.settext("description slider2");      javax.swing.grouplayout jpanel1layout = new javax.swing.grouplayout(jpanel1);     jpanel1.setlayout(jpanel1layout);     jpanel1layout.sethorizontalgroup(         jpanel1layout.createparallelgroup(javax.swing.grouplayout.alignment.leading)         .addgroup(jpanel1layout.createsequentialgroup()             .addcontainergap()             .addgroup(jpanel1layout.createparallelgroup(javax.swing.grouplayout.alignment.leading)                 .addgroup(jpanel1layout.createsequentialgroup()                     .addcomponent(jlabel1)                     .addpreferredgap(javax.swing.layoutstyle.componentplacement.related)                     .addcomponent(jslider1, javax.swing.grouplayout.default_size, javax.swing.grouplayout.default_size, short.max_value))                 .addgroup(jpanel1layout.createsequentialgroup()                     .addcomponent(jlabel2)                     .addpreferredgap(javax.swing.layoutstyle.componentplacement.related)                     .addcomponent(jslider2, javax.swing.grouplayout.default_size, 271, short.max_value)))             .addcontainergap())     );     jpanel1layout.setverticalgroup(         jpanel1layout.createparallelgroup(javax.swing.grouplayout.alignment.leading)         .addgroup(jpanel1layout.createsequentialgroup()             .addcontainergap()             .addgroup(jpanel1layout.createparallelgroup(javax.swing.grouplayout.alignment.leading, false)                 .addcomponent(jslider1, javax.swing.grouplayout.default_size, javax.swing.grouplayout.default_size, short.max_value)                 .addcomponent(jlabel1, javax.swing.grouplayout.preferred_size, 23, javax.swing.grouplayout.preferred_size))             .addpreferredgap(javax.swing.layoutstyle.componentplacement.related)             .addgroup(jpanel1layout.createparallelgroup(javax.swing.grouplayout.alignment.leading, false)                 .addcomponent(jslider2, javax.swing.grouplayout.default_size, javax.swing.grouplayout.default_size, short.max_value)                 .addcomponent(jlabel2, javax.swing.grouplayout.preferred_size, 23, javax.swing.grouplayout.preferred_size))             .addcontainergap())     );      contentpanel.add(jpanel1, java.awt.borderlayout.south);      getcontentpane().add(contentpanel, java.awt.borderlayout.center);      pack(); }// </editor-fold>                          /**  * @param args command line arguments  */ public static void main(string args[]) {     /* set nimbus , feel */     //<editor-fold defaultstate="collapsed" desc=" , feel setting code (optional) ">     /* if nimbus (introduced in java se 6) not available, stay default , feel.      * details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html       */     try {         (javax.swing.uimanager.lookandfeelinfo info : javax.swing.uimanager.getinstalledlookandfeels()) {             if ("nimbus".equals(info.getname())) {                 javax.swing.uimanager.setlookandfeel(info.getclassname());                 break;             }         }     } catch (classnotfoundexception ex) {         java.util.logging.logger.getlogger(newjframe.class.getname()).log(java.util.logging.level.severe, null, ex);     } catch (instantiationexception ex) {         java.util.logging.logger.getlogger(newjframe.class.getname()).log(java.util.logging.level.severe, null, ex);     } catch (illegalaccessexception ex) {         java.util.logging.logger.getlogger(newjframe.class.getname()).log(java.util.logging.level.severe, null, ex);     } catch (javax.swing.unsupportedlookandfeelexception ex) {         java.util.logging.logger.getlogger(newjframe.class.getname()).log(java.util.logging.level.severe, null, ex);     }     //</editor-fold>      /* create , display form */     java.awt.eventqueue.invokelater(new runnable() {         public void run() {             new newjframe().setvisible(true);         }     }); }  // variables declaration - not modify                      private javax.swing.jpanel contentpanel; private javax.swing.jeditorpane jeditorpane1; private javax.swing.jlabel jlabel1; private javax.swing.jlabel jlabel2; private javax.swing.jpanel jpanel1; private javax.swing.jscrollpane jscrollpane1; private javax.swing.jslider jslider1; private javax.swing.jslider jslider2; // end of variables declaration                    } 

output

enter image description here


Comments

Popular posts from this blog

twig - Using Twigbridge in a Laravel 5.1 Package -

jdbc - Not able to establish database connection in eclipse -

firemonkey - How do I make a beep sound in Android using Delphi and the API? -