java - ActionListeners JMenuItems - Cross classes -


i'm making poor attempt @ actionlisteners here.

i'm trying use actionlisteners run code in class (addform.java) once jmenuitem (in mainmenu.java) clicked.

first, here's code: mainmenu.java

package carparksystem;  import javax.swing.jframe; import javax.swing.*; import java.awt.event.actionlistener; import java.awt.event.actionevent; import java.awt.*; import java.awt.event.*;  public class mainmenu extends jframe { public mainmenu() {     jmenubar mainmenu = new jmenubar();     jmenu main = new jmenu("menu");      mainmenu.add(main);      jmenuitem addcar = new jmenuitem("add car");     addcar.setactioncommand("add");     main.add(addcar);     //addcar.addactionlistener();      jmenuitem removecar = new jmenuitem("remove car");     removecar.setactioncommand("remove");     main.add(removecar);      jmenuitem searchcars = new jmenuitem("search cars");     searchcars.setactioncommand("search");     main.add(searchcars);      setjmenubar(mainmenu);      /*     //add action listener add car button     addcar.addactionlistener     (         new actionlistener()             {                 @override                 public void actionperformed(actionevent e)                  {                     mainmenu.windowclosed();                 }                 }             );     */ } } 

addform.java:

package carparksystem;  import javax.swing.buttongroup; import javax.swing.grouplayout; import javax.swing.jbutton; import javax.swing.jlabel; import javax.swing.jradiobutton; import javax.swing.jtextfield; import javax.swing.windowconstants; import javax.swing.jframe;  public class addform extends jframe { public addform() {             jlabel regnumlabel = new jlabel("registration number:");     jlabel highvallabel = new jlabel("high value?");     jlabel largelabel = new jlabel("large vehicle?");      jradiobutton btnyeshighval = new jradiobutton("yes", false);     jradiobutton btnnohighval = new jradiobutton("no", true);     jradiobutton btnyeslarge = new jradiobutton("yes", false);     jradiobutton btnnolarge = new jradiobutton("no", true);      buttongroup highval = new buttongroup();        //allows 1 radio button group selected     highval.add(btnyeshighval);     highval.add(btnnohighval);      buttongroup largecar = new buttongroup();       //allows 1 radio button group selected     largecar.add(btnyeslarge);     largecar.add(btnnolarge);      jtextfield regnumfield = new jtextfield();      jbutton addcar = new jbutton("   add  ");     jbutton addcancel = new jbutton("cancel");      grouplayout addlayout = new grouplayout(getcontentpane());      //chosen display components in group layout     getcontentpane().setlayout(addlayout);     addlayout.setautocreategaps(true);     addlayout.setautocreatecontainergaps(true);      addlayout.sethorizontalgroup(addlayout.createsequentialgroup()         .addgroup(addlayout.createparallelgroup(grouplayout.alignment.leading)             .addcomponent(regnumlabel)             .addcomponent(highvallabel)             .addcomponent(largelabel))         .addgroup(addlayout.createparallelgroup(grouplayout.alignment.leading)             .addcomponent(regnumfield)             .addgroup(addlayout.createsequentialgroup()                 .addgroup(addlayout.createparallelgroup(grouplayout.alignment.leading)                 .addcomponent(btnyeshighval)                 .addcomponent(btnyeslarge))             .addgroup(addlayout.createparallelgroup(grouplayout.alignment.leading)                 .addcomponent(btnnohighval)                 .addcomponent(btnnolarge))))         .addgroup(addlayout.createparallelgroup(grouplayout.alignment.leading)             .addcomponent(addcar)             .addcomponent(addcancel))     );      addlayout.setverticalgroup(addlayout.createsequentialgroup()         .addgroup(addlayout.createparallelgroup(grouplayout.alignment.baseline)             .addcomponent(regnumlabel)             .addcomponent(regnumfield)             .addcomponent(addcar))         .addgroup(addlayout.createparallelgroup(grouplayout.alignment.leading)             .addgroup(addlayout.createsequentialgroup()                 .addgroup(addlayout.createparallelgroup(grouplayout.alignment.baseline)                     .addcomponent(highvallabel)                     .addcomponent(btnyeshighval)                     .addcomponent(btnnohighval))                 .addgroup(addlayout.createparallelgroup(grouplayout.alignment.baseline)                     .addcomponent(largelabel)                         .addcomponent(btnyeslarge)                     .addcomponent(btnnolarge)))                 .addcomponent(addcancel))         );      setsize(375, 150);     settitle("add car");     setdefaultcloseoperation(windowconstants.dispose_on_close); } 

on main frame have drawn series of shapes appear on frame @ first. , there jmenu above jmenuitems add, remove , search cars in menu.

once these add remove , search 'menu buttons' clicked open corresponding form allow user input data.

when run code , without action listeners, runs normal menus don't link @ all. it's if have no meaning?

some basic issues:

  • if 1 class have effect on another, need reference other class can methods.
  • thus if mainmenu class needs change state of addform class, 1 way occur mainmenu need addform field, , more importantly, field must reference current active addform object.
  • an better program structure use m-v-c or model-view-control structure 2 views connected shared model, may bit advanced simple program. still if want follow best practices, way go. this example shows more simple view-control idea work.
  • it's bad idea gui have multiple top-level windows, java means multiple jframes. better have 1 main window , other views can swapped in if need cardlayout, or can displayed in dialog windows if need be. please check out the use of multiple jframes, good/bad practice?.

for simple non-mvc example, have 1 class calling methods of second. assume jpanel holds jlist called view1 has public method, additem(string item) adds items tot jlist's model:

public class view1 extends jpanel {    private static final string prototype = string.format("%50s", " ");    private defaultlistmodel<string> listmodel = new defaultlistmodel<>();    private jlist<string> list = new jlist<>(listmodel);     public view1() {       list.setprototypecellvalue(prototype);       list.setvisiblerowcount(8);       jscrollpane scrollpane = new jscrollpane(list);       scrollpane.setverticalscrollbarpolicy(jscrollpane.vertical_scrollbar_always);       add(scrollpane);    }     public void additem(string item) {       listmodel.addelement(item);    } } 

consider second jpanel class, view2, holds jtextarea , jbutton , instance of view1. second class call additem(...) method of view1:

public class view2 extends jpanel {    private view1 view1;    private jtextfield textfield = new jtextfield(10);     public view2(view1 view1) {       action additemaction = new additemaction();       this.view1 = view1;       add(textfield);       add(new jbutton(additemaction));       textfield.setaction(additemaction);    }     private class additemaction extends abstractaction {       public additemaction() {          super("add item");          putvalue(mnemonic_key, keyevent.vk_a);       }        @override       public void actionperformed(actionevent e) {          view1.additem(textfield.gettext()); // *** calls view1's method here          textfield.selectall();       }    } } 

you create 2 classes, passing view1 view2's constructor

  view1 view1 = new view1();   view2 view2 = new view2(view1); 

and put 1 jframe other non-modal jdialog , display. instance:

import java.awt.dialog.modalitytype; import java.awt.event.actionevent; import java.awt.event.keyevent;  import javax.swing.*;  public class simpleguis {    private static void createandshowgui() {       view1 view1 = new view1();       view2 view2 = new view2(view1);        jframe frame = new jframe("simpleguis");       frame.setdefaultcloseoperation(jframe.dispose_on_close);       frame.getcontentpane().add(view1);       frame.pack();       frame.setlocationrelativeto(null);       frame.setvisible(true);        jdialog dialog = new jdialog(frame, "view2", modalitytype.modeless);       dialog.add(view2);       dialog.pack();       dialog.setlocationbyplatform(true);       dialog.setvisible(true);    }     public static void main(string[] args) {       swingutilities.invokelater(new runnable() {          public void run() {             createandshowgui();          }       });    } }  class view1 extends jpanel {    private static final string prototype = string.format("%50s", " ");    private defaultlistmodel<string> listmodel = new defaultlistmodel<>();    private jlist<string> list = new jlist<>(listmodel);     public view1() {       list.setprototypecellvalue(prototype);       list.setvisiblerowcount(8);       jscrollpane scrollpane = new jscrollpane(list);       scrollpane.setverticalscrollbarpolicy(jscrollpane.vertical_scrollbar_always);       add(scrollpane);    }     public void additem(string item) {       listmodel.addelement(item);    } }  class view2 extends jpanel {    private view1 view1;    private jtextfield textfield = new jtextfield(10);     public view2(view1 view1) {       action additemaction = new additemaction();       this.view1 = view1;       add(textfield);       add(new jbutton(additemaction));       textfield.setaction(additemaction);    }     private class additemaction extends abstractaction {       public additemaction() {          super("add item");          putvalue(mnemonic_key, keyevent.vk_a);       }        @override       public void actionperformed(actionevent e) {          view1.additem(textfield.gettext());          textfield.selectall();       }    } } 

Comments

Popular posts from this blog

ruby on rails - RuntimeError: Circular dependency detected while autoloading constant - ActiveAdmin.register Role -

c++ - OpenMP unpredictable overhead -

javascript - Wordpress slider, not displayed 100% width -