Passing JLabels between classes
Hi,
I'm trying to have multiple classes to complete a goal. I need multiple classes to access one JLabel and update it using the .setText() function. I have a variables class that just holds variables. this is its code:
Code:
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.Timer;
public class variables {
public static String bUrl;
public static String username;
public static String votingip;
public static int vote_count;
public JFrame frame = new JFrame();
public JLabel label = new JLabel ();
public JLabel userlabel= new JLabel ();
public JLabel iplabel = new JLabel ();
public JLabel timerheadinglabel = new JLabel ();
public JLabel votecountlabel = new JLabel ();
public JLabel lastvoteinfolabel = new JLabel ();
public JLabel totaltallylabel = new JLabel ();
public JPanel panel = new JPanel();
}
}
Now I have a class that accesses these to apply values and attributes as normal and it displays the JFrame.
Code:
import javax.swing.JFrame;
import back_end.variables;
public class label_set {
variables call_vars = new variables();
public void addMainResultsWindow(){
call_vars.frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
call_vars.frame.setSize(480, 250);
call_vars.panel.setLayout(null);
call_vars.timerheadinglabel.setText("Time till next vote:");
call_vars.votecountlabel.setText("Waiting");
call_vars.label.setText("Waiting");
call_vars.iplabel.setText("Waiting");
call_vars.userlabel.setText("Waiting");
call_vars.lastvoteinfolabel.setText("Last Vote Info");
call_vars.totaltallylabel.setText("Total Tally");
call_vars.timerheadinglabel.setBounds(175, 47, 150, 14);
call_vars.label.setBounds(214, 70, 60, 14);
call_vars.iplabel.setBounds(90, 125, 150, 14);
call_vars.userlabel.setBounds(90, 150, 120, 14);
call_vars.votecountlabel.setBounds(250, 125, 100, 20);
call_vars.lastvoteinfolabel.setBounds(95, 100, 100, 14);
call_vars.totaltallylabel.setBounds(250, 100, 100, 14);
call_vars.panel.add(call_vars.label);
call_vars.panel.add(call_vars.timerheadinglabel);
call_vars.panel.add(call_vars.userlabel);
call_vars.panel.add(call_vars.iplabel);
call_vars.panel.add(call_vars.votecountlabel);
call_vars.panel.add(call_vars.lastvoteinfolabel);
call_vars.panel.add(call_vars.totaltallylabel);
//this item echos out the first line in the console
System.out.println(call_vars.userlabel);
call_vars.frame.add(call_vars.panel);
call_vars.frame.setVisible(true);
}
}
Then once this is done i try and have the main class that runs this all print the call_vars.userlabel variable, the iteresting this is its not the same as the call in the label_set class. so that class is not updating the variable in the variiables class like i want it too. here is the main calss that excutes this:
Code:
import back_end.variables;
import GUI_components.label_set;
public class start_gui_bot {
variables call_vars = new variables();
public void startGuiBot (String url) throws Exception {
variables.bUrl = url;
call_vars.timeRemaining = variables.timertime;
}
public static void main(String[] args) throws Exception {
start_gui_bot vb = new start_gui_bot ();
label_set ls = new label_set();
variables call_vars = new variables();
vb.startGuiBot(variables.form_url);
ls.addMainResultsWindow();
//this is the se3cond output in the console
System.out.println(call_vars.userlabel);
call_vars.label.setText("Done!");
}
}
and this all out puts:
javax.swing.JLabel[,90,150,120x14,invalid,alignmentX=0.0,alignmentY=0 .0,border=,flags=8388608,maximumSize=,minimumSize= ,preferredSize=,defaultIcon=,disabledIcon=,horizon talAlignment=LEADING,horizontalTextPosition=TRAILI NG,iconTextGap=4,labelFor=,text=Waiting,verticalAl ignment=CENTER,verticalTextPosition=CENTER]
javax.swing.JLabel[,0,0,0x0,invalid,alignmentX=0.0,alignmentY=0.0,bor der=,flags=8388608,maximumSize=,minimumSize=,prefe rredSize=,defaultIcon=,disabledIcon=,horizontalAli gnment=LEADING,horizontalTextPosition=TRAILING,ico nTextGap=4,labelFor=,text=,verticalAlignment=CENTE R,verticalTextPosition=CENTER]
in the console when the main class runs. So my question is how can i get the addMainResultsWindow(); method to change the variables in the variables.java class?