java - Attaching ActionListener to JComboBox -


i working on program has several functions need happen based on user selection jcombobox.

ideally, user selection pulled out , used in series of if/else statements change necessary values.

this error getting:

method addactionlistener in class jcombobox<e> cannot applied given types;         classjcombobox.addactionlistener(this);  required: actionlistener  found: dwtools  reason: actual argument dwtools cannot converted actionlistener method invocation   conversion   e type-variable:   e extends object declared in class jcombobox 

and here code (at moment, have set put chosen class in name jtextfield testing purposes):

import java.awt.*; import javax.swing.*; import java.awt.event.*; import java.text.*; import java.util.*;  public class dwtools extends jframe { private string[] classes = { "bard", "cleric", "druid", "fighter", "paladin", "thief", "ranger", "wizard" }; public jtabbedpane mainpane; public jpanel basic; public jpanel moves; public jpanel spells; private jlabel namejlabel; private jlabel classjlabel; private jtextfield namejtextfield; private jcombobox classjcombobox;  public dwtools() {     createuserinterface(); }  public void createuserinterface() {     container contentpane = getcontentpane();      jpanel toppanel = new jpanel();     toppanel.setlayout(new borderlayout());     contentpane.add(toppanel);      createbasic();     createmoves();     createspells();      mainpane = new jtabbedpane();     mainpane.addtab("basic information", basic);     mainpane.addtab("character moves", moves);     mainpane.addtab("character spells", spells);     toppanel.add(mainpane, borderlayout.center);      settitle("dungeon world tools");     setsize(500, 500);     setvisible(true); }  public void createbasic() {     basic = new jpanel();     basic.setlayout( null );      jlabel namejlabel = new jlabel("name:");     namejlabel.setbounds(10, 10, 50, 25);     basic.add(namejlabel);      jtextfield namejtextfield = new jtextfield();     namejtextfield.setbounds( 60, 10, 100, 25 );     basic.add( namejtextfield );      jlabel classjlabel = new jlabel("class:");     classjlabel.setbounds(10, 60, 50, 25);     basic.add(classjlabel);      arrays.sort(classes);      jcombobox classjcombobox = new jcombobox(classes);     classjcombobox.setbounds( 60, 60, 100, 25 );     classjcombobox.setmaximumrowcount( 8 );     basic.add(classjcombobox);     classjcombobox.addactionlistener(this);  public void classjcomboboxactionperformed(actionevent event) {     jcombobox box = (jcombobox)event.getsource();     string chosenclass = (string)box.getselecteditem();     updateinfo(chosenclass); }  protected void updateinfo(string cclass) {     string chosen = cclass;     namejtextfield.settext("chosen:" + chosen); }  public void createmoves() {     moves = new jpanel();     moves.setlayout( null ); }  public void createspells() {     spells = new jpanel();     spells.setlayout( null ); }  public static void main(string[] args) {     dwtools application = new dwtools();     application.setdefaultcloseoperation(jframe.exit_on_close); } 

}

in addition jbnizets answer, i'd emphasize solution should dedicated actionlistener combo box. implementing interface in "main class" can lead mess when multiple action listeners required. 1 solution be

classjcombobox.addactionlistener(new actionlistener() {     @override     public void actionperformed(actionevent e)     {          // call existing method           // handling event:          classjcomboboxactionperformed(e);     } }); 

Comments

Popular posts from this blog

java - Intellij Synchronizing output directories .. -

git - Initial Commit: "fatal: could not create leading directories of ..." -