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

Thread: JButton

  1. #1
    Member
    Join Date
    Jan 2012
    Location
    Hyderabad, Andhra Pradesh, India
    Posts
    32
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default [SOLVED] JButton

    When the following program is compiled:
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    public class del1 extends JFrame{
    	public del1()
    	{
    		JPanel panel1=new JPanel();
    		JButton button=new JButton("HI");
    		button.addActionListener(new ButtonHandler());
    		panel1.add(button);
    	}
    	private class ButtonHandler implements ActionListener
    	{
    		public void actionPerformed(ActionEvent event)
    		{
    		panel1.remove(0); // line24
    		panel1.add(new JButton("BYE")); // line 25
     
    	}
    }
    the following errors are being shown: cannot find symbol variable panel 1 line 24 and 25
    Can't the variables of the main class be accessed from the inner class?
    And when I change the inner class into an anonymous class, the error message displayed during compilation is: declare panel1 as final
    When I do it and run the program ( by creating its object ) nothing is being displayed
    Last edited by ranjithfs1; March 17th, 2012 at 03:14 AM.


  2. #2
    Forum VIP
    Join Date
    Oct 2010
    Posts
    275
    My Mood
    Cool
    Thanks
    32
    Thanked 54 Times in 47 Posts
    Blog Entries
    2

    Default Re: JButton

    The variable panel1 is only in the scope of your constructor, in order for it to be in both your button handler's and your constructors scope, you will have to make it a global* variable, such as

    public class Foo
    {
      private Object someVariable;
      public Foo()
      {
         //Something with someVariable...
      }
      public void Bar()
      {
         //Something else with someVariable
      }
    }

    Also, with a glance-inspection of your code, after your button click you might want to remove the button
    and add a JLabel instead.
    Edit:
    -> It would be better not to do panel1.remove(0) [Although I've never tried to do that before], but rather keep button as a global variable as well and use panel1.remove(button) OR use panel1.removeAll(), however the more probable reason it did not work before when you used an anonymous class is you did not call
    panel1.revalidate();
    panel1.repaint();

    *I'm not sure if global is the right terminology
    Last edited by Tjstretch; March 16th, 2012 at 12:34 PM.

  3. #3
    Member
    Join Date
    Jan 2012
    Location
    Hyderabad, Andhra Pradesh, India
    Posts
    32
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Unhappy Re: JButton

    Here is my modified code:

    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    public class del1 extends JFrame{
    	JPanel panel1;
    	JButton button,button2;
    	public del1()
    	{
    		panel1=new JPanel();
    		button=new JButton("HI");
    		button.addActionListener(new ButtonHandler());
    		panel1.add(button);
    		button2= new JButton("BYE");
    	}
    	private class ButtonHandler implements ActionListener
    	{
    		public void actionPerformed(ActionEvent event)
    		{
    		panel1.remove(button);
    		panel1.add(button2);
    panel1.revalidate();
    panel1.repaint();
     
    	}
    }
    }

    import javax.swing.JFrame;
     
    public class delTest25 {
    	public static void main(String[] args)
    	{
    		del1 a=new del1();
    		a.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
          a.setSize( 300, 75 ); // set frame size
          a.setVisible( true ); // display frame
    	}
    }

    I still receive an empty window as my output.

  4. #4
    Forum VIP
    Join Date
    Jun 2011
    Posts
    273
    My Mood
    Bored
    Thanks
    39
    Thanked 76 Times in 64 Posts
    Blog Entries
    4

    Default Re: JButton

    You still need to add the panel to the JFrame. You can do it at the bottom of the constructor.

  5. #5
    Member
    Join Date
    Jan 2012
    Location
    Hyderabad, Andhra Pradesh, India
    Posts
    32
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: JButton

    Thanks. It's done now!

Similar Threads

  1. jbutton on applet
    By coder.freak in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 6th, 2011, 01:08 PM
  2. Help with jButton
    By ls7897 in forum AWT / Java Swing
    Replies: 6
    Last Post: March 13th, 2011, 03:22 PM
  3. JButton help
    By tabutcher in forum Java Applets
    Replies: 4
    Last Post: March 9th, 2010, 06:04 PM
  4. JButton Help
    By ravjot28 in forum AWT / Java Swing
    Replies: 3
    Last Post: January 17th, 2010, 10:38 AM
  5. JButton...
    By chronoz13 in forum AWT / Java Swing
    Replies: 1
    Last Post: November 27th, 2009, 10:39 AM