Results 1 to 6 of 6
  1. #1
    RchrdJ28 is offline Member
    Join Date
    Nov 2011
    Posts
    3
    Rep Power
    0

    Default Transfering JLabels Between actionPerformed Methods

    Hello,

    I have a few actionPerformed methods that correspond with buttons in one JFrame. I also have a button that when clicked, opens a new JFrame. I am trying to take one JLabel that is created in one actionPerformed method and pass it to the actionPerformed method that opens a new JFrame, thus displaying the JLabel in that second JFrame.

    When I try to do that by simply adding

    Java Code:
    examplePanel.add(exampleLabel)
    to the JFrame-creating actionPerformed method Eclipse recognizes exampleLabel as a valid JLabel, but when the button is pressed, the label is not displayed in the JFrame.

    So my question is: how do I transfer a JLabel from one actionPerformed method to another?

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

    Default Re: Transfering JLabels Between actionPerformed Methods

    Myself I'm having trouble fully understanding just what your problem is as there's a lot of information not yet said.

  3. #3
    RchrdJ28 is offline Member
    Join Date
    Nov 2011
    Posts
    3
    Rep Power
    0

    Default Re: Transfering JLabels Between actionPerformed Methods

    Sorry, I'm trying to make a scoreboard program. Here is the code I have so far:

    Java Code:
    public class main {
    	public static void main(String args[]){
    		
    		//Initiate frame object
    		frames frameOne = new frames();
    		frameOne.buttonView();
    		
    		//Make frame visible
    		frameOne.setVisible(true);
    		
    	}
    }
    
    public class frames extends JFrame{
    
    	//Class-level variables
    	int homeCount = 0;
    	int awayCount = 0;
    	JLabel homeLabel = new JLabel();
    	JLabel awayLabel = new JLabel();
    	
    	public void buttonView(){
    		
    		//Set buttons
    		JButton addHome = null;
    		JButton subtractHome = null;
    		JButton addAway = null;
    		JButton subtractAway = null;
    		JButton displayScore = null;
    		
    		//Set basic window parameters
    		setTitle("Scoreboard - Button View");
    		setSize(720, 480);
    		setBackground(Color.gray);
    		
    		//Create window layout style
    		final Panel buttonPanel = new Panel();
    		buttonPanel.setLayout (new GridLayout(3, 2));
    		getContentPane().add(buttonPanel);
    		
    		//Initialize buttons
    		addHome = new JButton("Add to HOME score");
    		addHome.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent addToHome){
                    //Increase counter when button is pressed
                	homeCount++;
                	//Set label text
                	homeLabel.setText("Score: " + homeCount);
                	
                }
            });
    		buttonPanel.add(addHome);
    
    		subtractHome = new JButton("Subtract from HOME score");
    		subtractHome.addActionListener(new ActionListener(){
    			public void actionPerformed(ActionEvent subtractFromHome){
    				//Decrease counter when button is pressed
    				homeCount--;
    				//Set label text
    				homeLabel.setText("Score: " + homeCount);
    			}
    		});
    		buttonPanel.add(subtractHome);
    		
    		addAway = new JButton("Add to AWAY score");
    		addAway.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent addToAway){
                    //Increase counter when button is pressed
                	awayCount++;
                	//Set label text
                	awayLabel.setText("Score: " + awayCount);
                }
            });
    		buttonPanel.add(addAway);
    		
    		subtractAway = new JButton("Subtract from AWAY score");
    		subtractAway.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent subtractFromAway){
        
                	//Decrease counter when button is pressed
                	awayCount++;
                	//Set label text
                	awayLabel.setText("Score: " + awayCount);
                }
            });
    		buttonPanel.add(subtractAway);
    		
    		displayScore = new JButton("Display Score");
    		displayScore.addActionListener(new ActionListener(){
    			public void actionPerformed(ActionEvent displayWindow){
    				
    				//Initiate new JFrame
    				JFrame scoreFrame = new JFrame(); 
                	
    				//Set JFrame to visible
    				scoreFrame.setVisible(true);
    				
    				//Set JFrame parameters
                	scoreFrame.setTitle("Scoreboard - Score View");
            		scoreFrame.setSize(300, 250);
            		scoreFrame.setBackground(Color.gray);
            		
            		//Create window layout style
            		JPanel scorePanel = new JPanel();
            		scorePanel.setLayout (new BorderLayout());
            		getContentPane().add(scorePanel);
            		scorePanel.add(awayLabel);
                	scorePanel.revalidate();
    
    			}
    		});
    		buttonPanel.add(displayScore);
    	}
    }
    Specifically, I am trying to take the homeLabel JLabel from this method:

    Java Code:
    addHome = new JButton("Add to HOME score");
    		addHome.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent addToHome){
                    //Increase counter when button is pressed
                	homeCount++;
                	//Set label text
                	homeLabel.setText("Score: " + homeCount);
                	
                }
            });
    		buttonPanel.add(addHome);
    and place it in this method:

    Java Code:
    displayScore = new JButton("Display Score");
    		displayScore.addActionListener(new ActionListener(){
    			public void actionPerformed(ActionEvent displayWindow){
    				
    				//Initiate new JFrame
    				JFrame scoreFrame = new JFrame(); 
                	
    				//Set JFrame to visible
    				scoreFrame.setVisible(true);
    				
    				//Set JFrame parameters
                	scoreFrame.setTitle("Scoreboard - Score View");
            		scoreFrame.setSize(300, 250);
            		scoreFrame.setBackground(Color.gray);
            		
            		//Create window layout style
            		JPanel scorePanel = new JPanel();
            		scorePanel.setLayout (new BorderLayout());
            		getContentPane().add(scorePanel);
            		scorePanel.add(awayLabel);
                	scorePanel.revalidate();
    
    			}
    		});
    		buttonPanel.add(displayScore);
    So that I can add

    Java Code:
    scorePanel.add(homeLabel);
    and have it place the increasing score in the second JFrame.

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

    Default Re: Transfering JLabels Between actionPerformed Methods

    I think that you need to add a component to the newly created JFrame for it to show. Right now you add nothing to its contentPane.

    Also, you'll want to call setVisible(true) only after adding all components to a GUI, not before.

    Also, I'm not crazy with your design. Why not have the scores already displayed and then have the buttons on the bottom rather than popping JFrames up?

  5. #5
    RchrdJ28 is offline Member
    Join Date
    Nov 2011
    Posts
    3
    Rep Power
    0

    Default Re: Transfering JLabels Between actionPerformed Methods

    Not adding to the contentPane was my problem. By adding

    Java Code:
    Container content = scoreFrame.getContentPane();
    to the JFrame-creating actionPerformed method (last one below the score-manipulating buttons) I was able to add to the contentPane using

    Java Code:
    content.add(homeLabel, BorderLayout.CENTER);
    .

    Thank you for your help, Fubarable. As for the design, I want to have the scoreboard view seperate from the buttons, so this is how I am accomplishing this for now. I will take your advice and try to find a different approach as I go forward.

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

    Default Re: Transfering JLabels Between actionPerformed Methods

    You're quite welcome. Best of luck!

Similar Threads

  1. Check if file is done transfering
    By markthien in forum Advanced Java
    Replies: 1
    Last Post: 02-21-2011, 07:35 AM
  2. Transfering data between core Java and Jsp
    By imthiyazg@gmail.com in forum JavaServer Pages (JSP) and JSTL
    Replies: 3
    Last Post: 09-06-2010, 12:17 PM
  3. Transfering all items from one List to another
    By Bulelakes in forum New To Java
    Replies: 6
    Last Post: 08-23-2010, 08:48 AM
  4. Transfering Focus
    By kevh in forum AWT / Swing
    Replies: 0
    Last Post: 07-30-2010, 06:12 PM
  5. Transfering files over HTTP
    By DannyZB in forum Networking
    Replies: 16
    Last Post: 11-09-2008, 09:50 PM

Tags for this Thread

Posting Permissions

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