Results 1 to 2 of 2
  1. #1
    nat45928 is offline Member
    Join Date
    Apr 2011
    Posts
    7
    Rep Power
    0

    Default 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:

    Java 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.

    Java 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:

    Java 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?

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    Your "out puts" is simply the toString() representation of a JLabel, and should be the expected output given your trying to call println on a JLabel rather than its text.

    More importantly, your design is very un-OOPish and is liable to break. I urge you to study a book on OOP (it doesn't have to be OOP as it applies to Java, but that would help too) before trying to create complex Java GUI's.

Similar Threads

  1. Passing variables through classes?
    By EternalFacepalm in forum New To Java
    Replies: 4
    Last Post: 04-27-2011, 12:46 AM
  2. Passing data between 3 classes
    By tocakt in forum New To Java
    Replies: 3
    Last Post: 04-11-2011, 10:50 PM
  3. Passing objects to classes
    By Catfish1 in forum New To Java
    Replies: 3
    Last Post: 11-30-2010, 05:27 PM
  4. passing an array between classes
    By gisler in forum New To Java
    Replies: 10
    Last Post: 04-10-2009, 10:31 PM
  5. help with passing objects between classes
    By aruna1 in forum New To Java
    Replies: 7
    Last Post: 03-22-2009, 02:41 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •