java - Update border title when JmenuItem is clicked -


my question little complicated please,am trying update bordertitle of jpanel when jmenuitem clicked,i have 3 classes,a implements actionlistener, b jpanel class , c jframe class,here have tried already

public class paneltitle implements actionlistener{      string title;     public paneltitle(){     }                          @override     public void actionperformed(actionevent ae){          sedimentpanel sp = new sedimentpanel();          sp.titledborder.settitle("sediment");           sp.repaint();          sp.revalidate();       } } 

i have in jframe class

 velocitymenuitem.addactionlistener(new paneltitle()); 

here jpanel class

public class sedimentpanel extends jpanel{     public sedimentpanel(){         super();         initcomponents();         initplaceholders();         setborder(titledborder);       }      titledborder titledborder = borderfactory.createtitledborder(null, "border title",titledborder.center,titledborder.default_position);  } 

please how bordertitle change when click jmenuitem? here how referenced in frame class,now nullpointer exception

public class frameclass extends jframe{ private static sedimentpanel sp;     public frameclass(sedimentpanel sp){         this.sp = sp;} } public static void main(string args[]){  frameclass fc = new frameclass(sp); } 

you're making basic mistake here:

public class paneltitle implements actionlistener{      string title;     public paneltitle(){     }                          @override     public void actionperformed(actionevent ae){          sedimentpanel sp = new sedimentpanel(); // ********          sp.titledborder.settitle("sediment");           sp.repaint();          sp.revalidate();     } } 

that new sedimentpanel new reference , calling method on have no effect on original displayed object. don't this, appropriate reference , call method on it.

public class paneltitle implements actionlistener{     string title;     private sedimentpanel sp;      public paneltitle(sedimentpanel sp){  // pass in reference         this.sp = sp;     }      @override     public void actionperformed(actionevent ae){          // sedimentpanel sp = new sedimentpanel(); // ******** no          // sp.titledborder.settitle("sediment");          sp.settitle("sediment"); // better give class method          sp.repaint();          sp.revalidate();     } } 

public class sedimentpanel {     private titledborder titledborder = ....;      public void settitle(string title) {         titledborder.settext(title);     } }    

then when create listener, pass in appropriate reference actual visualized jpanel.

that you're making mistake suggests wouldn't harm read or re-read decent chapter in text on object/reference , represents because foundational mistake you're making.

a working example:

import java.awt.dimension; import java.awt.event.*; import javax.swing.*; import javax.swing.border.*;  public class titleexample {      private static void createandshowgui() {         sedimentpanel sedimentpanel = new sedimentpanel();         paneltitle paneltitle = new paneltitle(sedimentpanel); // pass in reference         jmenuitem menuitem = new jmenuitem("sediment");         menuitem.addactionlistener(paneltitle);         jmenu jmenu = new jmenu("menu");         jmenu.add(menuitem);         jmenubar menubar = new jmenubar();         menubar.add(jmenu);          jframe frame = new jframe("titleexample");         frame.setdefaultcloseoperation(jframe.exit_on_close);         frame.getcontentpane().add(sedimentpanel);         frame.setjmenubar(menubar);         frame.pack();         frame.setlocationrelativeto(null);         frame.setvisible(true);     }      public static void main(string[] args) {         swingutilities.invokelater(() -> createandshowgui());     } } 

class sedimentpanel extends jpanel {     private titledborder titledborder = borderfactory.createtitledborder(null, "border title", titledborder.center,             titledborder.default_position);      public sedimentpanel() {         super();         setborder(titledborder);         setpreferredsize(new dimension(400, 300));     }      public void settitle(string title) {         titledborder.settitle(title);         repaint();     }  } 

class paneltitle implements actionlistener{     string title;     private sedimentpanel sp;      public paneltitle(sedimentpanel sp){  // pass in reference         this.sp = sp;     }      @override     public void actionperformed(actionevent ae){          sp.settitle("sediment"); // better give class method     } } 

Comments

Popular posts from this blog

python Tkinter Capturing keyboard events save as one single string -

android - InAppBilling registering BroadcastReceiver in AndroidManifest -

javascript - Z-index in d3.js -