java - Accessing Components in JFrame from External Class -


i having trouble accessing several components in jframe use settext("...") method. main in seperate class, because actual program has many windows need managed @ same time.

public gamewindow() {      initialize();     gameframe.setvisible(true); }  private void initialize() {          gameframe = new jframe();  jtextpane gametextpane = new jtextpane();       // text pane contain game text     gametextpanel.add(gametextpane); 

and main:

public class gamemain {     public static gamewindow gw = new gamewindow();     //i have tried using code below various numbers, "settext()" method never available     gw.getgameframe().getcontentpane().getcomponent(x); } 

i trying set text of seperate class, can not access components. ultimately, final code should this:

public static void main(string[] args) {      // make gui , initilize     changethetext(); }  public static void changethetext() {     [code access textfield].settext("hello world"); } 

i have tried many different methods i've found searching around, don't understand of them, , none of them still allow me access methods need.

move declaration of jtextpane out of initialize method make parameter can acces anytime within class. make accessible class can either make public or add set method. this:

public class gamewindow {     private jtextpane gametextpane;     ...     private void initialize(){...}     ...     public void settext(string s) {         gametextpane.settext(s);     } } 

to change text main class:

gw.settext("this cool text"); 

Comments

Popular posts from this blog

Change php variable from jquery value using ajax (same page) -

Pull out data related to my apps from Android Play Store and iOS App Store -

How can I fetch data from a web server in an android application? -