java - Difference Between ItemEvent.getStateChange() and checkbox.getState() when using it on ItemListener? -


so i'm new java , i'm trying make simple text editor 1st java program. i'm working on text wrapping. code looks this.

public class wordwraplistener implements itemlistener {     public void itemstatechanged (itemevent e) {         if(wrapmenuitem.getstate() == true) { // changed line of code             editortextarea.setlinewrap(true);             editortextarea.setwrapstyleword(true);             frame.repaint();         } else {             editortextarea.setlinewrap(false);             editortextarea.setwrapstyleword(false);             frame.repaint();         } 

now code above not work. googled how make itemslistener , found answer below:

public class wordwraplistener implements itemlistener {     public void itemstatechanged (itemevent e) {         if(e.getstatechange() == itemevent.selected) { // changed line             editortextarea.setlinewrap(true);             editortextarea.setwrapstyleword(true);             frame.repaint();         } else {             editortextarea.setlinewrap(false);             editortextarea.setwrapstyleword(false);             frame.repaint();         }     } } // end inner class 

i'm little confused why original code not work? want better understanding on cause based on understanding 2 pretty same difference getstate() method checkbox output true/false , getstatechange() itemevent output selected/deselected?

if there way make 1st code working? using getstate() == true??

edit:

here's snippet of code uses listener. declared jtextarea instance variable.

private void go() {     // make frame     jframe frame = new jframe("dvtexteditor");      //make menu bar , menu     jmenubar menubar = new jmenubar();     jmenu filemenu = new jmenu("file");     jmenu formatmenu = new jmenu("format");      //make menu items file menu     jmenuitem newmenuitem = new jmenuitem("new");     jmenuitem openmenuitem = new jmenuitem("open");     jmenuitem savemenuitem = new jmenuitem("save");      //add menu items in file menu     filemenu.add(newmenuitem);     filemenu.add(openmenuitem);     filemenu.add(savemenuitem);      //action listeners new , save menu items     newmenuitem.addactionlistener(new newmenulistener());     openmenuitem.addactionlistener(new openmenulistener());     savemenuitem.addactionlistener(new savemenulistener());      //make menu items format menu     jcheckboxmenuitem wrapmenuitem = new jcheckboxmenuitem("word wrap ");      //add menu items in format menu     formatmenu.add(wrapmenuitem);      //action listener word wrap item menu     wrapmenuitem.additemlistener(new wordwraplistener());  // <------------------------------      // add menu in menu bar     menubar.add(filemenu);     menubar.add(formatmenu);      // meke text area , put inside scrollpane     editortextarea = new jtextarea(15, 50);     jscrollpane escrollbar = new jscrollpane(editortextarea);     escrollbar.setverticalscrollbarpolicy(scrollpaneconstants.vertical_scrollbar_always);     escrollbar.sethorizontalscrollbarpolicy(scrollpaneconstants.horizontal_scrollbar_always);      // add menu bar , text area in frame     frame.setjmenubar(menubar);     frame.getcontentpane().add(borderlayout.center, escrollbar);      // enable frame     frame.setdefaultcloseoperation(jframe.exit_on_close);     frame.setsize(800, 600);     frame.setvisible(true);  } 

thank you,

so turns out added listener wrong component kgeorgiy suspected. tried running both code listener added wrapmenuitem.additemlistener(new wordwraplistener());

both codes working although prefer use latter since 1 don't have declare wrapmenuitem instance variable.


Comments

Popular posts from this blog

java - SSE Emitter : Manage timeouts and complete() -

jquery - uncaught exception: DataTables Editor - remote hosting of code not allowed -

java - How to resolve error - package com.squareup.okhttp3 doesn't exist? -