9 Replies - 9389 Views - Last Post: 09 February 2011 - 07:39 PM Rate Topic: -----

#1 Dev1462  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 145
  • Joined: 20-May 10

Calling the paint method via button click?

Posted 09 February 2011 - 06:45 PM

So i wrote an inner class that implements the ActionListener interface...and now im curious....How do you call the paint method via a click from a button...I want to change an image in a Jpanel when i click a certain button...hopfully you guys know of a good tutorial
Is This A Good Question/Topic? 0
  • +

Replies To: Calling the paint method via button click?

#2 japanir  Icon User is offline

  • jaVanir
  • member icon

Reputation: 1010
  • View blog
  • Posts: 3,025
  • Joined: 20-August 09

Re: Calling the paint method via button click?

Posted 09 February 2011 - 06:47 PM

Since you are inside the JPanel class scope, call the repaint() method.
Was This Post Helpful? 3
  • +
  • -

#3 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8324
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Calling the paint method via button click?

Posted 09 February 2011 - 06:54 PM

Would be alot easier if you implement the ActionListener in your class
If you use another class (even inner) you will have to pass your JPanel as parameter to your Listener constructor

class MyClass implements ActionListener {
     JPanel panel;

     MyClass() {
       panel = new JPanel();
       ....
       button.addActionListener(this);
     }

     public void actionPerformed(ActionEvent e) {
       // easy I can see panel
       panel.repaint();
     }
}


with inner class
class MyClass {

     MyClass() {
       JPanel panel = new JPanel();
       ....
       MyListener ml = new Listener(panel);
       button.addActionListener(ml);
     }

     class MyListener implements ActionListener {
        JPanel panel;

        // constructor
        MyListener(JPanel panel) {
          this.panel = panel;       // save panel to repaint
        }

        public void actionPerformed(ActionEvent e) {
          // use JPanel saved when instance was built
          panel.repaint();
        }
     }
}


This post has been edited by pbl: 09 February 2011 - 06:56 PM

Was This Post Helpful? 1
  • +
  • -

#4 Dev1462  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 145
  • Joined: 20-May 10

Re: Calling the paint method via button click?

Posted 09 February 2011 - 06:56 PM

I dont think you read my description. I want to change an draw a new image when the actoinPerformed(ActionEvent [var]) gets invoked via a button click...I just have no idea how to even start such a thing because my current OO logic tells me its impossible from my standpoint

Edit:
pbl, if i call the repaint all it will do is call the current paint method and not change which image that is drawn

This post has been edited by Dev1462: 09 February 2011 - 06:58 PM

Was This Post Helpful? 0
  • +
  • -

#5 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8324
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Calling the paint method via button click?

Posted 09 February 2011 - 06:59 PM

View Postjapanir, on 09 February 2011 - 08:47 PM, said:

Since you are inside the JPanel class scope, call the repaint() method.

Won't work
If you are in an inner class you need more than just repaint() you need access to the instance object of the class you ar in as I have showed with my code

This post has been edited by pbl: 09 February 2011 - 07:43 PM
Reason for edit:: My bad

Was This Post Helpful? 2
  • +
  • -

#6 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8324
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Calling the paint method via button click?

Posted 09 February 2011 - 07:05 PM

View PostDev1462, on 09 February 2011 - 08:56 PM, said:

pbl, if i call the repaint all it will do is call the current paint method and not change which image that is drawn

So you will have to overload paint() or paintComponent() and react based to a variable set in your actionPerformed
class MyPanel extends JPanel implements ActionListener {
    boolean drawOtherImage = false;

    MyPanel() {
      button.addActionListener(this);
    }

    public void paintComponent(Graphics g) {
      super.paintComponent(g);
      if(drawOtherImage) {
        // actionPerformed has been called
        g.drawOtherImage(....
      }
    }

    public void actionPerformed(ActionEvent e) {
      drawOtherImage = true;
      repaint();
    }
}


Was This Post Helpful? 1
  • +
  • -

#7 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10364
  • View blog
  • Posts: 38,378
  • Joined: 27-December 08

Re: Calling the paint method via button click?

Posted 09 February 2011 - 07:19 PM

View Postpbl, on 09 February 2011 - 10:54 PM, said:

Would be alot easier if you implement the ActionListener in your class
If you use another class (even inner) you will have to pass your JPanel as parameter to your Listener constructor

Sorry pbl, but not true. Non-static Inner classes can still access outer class fields and methods. There are just some nuances with anonymous inner classes and local variables.

As proof of concept, note how the inner class can still access the outer instance variable.
class Outer{
	int x;

	Outer(){
		x = 3;
		new Inner();
		new Inner();
	}

	class Inner{
		Inner(){
			x++;
			System.out.println(x);
		}
	}

	public static void main(String[] args){new Outer();}

}


Was This Post Helpful? 1
  • +
  • -

#8 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8324
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Calling the paint method via button click?

Posted 09 February 2011 - 07:30 PM

View Postmacosxnerd101, on 09 February 2011 - 09:19 PM, said:

Sorry pbl, but not true. Non-static Inner classes can still access outer class fields and methods. There are just some nuances with anonymous inner classes and local variables.

Yes, and use it many times but in that case you will need to save the instance in a variable
May be I was not clear enough. In that case you need a reference to your instance in the instance variables
class MyPanel extends JPanel {

     MyPanel() {
       ...
     }

     class MyListener extends ActionListener {
        public void ActionPerformed(ActionEvent e) {
          ... cannot access MyPanel from here unless
        }
     }


class MyPanel extends JPanel {

     MyPanel instance;
     MyPanel() {
       instance = this;
     }

     class MyListener extends ActionListener {
        public void ActionPerformed(ActionEvent e) {
          ... OK can access instance here
        }
     }



Thing for sure you cannot just do repaint() in the inner class as japanir suggested
You will have to save instance and instnce.repaint()

This post has been edited by pbl: 09 February 2011 - 07:40 PM
Reason for edit:: My bad

Was This Post Helpful? 0
  • +
  • -

#9 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10364
  • View blog
  • Posts: 38,378
  • Joined: 27-December 08

Re: Calling the paint method via button click?

Posted 09 February 2011 - 07:31 PM

Still not exactly true. I modified my example to include a direct call to Outer.test() (an instance method) from the inner class. It works b/c in order for the inner class to access the outer class method, there has to be an instance of the outer class in existance.
class Outer{
	int x;

	Outer(){
		x = 3;
		new Inner();
		new Inner();
	}

	class Inner{
		Inner(){
			x++;
			System.out.println(x);
			print();
		}
	}

	void print(){System.out.println("Testing");}

	public static void main(String[] args){new Outer();}

}

Was This Post Helpful? 2
  • +
  • -

#10 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8324
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Calling the paint method via button click?

Posted 09 February 2011 - 07:39 PM

You are right
This compiles

public class Radio extends JPanel {
	
	Radio() {
	}

	class MyListener implements ActionListener {

		@Override
		public void actionPerformed(ActionEvent arg0) {
			repaint();		
		}
		
	}
}


My apologies... and a few +1 for you and japanir I guess :)
Was This Post Helpful? 2
  • +
  • -

Page 1 of 1