Results 1 to 9 of 9

Thread: JButton help

  1. #1
    mbschutlz97 is offline Member
    Join Date
    Jan 2014
    Posts
    30
    Rep Power
    0

    Default JButton help

    I'm trying to make a button that will make a panel not visible and then will make another panel visible but i keep getting this error...

    test.java:19: error: local variable panel is accessed from within inner class; needs to be declared final
    panel.setVisible(false);
    ^
    1 error


    Java Code:
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    public class test{
    	public static void main(String[] args){
    		JFrame test = new JFrame("Test");
    		test.setSize(600,400);
    		test.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    		test.setVisible(true);
    		JPanel panel = new JPanel();
    		JLabel label = new JLabel("Random",SwingConstants.LEFT);
    		test.getContentPane().add(label, BorderLayout.NORTH);
    		JButton button = new JButton("Test");
    		test.getContentPane().add(panel);
    		panel.add(button);
          button.addActionListener(new ActionListener(){
             public void actionPerformed(ActionEvent e){
               panel.setVisible(false);
             }
          });
    	}
    }
    Last edited by mbschutlz97; 01-28-2014 at 02:53 AM.

  2. #2
    mbschutlz97 is offline Member
    Join Date
    Jan 2014
    Posts
    30
    Rep Power
    0

    Default Re: JButton help

    still nothing?

  3. #3
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,601
    Rep Power
    5

    Default Re: JButton help

    So after 43 minutes you expect to have an answer. Sometimes you will never get answers. In the future, if you haven't received one in a week then reply to your own thread to re-prompt (with hopefully some modified code).

    First, place your code between tags to properly format your code. To answer your question, if have a local variable (like panel which is inside of main) and want to reference it inside of an inner class (anonymous or otherwise) which is also in main, you need to declare it final. If you don't want to declare it final, make it an instance field of the containing class. In your case, the containing class is test. Also, by convention, class names begin with an uppercase letter.

    Regards,
    Jim
    Last edited by jim829; 01-28-2014 at 02:33 AM. Reason: clarification
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,397
    Rep Power
    25

    Default Re: JButton help

    needs to be declared final
    Did you try what the compiler suggested? Local variables need to be treated specially if used in an inner class so their values are around when the inner class's methods execute.

    Please edit your post and wrap your code with code tags:
    [code]
    YOUR CODE HERE
    [/code]
    to get highlighting and preserve formatting.
    If you don't understand my response, don't ignore it, ask a question.

  5. #5
    mbschutlz97 is offline Member
    Join Date
    Jan 2014
    Posts
    30
    Rep Power
    0

    Default Re: JButton help

    Quote Originally Posted by jim829 View Post
    So after 43 minutes you expect to have an answer. Sometimes you will never get answers. In the future, if you haven't received one in a week then reply to your own thread to re-prompt (with hopefully some modified code).

    First, place your code between tags to properly format your code. To answer your question, if have a local variable (like panel which is inside of main) and want to reference it inside of an inner class (anonymous or otherwise) which is also in main, you need to declare it final. If you don't want to declare it final, make it an instance field of the containing class. In your case, the containing class is test. Also, by convention, class names begin with an uppercase letter.

    Regards,
    Jim
    sorry for being impatient :P... how do i make it an instance field? could you show me some example code? (just started learning JFrame today)

  6. #6
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,601
    Rep Power
    5

    Default Re: JButton help

    Just move the JPanel panel declaration up above main. But since you will be referencing it in a static context (main is a static method) you will need to qualify it with a an instance of your test class. So in your case, since you will probably not be changing the value of panel, just make it final and leave it where it is.

    Java Code:
    final JPanel panel;
    panel = new JPanel();
    //or
    final JPanel panel = new JPanel();
    Regard,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  7. #7
    mbschutlz97 is offline Member
    Join Date
    Jan 2014
    Posts
    30
    Rep Power
    0

    Default Re: JButton help

    Quote Originally Posted by jim829 View Post
    Just move the JPanel panel declaration up above main. But since you will be referencing it in a static context (main is a static method) you will need to qualify it with a an instance of your test class. So in your case, since you will probably not be changing the value of panel, just make it final and leave it where it is.

    Java Code:
    final JPanel panel;
    panel = new JPanel();
    //or
    final JPanel panel = new JPanel();
    Regard,
    Jim
    hey... I can't make it final because it's like the main menu so it will have to be visible again whenever anyone wants to go back to it... I've been trying out some other stuff too and I've found some more problems (ofc)

    Java Code:
    //Matthew
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    public class Test{
    
    	public static void main(String[] args){
       
    		JFrame frame = new JFrame("Test");
    		frame.setSize(600,400);
    		frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    		frame.setVisible(true);
          
          JPanel panel = new JPanel();
          JPanel panel2 = new JPanel();
          panel.setVisible(true);
          panel.setVisible(false);
    
    		JLabel label = new JLabel("Chemistry Mole Converter",SwingConstants.LEFT);
          JLabel label2 = new JLabel("Made by: Matthew",SwingConstants.RIGHT);
          
    		frame.add(label, BorderLayout.NORTH);
          frame.add(label2, BorderLayout.NORTH);
          
    		JButton button = new JButton("Grams to Moles");
          JButton button2 = new JButton("Liters to Moles");
          
    		frame.add(panel);
          frame.add(panel2);
          
    		panel.add(button);
          panel.add(button2);
          
          button.addActionListener(new ActionListener(){
             public void actionPerformed(ActionEvent e){
               //panel.setVisible(false);
               //panel2.setVisible(true);
             }
          });
    	}
    }
    first... when i have two panels added to the frame neither of them show up... also how can i have two labels NORTH?... when i try to run it only label2 shows i would like to have one LEFT and one RIGHT. For now I'm just gonna forget about the actionlistener

  8. #8
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,601
    Rep Power
    5

    Default Re: JButton help

    final just means you can't reassign it another value. If you need to reassign it later, then you must do my first suggestion.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  9. #9
    mbschutlz97 is offline Member
    Join Date
    Jan 2014
    Posts
    30
    Rep Power
    0

    Default Re: JButton help

    Quote Originally Posted by jim829 View Post
    final just means you can't reassign it another value. If you need to reassign it later, then you must do my first suggestion.

    Regards,
    Jim
    how do i fix the problem with my panels not showing up and only one label being able to be NORTH? also can you give me an example of making it an instance of?

Similar Threads

  1. Replies: 1
    Last Post: 01-15-2013, 06:44 PM
  2. JButton
    By Proshitness in forum AWT / Swing
    Replies: 11
    Last Post: 12-07-2011, 04:25 PM
  3. ActionListener for JButton after changing Button to JButton
    By ravi.joshi53 in forum Java Applets
    Replies: 2
    Last Post: 10-07-2011, 07:35 AM
  4. JButton help :)
    By yasmin k in forum AWT / Swing
    Replies: 7
    Last Post: 11-12-2009, 09:53 PM
  5. Help with JButton
    By geoffreybarwise in forum New To Java
    Replies: 4
    Last Post: 05-21-2008, 10:48 AM

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
  •