java - Accessing changes made inside JButton ActionListener outside the ActionListener -


i ll explain code in bit:

i have jcombobox list of items , when jbutton "select" pressed, registers last index of last selected item jcombobox.

now need access index inside main.

here code :

public static final jcombobox c = new jcombobox();  private static final jbutton btnnewbutton = new jbutton("select"); 

and jbutton actionlistener is:

btnnewbutton.addactionlistener(new actionlistener() {

  public void actionperformed(actionevent e)   {        ind = c.getselectedindex();         frame.setvisible(false);     }   });       

so after button pressed, frame closes

but when try access ind inside main

public static void main(string[] args) { system.out.println(ind);} 

i return value of ind = 0

i understand because has been initialized 0 as

static ind = 0  

but how access index outside jbutton actionlistener?

i need use index further in code.

edit

here's mcve

import java.util.arraylist; import javax.swing.jbutton; import javax.swing.jcombobox; import javax.swing.jframe; import javax.swing.springlayout; import java.awt.event.actionevent; import java.awt.event.actionlistener; import javax.swing.uimanager;  public class minimalexprogram { private static string[] description = { "one", "two", "three"};  static int ind;  public static final jcombobox c = new jcombobox(description);  private static final jbutton btnnewbutton = new jbutton("select");   public static void main(string[] args) {  jframe frame = new jframe(); frame.getcontentpane().setforeground(uimanager.getcolor("combobox.disabledbackground")); frame.getcontentpane().setbackground(uimanager.getcolor("editorpane.disabledbackground")); springlayout springlayout = new springlayout(); springlayout.putconstraint(springlayout.north, btnnewbutton, 5, springlayout.north, frame.getcontentpane()); springlayout.putconstraint(springlayout.east, c, -6, springlayout.west, btnnewbutton); springlayout.putconstraint(springlayout.east, btnnewbutton, -10, springlayout.east, frame.getcontentpane()); springlayout.putconstraint(springlayout.west, c, 6, springlayout.west, frame.getcontentpane()); springlayout.putconstraint(springlayout.north, c, 6, springlayout.north, frame.getcontentpane()); frame.getcontentpane().setlayout(springlayout); frame.setsize(500, 150); frame.setlocation(400, 200); frame.setresizable(false); c.setbackground(uimanager.getcolor("combobox.disabledbackground"));   btnnewbutton.addactionlistener(new actionlistener() {  public void actionperformed(actionevent e) {       system.out.print("what want:");       ind = c.getselectedindex();       system.out.println(ind);         frame.setvisible(false); } });        frame.getcontentpane().add(c); btnnewbutton.setforeground(uimanager.getcolor("combobox.buttondarkshadow")); btnnewbutton.setbackground(uimanager.getcolor("editorpane.disabledbackground    "));  frame.getcontentpane().add(btnnewbutton); frame.setvisible(true); system.out.print("what get: "); system.out.println(ind); } } 

i need access ind inside main mentioned before here when print out ind 0 despite whatever choice make inside combobox.

ok, i'm going guess since you've yet show enough information more this, assume that:

  • you're showing 2nd window dialog off of main window
  • that 2nd window jframe , holds jcombobox , button
  • that you're making invisible on button push,
  • that try information combobox, it's 0
  • and may because you're getting information before user has had chance interact 2nd window.

if so, solution make 2nd window modal dialog window such modal jdialog , not jframe. way, calling code know when 2nd window no longer visible since calling code's code flow blocked until 2nd window no longer visible.


edit

proof of concept: change code from:

  final jframe frame = new jframe(); 

to:

  // rename frame variable dialog clarity   final jdialog dialog = new jdialog();   dialog.setmodalitytype(modalitytype.application_modal); 

and works.


but again, i'd rid of unnecessary statics , rid of doing code in main method. instance,...

import javax.swing.abstractaction; import javax.swing.jbutton; import javax.swing.jcombobox; import javax.swing.jdialog; import javax.swing.jframe; import javax.swing.jpanel; import javax.swing.jtextfield; import javax.swing.swingutilities;  import java.awt.dialog.modalitytype; import java.awt.dimension; import java.awt.window; import java.awt.event.actionevent;  @suppresswarnings("serial") public class minimalexprogram3 extends jpanel {    private static void createandshowgui() {       mainpanel mainpanel = new mainpanel();        jframe frame = new jframe("minimalexprogram3");       frame.setdefaultcloseoperation(jframe.dispose_on_close);       frame.getcontentpane().add(mainpanel);       frame.pack();       frame.setlocationbyplatform(true);       frame.setvisible(true);    }     public static void main(string[] args) {       swingutilities.invokelater(new runnable() {          public void run() {             createandshowgui();          }       });    } }  @suppresswarnings("serial") class mainpanel extends jpanel {    private static final int pref_w = 400;    private static final int pref_h = pref_w;    private jtextfield field = new jtextfield(8);    private combopanel combopanel = new combopanel();    jdialog dialog = null;     public mainpanel() {       field.setfocusable(false);        add(field);       add(new jbutton(new showcomboaction("show combo")));    }     @override // make bigger    public dimension getpreferredsize() {       if (ispreferredsizeset()) {          return super.getpreferredsize();       }       return new dimension(pref_w, pref_h);    }     private class showcomboaction extends abstractaction {       public showcomboaction(string name) {          super(name);          int mnemonic = (int) name.charat(0);          putvalue(mnemonic_key, mnemonic);       }        @override       public void actionperformed(actionevent e) {          window mainwin = swingutilities.getwindowancestor(mainpanel.this);          if (dialog == null) {             dialog = new jdialog(mainwin, "dialog", modalitytype.application_modal);             dialog.add(combopanel);             dialog.pack();             dialog.setlocationrelativeto(null);          }           dialog.setvisible(true);           // code called here after dialog no longer visible          string text = combopanel.gettext();          field.settext(text);       }    } }  @suppresswarnings("serial") class combopanel extends jpanel {    private static final string[] description = { "one", "two", "three" };    private int ind;    private jcombobox<string> combo = new jcombobox<>(description);    private string text;    private selectionaction selectionaction = new selectionaction("select");    private jbutton selectionbutton = new jbutton(selectionaction);     public combopanel() {       add(combo);       add(selectionbutton);        combo.setaction(selectionaction);    }     public int getind() {       return ind;    }     public string gettext() {       return text;    }     private class selectionaction extends abstractaction {       public selectionaction(string name) {          super(name);          int mnemonic = (int) name.charat(0);          putvalue(mnemonic_key, mnemonic);       }        @override       public void actionperformed(actionevent e) {          ind = combo.getselectedindex();          if (ind >= 0) {             text = combo.getselecteditem().tostring();          }          window win = swingutilities.getwindowancestor(combopanel.this);          win.dispose();       }    } } 

Comments

Popular posts from this blog

node.js - Using Node without global install -

How to access a php class file from PHPFox framework into javascript code written in simple HTML file? -

java - Null response to php query in android, even though php works properly -