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 3 of 3

Thread: Buttons out of scope for setBounds()

  1. #1
    Junior Member
    Join Date
    Nov 2012
    Posts
    4
    Thanks
    1
    Thanked 1 Time in 1 Post

    Default Buttons out of scope for setBounds()

    I'm working on a project (which we have not learned to load other classes over our existing window) which on button press I want to remove all elements from the screen by just positioning them off screen by 1000 px. I have two different select buttons which have different variables attached to them but essentially I want them to do the same thing which is move everything by 1000 px in the x direction. One other buttons works, but the other button will work for everthing but the button below it because it is out of scope. Is there anyway I can grab the positioning from below in the code to change it higher up. the snipit of my code that im having issues with follows:
    		final JButton btnWarrior = new JButton("Warrior");
    		btnWarrior.addActionListener(new ActionListener() {
    			public void actionPerformed(ActionEvent arg0) {		
    				hp = 30;
    				att = 5;
    				System.out.println(hp+"    "+att);
    				btnWarrior.setBounds(-942, 97, 89, 23);
    				btnAssassin.setBounds(-710, 97,89,23);
    				lblTitle.setBounds(-990, 11, 414, 43);
    				lblHpW.setBounds(-915, 131, 46, 14);
    				lblAttW.setBounds(-915, 168, 46, 14);
    				lblHpA.setBounds(-687, 131, 46, 14);
    				lblAttA.setBounds(-700, 168, 79, 14);
    			}
    		});
    		btnWarrior.setBounds(58, 97, 89, 23);
    		frame.getContentPane().add(btnWarrior);
     
    		final JButton btnAssassin = new JButton("Assassin");
    		btnAssassin.addActionListener(new ActionListener() {
    			public void actionPerformed(ActionEvent e) {
    				hp = 25;
    				att = 4;
    				System.out.println(hp+"    "+att);
    				btnAssassin.setBounds(-710, 97,89,23);
    				btnWarrior.setBounds(-942, 97, 89, 23);
    				lblTitle.setBounds(-990, 11, 414, 43);
    				lblHpW.setBounds(-915, 131, 46, 14);
    				lblAttW.setBounds(-915, 168, 46, 14);
    				lblHpA.setBounds(-687, 131, 46, 14);
    				lblAttA.setBounds(-700, 168, 79, 14);
     
    			}
    		});
    btnAssassin is out of scope of the actions performed in btnWarrior. Is there anything I can do to change the scope

  2. The Following User Says Thank You to scrotty544 For This Useful Post:

    nrickinedi (November 23rd, 2012)


  3. #2
    Super Moderator curmudgeon's Avatar
    Join Date
    Aug 2012
    Posts
    1,130
    My Mood
    Cynical
    Thanks
    64
    Thanked 139 Times in 134 Posts

    Default Re: Buttons out of scope for setBounds()

    It looks like you're having your JButtons swap position and are running into a problem as your JButtons are not class fields. Suggestions:

    • Make your JButtons, or perhaps better, a JButton[] array or ArrayList, into class fields.
    • Myself, I avoid using absolute positioning if at all possible. Instead try to use a smart combination of nested layouts.
    • Rather than try to swap JButton positions, why not leave the JButtons where they are, and simply swap the Actions that they hold. If done right, the JButton's text, icons and behaviors will all swap with this, and you don't have to revalidate or repaint your GUI.

  4. #3
    Junior Member
    Join Date
    Nov 2012
    Posts
    4
    Thanks
    1
    Thanked 1 Time in 1 Post

    Default Re: Buttons out of scope for setBounds()

    Thanks, In the future I plan to be rid of the absolute layout but im using it now as requested in class. I ended up establishing the button at the top of the of the class with
    public class Index extends JFrame{
    	JButton btnAssassin;
    and at the bottom with
    		btnAssassin = new JButton("Assassin");
    to repair the scope. Thanks for the tips ill research the arrays and try to apply that next.

    --- Update ---

    I would like to add I am creating a game so I would need to repaint the GUI for each segment of the game, this one would be the character select screen but I have a movement and fight screen as well.

Similar Threads

  1. Problem with scope, please help!
    By austin.rose94 in forum What's Wrong With My Code?
    Replies: 5
    Last Post: December 14th, 2011, 11:11 AM
  2. Scope problem
    By TP-Oreilly in forum What's Wrong With My Code?
    Replies: 5
    Last Post: December 9th, 2011, 07:41 AM
  3. Variable Scope
    By TP-Oreilly in forum Java Theory & Questions
    Replies: 7
    Last Post: October 5th, 2011, 04:08 PM
  4. Scope of Variables
    By PineAppleKing in forum Java Theory & Questions
    Replies: 5
    Last Post: June 11th, 2011, 10:22 AM
  5. variabe not within scope
    By brainwave in forum Java Servlet
    Replies: 0
    Last Post: April 17th, 2010, 05:51 AM