java - Using an individual String with a JList<String> for an action -


i writing pokedex in java swing , plan have each pokemon represented string in jlist object. upon user clicking pokemon, want open new tabbed panel within main frame holds information specific pokemon sorted categories. can't figure out how string implement action. willing change type of list if fixes problem. current code below:

//this intermediate pokedex application windows.  code application below. //import needed jlibraries program import javax.swing.*; import java.awt.*; import java.awt.image.*; import java.awt.color; import java.awt.event.*; import javax.swing.uimanager.*; import javax.swing.border.*; import javax.imageio.*; import java.io.*; import java.lang.*; import javax.swing.jradiobuttonmenuitem.*; import java.util.*; import javax.swing.joptionpane;  //everything below pokedex class public class pokedex extends jframe {     string[] gen1 = {"bulbasaur", "ivysaur"};     jlist<string> gen1list = new jlist<string>(gen1);     jscrollpane browsepane = new jscrollpane(gen1list);     jpanel topindexpanel = new jpanel();      int intx;      public static void main(string[] args)     {         toolkit.getdefaulttoolkit().setdynamiclayout(true);         system.setproperty("sun.awt.noerasebackground", "true");         jframe.setdefaultlookandfeeldecorated(true);         jdialog.setdefaultlookandfeeldecorated(true);          try         {             uimanager.setlookandfeel("com.jtattoo.plaf.noire.noirelookandfeel");         }         catch (classnotfoundexception ex)         {             java.util.logging.logger.getlogger(pokedex.class.getname()).log(java.util.logging.level.severe, null, ex);         }         catch (instantiationexception ex)         {             java.util.logging.logger.getlogger(pokedex.class.getname()).log(java.util.logging.level.severe, null, ex);         }         catch (illegalaccessexception ex)         {             java.util.logging.logger.getlogger(pokedex.class.getname()).log(java.util.logging.level.severe, null, ex);         }         catch (javax.swing.unsupportedlookandfeelexception ex)         {             java.util.logging.logger.getlogger(pokedex.class.getname()).log(java.util.logging.level.severe, null, ex);         }         pokedex f = new pokedex();         dimension screensize = toolkit.getdefaulttoolkit().getscreensize();                 f.setbounds(0,0,screensize.width, screensize.height);                 f.setvisible(true);                 f.settitle("pokedex");     }      public pokedex()     {         add(browsepane, borderlayout.west);         gen1list.setselectionmode(listselectionmodel.single_interval_selection);         gen1list.setbackground(color.black);         gen1list.setforeground(color.white);         gen1list.setfont(new font("arial", font.plain, 18));         addwindowlistener(         new windowadapter()         {             @override             public void windowclosing(windowevent e)             {             system.exit(0);             }         });          string selected = gen1list.getselectedvalue().tostring();         system.out.println(selected);     } } 

edit

i able create work around below code , couple of modifications need.

gameredlist.addlistselectionlistener(new listselectionlistener()         {             public void valuechanged(listselectionevent arg0)             {                 if (!arg0.getvalueisadjusting())                 {                   actionstring = gameredlist.getselectedvalue().tostring();                   if (actionstring=="bulbasaur")                   {                       jframe testframe = new jframe();                       testframe.setbounds(0,0,500,500);                       testframe.setbackground(color.black);                       testframe.setforeground(color.white);                       testframe.settitle("success");                       testframe.setvisible(true);                    }                 }             }         }); 

upon user clicking pokémon

you can't assume user click on pokémon. user use mouse or keyboard. notified when item in jlist selected use listselectionlistener. read section swing tutorial on selecting items in list more information , working examples

i want open new tabbed panel within main frame holds information specific pokemon

you don't want open new tabbed panel. instead create gui component. selection changed update data displayed. is, when designing gui avoid adding/removing components.

also don't need windowlistener. old awt code. in swing can use:

setdefaultcloseoperation(jframe.exit_on_close); 

Comments

Popular posts from this blog

twig - Using Twigbridge in a Laravel 5.1 Package -

jdbc - Not able to establish database connection in eclipse -

Kivy: Swiping (Carousel & ScreenManager) -