Results 1 to 6 of 6
- 11-20-2011, 10:25 PM #1
Member
- Join Date
- Nov 2011
- Posts
- 3
- Rep Power
- 0
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
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.Java Code:examplePanel.add(exampleLabel)
So my question is: how do I transfer a JLabel from one actionPerformed method to another?
-
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.
- 11-21-2011, 12:10 AM #3
Member
- Join Date
- Nov 2011
- Posts
- 3
- Rep Power
- 0
Re: Transfering JLabels Between actionPerformed Methods
Sorry, I'm trying to make a scoreboard program. Here is the code I have so far:
Specifically, I am trying to take the homeLabel JLabel from this method: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); } }
and place it in 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);
So that I can addJava 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);
and have it place the increasing score in the second JFrame.Java Code:scorePanel.add(homeLabel);
-
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?
- 11-21-2011, 01:40 AM #5
Member
- Join Date
- Nov 2011
- Posts
- 3
- Rep Power
- 0
Re: Transfering JLabels Between actionPerformed Methods
Not adding to the contentPane was my problem. By adding
to the JFrame-creating actionPerformed method (last one below the score-manipulating buttons) I was able to add to the contentPane usingJava Code:Container content = scoreFrame.getContentPane();
.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.
-
Similar Threads
-
Check if file is done transfering
By markthien in forum Advanced JavaReplies: 1Last Post: 02-21-2011, 07:35 AM -
Transfering data between core Java and Jsp
By imthiyazg@gmail.com in forum JavaServer Pages (JSP) and JSTLReplies: 3Last Post: 09-06-2010, 12:17 PM -
Transfering all items from one List to another
By Bulelakes in forum New To JavaReplies: 6Last Post: 08-23-2010, 08:48 AM -
Transfering Focus
By kevh in forum AWT / SwingReplies: 0Last Post: 07-30-2010, 06:12 PM -
Transfering files over HTTP
By DannyZB in forum NetworkingReplies: 16Last Post: 11-09-2008, 09:50 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks