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: Action Listener

  1. #1
    Junior Member
    Join Date
    Apr 2010
    Location
    SUTTON,SURREY
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Action Listener

    Hi Everyone,
    I am writing this program to display this GUI with buttons ,I am trying to add an action listener to add functionality to the buttons.

    Problem:I am trying to display the colour of the buttons as in (i.e. When a blue button is clicked a blue rectangle is displayed in the center pane)

    Now I have managed to draw the rectangle on the center pane but when I add the action Listener to the button it does not compile it generates two errors.

    Can someone have a look at my code please , i dont know how to call the paint function into the Listener

    errors:

    shape.java:43:cannot find symbol
    symbol: method setColor(java.awt.Color)
    location: class MyPanel
    disp.setColor(Color.red)

    shape.java:43:cannot find symbol
    symbol: method fillRectr(int,int,int,int)
    location: class MyPanel
    disp.fillRectr(100,50,100,50)

    code:

    shape.java
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
     
     
     
    class shape extends JFrame{
     
          JButton b1 = new JButton("Red");
          JButton b2 = new JButton("Green");
          JButton b3 = new JButton("Blue");
          JButton b4 = new JButton("Black");
          JButton b5 = new JButton("Erase"); 
     
      JPanel east = new JPanel();
      MyPanel disp = new MyPanel();
      JPanel center = new JPanel();
     
     
      shape(){
     
         setupGUI();
         setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
       }
     
       private void setupGUI(){
     
       Container cp = getContentPane();
       cp.setLayout(new BorderLayout());
       cp.add(BorderLayout.CENTER,disp);
       cp.add(disp);
       cp.add(BorderLayout.EAST,east);
       east.setLayout(new GridLayout(5,1));
       east.add(b1);
       east.add(b2);
       east.add(b3);
       east.add(b4);
       east.add(b5);
       b1.addActionListener(new ActionListener(){
           public void actionPerformed(ActionEvent e){
             if(e.getActionCommand().equals("RED")){
     
                 disp.setColor(Color.red);
                 disp.fillRect(100,50,100,50);
     
     
              //setColor(Color.red);
              //fillRect(100,50,100,50);
     
     
             }
           }
     
        });
       setSize(350,450);
       setVisible(true);
     
      }
      public static void main(String args[]){
     
         shape Pane = new shape();
     
     
     
       }
    }

    MyPanel.java

    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
     
     
    class MyPanel extends JPanel{
     
       public void paintComponent(Graphics g){
     
         super.paintComponent(g);
         g.setColor(Color.white);
         g.drawRect(100,50,100,50);
         g.fillRect(100,50,100,50);
     
       }
     
     
    }


  2. #2
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 294 Times in 227 Posts
    Blog Entries
    4

    Default Re: Action Listener

    Hello kray66,

    Welcome to the Java Programming Forums.

    Hows sunny Surrey? I'm in Essex myself..

    I think this link may help solve your problem:

    http://www.javaprogrammingforums.com...ava-swing.html
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

  3. #3
    Junior Member
    Join Date
    Dec 2008
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Action Listener

    HI kray66 i just take a look u'r code and how its means that ju called directly the method setColor or setSize from the object MyPanel ,u must inicialize a graphics pen graphics2d a=..... a.setColor...I'would look and one more time to unsure that that was a problem.All wishes

Similar Threads

  1. jbutton and action event
    By mt888 in forum AWT / Java Swing
    Replies: 3
    Last Post: March 26th, 2010, 06:24 AM
  2. Problem with Action Listener
    By JonoScho in forum AWT / Java Swing
    Replies: 4
    Last Post: March 19th, 2010, 01:03 AM
  3. Key Listener not working in jinternalframe
    By furqankhan in forum What's Wrong With My Code?
    Replies: 1
    Last Post: February 12th, 2010, 09:48 AM
  4. Key listener help
    By airsim15 in forum AWT / Java Swing
    Replies: 2
    Last Post: December 13th, 2009, 03:15 PM
  5. Need Help Action Listener....
    By vhizent23 in forum AWT / Java Swing
    Replies: 2
    Last Post: October 9th, 2009, 01:46 PM