Java (swing) toolbar not showing -


i have been following tutorial:
java toolbar tutorial
when tried insert toolbar gui, doesn't show up. can kindly give me advice on do? code far this:

import javax.swing.box; import java.awt.container; import java.awt.eventqueue; import java.awt.event.actionevent; import java.awt.event.actionlistener; import javax.swing.grouplayout; import javax.swing.jbutton; import javax.swing.jcomponent; import javax.swing.jframe; import javax.swing.jpanel; import java.awt.event.keyevent; import javax.swing.imageicon; import javax.swing.jmenu; import javax.swing.jmenubar; import javax.swing.jmenuitem; import javax.swing.abstractaction; import static javax.swing.action.mnemonic_key; import static javax.swing.action.small_icon; import static javax.swing.jframe.exit_on_close; import javax.swing.keystroke; import java.awt.borderlayout; import java.awt.event.itemevent; import java.awt.event.itemlistener; import javax.swing.borderfactory; import javax.swing.jcheckboxmenuitem; import javax.swing.jlabel; import javax.swing.jtoolbar;  public class gui extends jframe {  private jlabel statusbar;  public gui() {      initui(); }  private void initui() {       createmenubar();     createtoolbar();      statusbar = new jlabel("ready");     statusbar.setborder   (borderfactory.createetchedborder());   add(statusbar, borderlayout.south);      jbutton quitbutton = new jbutton("quit");     quitbutton.settooltiptext("a button component");      quitbutton.addactionlistener(new actionlistener()   {         @override         public void actionperformed(actionevent event)   {             system.exit(0);         }     });      quitbutton.setmnemonic(keyevent.vk_b);      createlayout(quitbutton);      settitle("typing tutor");     setsize(360, 250);     setlocationrelativeto(null);     setdefaultcloseoperation(exit_on_close); }  private void createmenubar() {      jmenubar menubar = new jmenubar();      imageicon iconnew = new imageicon("new.png");     imageicon iconopen = new imageicon("open.png");     imageicon iconsave = new imageicon("save.png");     imageicon iconexit = new imageicon("exit.png");      jmenu filemenu = new jmenu("file");     filemenu.setmnemonic(keyevent.vk_f);      // old code       jmenu impmenu = new jmenu("import");     filemenu.setmnemonic(keyevent.vk_i);      jmenuitem newsfmi = new jmenuitem("import newsfeed list...");     jmenuitem bookmmi = new jmenuitem("import bookmarks...");     jmenuitem mailmi = new jmenuitem("import mail...");      impmenu.add(newsfmi);     impmenu.add(bookmmi);     impmenu.add(mailmi);      jmenuitem newmi = new jmenuitem("new", iconnew);     jmenuitem openmi = new jmenuitem("open", iconopen);     jmenuitem savemi = new jmenuitem("save", iconsave);      jmenuitem exitmi = new jmenuitem("exit", iconexit);     exitmi.setmnemonic(keyevent.vk_e);     exitmi.settooltiptext("exit application");     exitmi.addactionlistener(new actionlistener() {         @override         public void actionperformed(actionevent event)       {                 system.exit(0);             }         });          // new code          jmenu viewmenu = new jmenu("view");         viewmenu.setmnemonic(keyevent.vk_v);          jcheckboxmenuitem sbarmi = new jcheckboxmenuitem  ("show status bar");         sbarmi.setmnemonic(keyevent.vk_s);         sbarmi.setdisplayedmnemonicindex(5);         sbarmi.setselected(true);          sbarmi.additemlistener(new itemlistener() {              @override             public void itemstatechanged(itemevent e) {                  if (e.getstatechange() ==   itemevent.selected) {                     statusbar.setvisible(true);                 } else {                     statusbar.setvisible(false);                 }              }          });          // newer code         jmenu helpmenu = new jmenu("help");           viewmenu.add(sbarmi);         filemenu.add(newmi);         filemenu.add(openmi);         filemenu.add(savemi);         filemenu.addseparator();         filemenu.add(impmenu);         filemenu.addseparator();         filemenu.add(exitmi);           menubar.add(filemenu);         menubar.add(viewmenu);         menubar.add(box.createhorizontalglue());         menubar.add(helpmenu);          setjmenubar(menubar);     }   private void createtoolbar() {          jtoolbar toolbar = new jtoolbar();          imageicon icon = new imageicon("exit.png");          jbutton exitbutton = new jbutton(icon);         toolbar.add(exitbutton);          exitbutton.addactionlistener(new actionlistener()   {             @override             public void actionperformed(actionevent event)   {                 system.exit(0);             }         });          add(toolbar, borderlayout.north);             }      private void createlayout(jcomponent... arg) {          container pane = getcontentpane();         grouplayout gl = new grouplayout(pane);         pane.setlayout(gl);          gl.setautocreatecontainergaps(true);          gl.sethorizontalgroup(gl.createsequentialgroup()                 .addcomponent(arg[0])         );          gl.setverticalgroup(gl.createsequentialgroup()                 .addcomponent(arg[0])         );          pack();     }      public static void main(string[] args) {          eventqueue.invokelater(new runnable() {              @override             public void run() {                 gui ex = new gui();                 ex.setvisible(true);             }         });     } } 

i checked stackoverflow , google searches ones i've seen either android and/or can understand. advice appreciated. thank you.

you've changed layout after creating , adding jtoolbar (to grouplayout - don't me started on choice)

so, if change createlayout creates secondary component, like...

private void createlayout(jcomponent... arg) {      jpanel pane = new jpanel();     //container pane = getcontentpane();     grouplayout gl = new grouplayout(pane);     pane.setlayout(gl);      gl.setautocreatecontainergaps(true);      gl.sethorizontalgroup(gl.createsequentialgroup()                     .addcomponent(arg[0])     );      gl.setverticalgroup(gl.createsequentialgroup()                     .addcomponent(arg[0])     );      add(pane);      pack(); } 

it display like...

frame

because both content , jtoolbar need live in contentpane, need concious of changes make it. solution makes secondary pane, onto "main" content can placed, in ever manner want, added contentpane still using borderlayout


Comments

Popular posts from this blog

r - how do you merge two data frames the best way? -

How to debug "expected android.widget.TextView but found java.lang.string" in Android? -

php - mySQL problems with this code? -