Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 8 of 8

Thread: Passing variable from one class to another

  1. #1
    Junior Member
    Join Date
    Oct 2013
    Posts
    15
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Passing variable from one class to another

    String user1 = JOptionPane.showInputDialog("Please enter the first user's name"); 
    		JOptionPane.showMessageDialog(null, user1 + " will go first! good luck!");

    and in my other class i have

     
    		public tuna(){
    				super("title bar");
    				setLayout(new FlowLayout());
    				JPanel panel = new JPanel();
    				JPanel panel1 = new JPanel();
    				q1 = new JLabel(user1);
    				a1 = new JButton("A");
    				a2 = new JButton("B");
    				a3 = new JButton("C");
    				a4 = new JButton("D");

    I want whatever the user inputs as 'user1' to be printed in the q1 = new JLabel(user1);

    how would i go about doing this?

    Thanks


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Passing variable from one class to another

    You could pass the String: user1 in the Tuna class's constructor.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Passing variable from one class to another

    Pass the desired value to Tuna's constructor (class names should be capitalized).

    public Tuna( String user1 ) { // the other stuff }

  4. #4
    Junior Member
    Join Date
    Oct 2013
    Posts
    15
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Passing variable from one class to another

    I'm not sure i follow :S how would I pass the desired value to Tuna's constructor from my other class :S

  5. #5
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Passing variable from one class to another

    When you create an instance of the Tuna class you would pass the String in its constructor the same way the String is passed to the JLabel class's constructor:
    	q1 = new JLabel(user1);  //  pass user1 to JLabel's constructor
    If you don't understand my answer, don't ignore it, ask a question.

  6. #6
    Junior Member
    Join Date
    Oct 2013
    Posts
    15
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Passing variable from one class to another

    What would i type though?

  7. #7
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Passing variable from one class to another

    Post the code where you create an instance of the Tuna class using the new operator.

    Compare that statement with the code that creates a new instance of the JLabel class in post#5


    See how to code the Tuna class's constructor in post#3
    If you don't understand my answer, don't ignore it, ask a question.

  8. #8
    Member
    Join Date
    Jul 2013
    Location
    Franklin, TN
    Posts
    47
    Thanks
    3
    Thanked 4 Times in 4 Posts

    Default Re: Passing variable from one class to another

    Quote Originally Posted by kingsta View Post
    What would i type though?
    public Tuna(String yourStringVariableHere){ //unnecessary identifier but just to make it obvious..
         q1 = new JLabel(yourStringVariableHere);
    }

    Passing the string variable passed to your class constructor (Tuna), to the constructor for the JLabel object you are initializing.

Similar Threads

  1. Replies: 18
    Last Post: March 30th, 2013, 09:11 AM
  2. Passing as a parameter, an object + another variable
    By goochasauras in forum What's Wrong With My Code?
    Replies: 6
    Last Post: December 11th, 2012, 11:13 PM
  3. Need help with passing user defined variable in SQLJ
    By redkryptonite in forum JDBC & Databases
    Replies: 1
    Last Post: March 7th, 2012, 05:47 AM
  4. Passing a value to another class
    By Semple in forum What's Wrong With My Code?
    Replies: 1
    Last Post: December 4th, 2010, 05:49 PM
  5. Replies: 0
    Last Post: April 11th, 2010, 08:56 AM