Action listener

Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Nov 2006
Posts: 27
Reputation: anti_genius is an unknown quantity at this point 
Solved Threads: 0
anti_genius anti_genius is offline Offline
Light Poster

Action listener

 
0
  #1
Feb 15th, 2007
Hi,

Can anybody tell me how can i set attributes of frame or components in a frame in ActionListener of a button.

Regards.
Reply With Quote Quick reply to this message  
Join Date: Dec 2004
Posts: 4,267
Reputation: peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of 
Solved Threads: 493
Moderator
Featured Poster
peter_budo's Avatar
peter_budo peter_budo is offline Offline
Code tags enforcer

Re: Action listener

 
0
  #2
Feb 15th, 2007
Just quick pseudo-code

  1. public void actionPerfomed
  2. if resize
  3. myFrame.setSize(new Dimension(300, 300))
  4. else if changeFont
  5. myFrame.setFont(new Font( "Serif", Font.BOLD, 20 ));

you should get idea of it
Learn to see in another's calamity the ills which you should avoid.
Publilius Syrus
(~100 BC)

LJC - London Java Community, Graduate & Undergraduate Software Development Community, JAVAWUG (Java Web User Group), The London Android Group
Reply With Quote Quick reply to this message  
Join Date: Nov 2006
Posts: 27
Reputation: anti_genius is an unknown quantity at this point 
Solved Threads: 0
anti_genius anti_genius is offline Offline
Light Poster

Re: Action listener

 
0
  #3
Feb 15th, 2007
Originally Posted by peter_budo View Post
Just quick pseudo-code

  1. public void actionPerfomed
  2. if resize
  3. myFrame.setSize(new Dimension(300, 300))
  4. else if changeFont
  5. myFrame.setFont(new Font( "Serif", Font.BOLD, 20 ));

you should get idea of it
i have tried what has been told above as

this.setSize();

but "this" is not recognized within ActionListener.
And i have extended frame class so there is no name for the frame as well.

Regards.
Reply With Quote Quick reply to this message  
Join Date: Nov 2006
Posts: 27
Reputation: anti_genius is an unknown quantity at this point 
Solved Threads: 0
anti_genius anti_genius is offline Offline
Light Poster

Re: Action listener

 
0
  #4
Feb 19th, 2007
Nobody knows any solution of the problem???
Reply With Quote Quick reply to this message  
Join Date: May 2006
Posts: 1,580
Reputation: Infarction has a spectacular aura about Infarction has a spectacular aura about Infarction has a spectacular aura about 
Solved Threads: 52
Infarction's Avatar
Infarction Infarction is offline Offline
Battle Programmer

Re: Action listener

 
0
  #5
Feb 20th, 2007
post some relevant code please
Reply With Quote Quick reply to this message  
Join Date: Nov 2004
Posts: 6,143
Reputation: jwenting is just really nice jwenting is just really nice jwenting is just really nice jwenting is just really nice 
Solved Threads: 212
Team Colleague
jwenting's Avatar
jwenting jwenting is offline Offline
duckman

Re: Action listener

 
0
  #6
Feb 20th, 2007
"this" IS recognised inside the actionlistener, it's just not what you think it is.

Learn a bit more about Java and you will see why, and what "this" is out there.

At the moment you're expecting quick hacks for everything without really understanding what you're doing.
That's the wrong way to go about programming.

Do simple things first until you thoroughly understand those, and only then try something more complicated (and adding an actionListener is NOT complicated, so if you have problems with that you need to really step back a lot).
As people are clearly allowed to attack me but I'm not allowed to defend myself, I no longer post to this site.
Reply With Quote Quick reply to this message  
Join Date: Nov 2006
Posts: 27
Reputation: anti_genius is an unknown quantity at this point 
Solved Threads: 0
anti_genius anti_genius is offline Offline
Light Poster

Re: Action listener

 
0
  #7
Feb 21st, 2007
Originally Posted by Infarction View Post
post some relevant code please
class ImgMod8 extends Frame  {  String msg = "";  int mX1 = 150;  int mY1 = 150;  int mX2 = 0;  int mY2 = 0;   int turn = 1;  static int dst = 0;  double sum = 0.0;  int count;  Image rawImg;  int imgCols;//Number of horizontal pixels  int imgRows;//Number of rows of pixels  Image modImg;//Reference to modified image  //Inset values for the Frame  int inTop;  int inLeft; //To add only one picture at a time int ig = 1; static String theProcessingClass = "PrintMouse5";  // This image file will be processed if another file name is  // not entered on the command line.    static String theImgFile = "white.gif";  MediaTracker tracker;  Display display = new Display();//A Canvas  Button replotButton = new Button("Replot");   //References to arrays that store pixel data.  int[][][] threeDPix;  int[][][] threeDPixMod;  int[] oneDPix;  //Reference to the image-processing object.  ImgIntfc04 imageProcessingObject;  //-------------------------------------------//  public static void main(String[] args){    // Program supports gif files and jpg files    // and possibly some other file types as well.    //Display name of processing program and    // image file.    System.out.println("Processing program: " + theProcessingClass);    System.out.println("Image file: " + theImgFile);    //Instantiate an object of this class    ImgMod8 obj = new ImgMod8();  }//end main  //-------------------------------------------//  public ImgMod8(){//constructor    //Get an image from the specified file.  Can    // be in a different directory if the path    // was entered with the file name on the command line.    rawImg = Toolkit.getDefaultToolkit().getImage(theImgFile);    //Use a MediaTracker object to block until    // the image is loaded or ten seconds has elapsed.    tracker = new MediaTracker(this);    tracker.addImage(rawImg,1);    try{      if(!tracker.waitForID(1,10000)){        System.out.println("Load error.");        System.exit(1);      }//end if    }catch(InterruptedException e){      e.printStackTrace();      System.exit(1);    }//end catch    //Make certain that the file was successfully    // loaded.    if((tracker.statusAll(false) & MediaTracker.ERRORED & MediaTracker.ABORTED) != 0){      System.out.println("Load errored or aborted");      System.exit(1);    }//end if    //Raw image has been loaded.  Get width and    // height of the raw image.    imgCols = rawImg.getWidth(this);    imgRows = rawImg.getHeight(this);    this.setTitle("Get Pixel Distance");    this.setBackground(Color.YELLOW);    this.add(display);    this.add(replotButton,BorderLayout.SOUTH);    //Make it possible to get insets and the    // height of the button.    setVisible(true);    //Get and store inset data for the Frame and    // the height of the button.    inTop = this.getInsets().top;    inLeft = this.getInsets().left;    int buttonHeight = replotButton.getSize().height;    //Size the frame so that a small amount of    // yellow background will show on the right    // and on the bottom when both images are    // displayed, one above the other.  Also, the    // placement of the images on the Canvas    // causes a small amount of background to    // show between the images.    this.setSize(inLeft+imgCols + 1,inTop + buttonHeight + imgRows + 7);     //=========================================//    //Anonymous inner class listener for replot    // button.  This actionPerformed method is    // invoked when the user clicks the Replot    // button.  It is also invoked at startup    // when this program posts an ActionEvent to    // the system event queue attributing the    // event to the Replot button.    replotButton.addActionListener(      new ActionListener(){        public void actionPerformed(ActionEvent e){   //Calculate distance   double a = mX1-mX2;   double b = mY1-mY2;   sum=(Math.sqrt(a*a+b*b)) ;   System.out.println("This Distance is: " + sum);          //Pass a 3D array of pixel data to the          // processing object and get a modified 3D array of pixel data back.    threeDPixMod = imageProcessingObject.processImg(threeDPix, imgRows, imgCols, mX2, mY2);          //Convert the modified pixel data to a          // 1D array of pixel data.          oneDPix = convertToOneDim(threeDPixMod,imgCols,imgRows);          //Use the createImage() method to          // create a new image from the 1D array of pixel data.          modImg = createImage(new MemoryImageSource(imgCols,imgRows,oneDPix,0,imgCols));          //Repaint the image display frame with          // the original image at the top and          // the modified pixel data at the bottom.          display.repaint();           }//end actionPerformed      }//end ActionListener    );//end addActionListener    //End anonymous inner class.    //=========================================//

Here is the code i am using.

As i have extended the frame it has no name. And "this" in actionListener points to the button. Then how can i use frame.setSize()
Reply With Quote Quick reply to this message  
Join Date: Nov 2006
Posts: 27
Reputation: anti_genius is an unknown quantity at this point 
Solved Threads: 0
anti_genius anti_genius is offline Offline
Light Poster

Re: Action listener

 
0
  #8
Feb 21st, 2007
Originally Posted by Infarction View Post
post some relevant code please
Below is the code i am using.

As i have extended the frame it has no name. And "this" in actionListener points to the button. Then how can i use frame.setSize()


  1. import java.awt.*;
  2. import java.awt.event.*;
  3. import java.awt.image.*;
  4. class ImgMod8 extends Frame {
  5. String msg = "";
  6. int mX1 = 150;
  7. int mY1 = 150;
  8. int mX2 = 0;
  9. int mY2 = 0;
  10. int turn = 1;
  11. static int dst = 0;
  12. double sum = 0.0;
  13. int count;
  14. Image rawImg;
  15. int imgCols;//Number of horizontal pixels
  16. int imgRows;//Number of rows of pixels
  17. Image modImg;//Reference to modified image
  18. //Inset values for the Frame
  19. int inTop;
  20. int inLeft;
  21. //To add only one picture at a time
  22. int ig = 1;
  23. //This program will be executed to process the
  24. // if the name of another program is not
  25. // entered on the command line.
  26. static String theProcessingClass = "PrintMouse5";
  27. // This image file will be processed if another file name is
  28. // not entered on the command line.
  29. static String theImgFile = "white.gif";
  30. MediaTracker tracker;
  31. Display display = new Display();//A Canvas
  32. Button replotButton = new Button("Replot");
  33. //References to arrays that store pixel data.
  34. int[][][] threeDPix;
  35. int[][][] threeDPixMod;
  36. int[] oneDPix;
  37. //Reference to the image-processing object.
  38. ImgIntfc04 imageProcessingObject;
  39. //-------------------------------------------//
  40. public static void main(String[] args){
  41. // Program supports gif files and jpg files
  42. // and possibly some other file types as well.
  43. if(args.length == 0){
  44. //Use default processing class and default image file. No code required here.
  45. // Class and file names were specified above.
  46. }else if(args.length == 1){
  47. theProcessingClass = args[0];
  48. //Use default image file
  49. }else if(args.length == 2){
  50. theProcessingClass = args[0];
  51. theImgFile = args[1];
  52. }else if(args.length == 3){
  53. theProcessingClass = args[0];
  54. theImgFile = args[1];
  55. dst = Integer.parseInt(args[2]);
  56. }else{
  57. System.out.println("Invalid args");
  58. System.exit(1);
  59. }//end else
  60. //Display name of processing program and
  61. // image file.
  62. System.out.println("Processing program: " + theProcessingClass);
  63. System.out.println("Image file: " + theImgFile);
  64. //Instantiate an object of this class
  65. ImgMod8 obj = new ImgMod8();
  66. }//end main
  67. //-------------------------------------------//
  68. public ImgMod8(){//constructor
  69. //Get an image from the specified file. Can
  70. // be in a different directory if the path
  71. // was entered with the file name on the command line.
  72. rawImg = Toolkit.getDefaultToolkit().getImage(theImgFile);
  73. //Use a MediaTracker object to block until
  74. // the image is loaded or ten seconds has elapsed.
  75. tracker = new MediaTracker(this);
  76. tracker.addImage(rawImg,1);
  77. try{
  78. if(!tracker.waitForID(1,10000)){
  79. System.out.println("Load error.");
  80. System.exit(1);
  81. }//end if
  82. }catch(InterruptedException e){
  83. e.printStackTrace();
  84. System.exit(1);
  85. }//end catch
  86. //Make certain that the file was successfully
  87. // loaded.
  88. if((tracker.statusAll(false) & MediaTracker.ERRORED & MediaTracker.ABORTED) != 0){
  89. System.out.println("Load errored or aborted");
  90. System.exit(1);
  91. }//end if
  92. //Raw image has been loaded. Get width and
  93. // height of the raw image.
  94. imgCols = rawImg.getWidth(this);
  95. imgRows = rawImg.getHeight(this);
  96. this.setTitle("Get Pixel Distance");
  97. this.setBackground(Color.YELLOW);
  98. this.add(display);
  99. this.add(replotButton,BorderLayout.SOUTH);
  100. //Make it possible to get insets and the
  101. // height of the button.
  102. setVisible(true);
  103. //Get and store inset data for the Frame and
  104. // the height of the button.
  105. inTop = this.getInsets().top;
  106. inLeft = this.getInsets().left;
  107. int buttonHeight = replotButton.getSize().height;
  108. //Size the frame so that a small amount of
  109. // yellow background will show on the right
  110. // and on the bottom when both images are
  111. // displayed, one above the other. Also, the
  112. // placement of the images on the Canvas
  113. // causes a small amount of background to
  114. // show between the images.
  115. this.setSize(inLeft+imgCols + 1,inTop + buttonHeight + imgRows + 7);
  116.  
  117. //=========================================//
  118. //Anonymous inner class listener for replot
  119. // button. This actionPerformed method is
  120. // invoked when the user clicks the Replot
  121. // button. It is also invoked at startup
  122. // when this program posts an ActionEvent to
  123. // the system event queue attributing the
  124. // event to the Replot button.
  125. replotButton.addActionListener(
  126. new ActionListener(){
  127. public void actionPerformed(ActionEvent e){
  128. //Calculate distance
  129. double a = mX1-mX2;
  130. double b = mY1-mY2;
  131. sum=(Math.sqrt(a*a+b*b)) ;
  132. System.out.println("This Distance is: " + sum);
  133. //Pass a 3D array of pixel data to the
  134. // processing object and get a modified 3D array of pixel data back.
  135. threeDPixMod = imageProcessingObject.processImg(threeDPix, imgRows, imgCols, mX2, mY2);
  136. //Convert the modified pixel data to a
  137. // 1D array of pixel data.
  138. oneDPix = convertToOneDim(threeDPixMod,imgCols,imgRows);
  139. //Use the createImage() method to
  140. // create a new image from the 1D array of pixel data.
  141. modImg = createImage(new MemoryImageSource(imgCols,imgRows,oneDPix,0,imgCols));
  142. //Repaint the image display frame with
  143. // the original image at the top and
  144. // the modified pixel data at the bottom.
  145. display.repaint();
  146.  
  147. }//end actionPerformed
  148. }//end ActionListener
  149. );//end addActionListener
  150. //End anonymous inner class.
  151. //=========================================//
Reply With Quote Quick reply to this message  
Join Date: Nov 2004
Posts: 6,143
Reputation: jwenting is just really nice jwenting is just really nice jwenting is just really nice jwenting is just really nice 
Solved Threads: 212
Team Colleague
jwenting's Avatar
jwenting jwenting is offline Offline
duckman

Re: Action listener

 
0
  #9
Feb 21st, 2007
no, "this" in the actionlistener points to the actionlistener...
this in any class instance points to that instance.

And don't put comments on every closing brace, it's not needed and makes your code that much harder to read.
And don't use C++ style comments for blocks.
As people are clearly allowed to attack me but I'm not allowed to defend myself, I no longer post to this site.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the Java Forum


Views: 1842 | Replies: 8
Thread Tools Search this Thread



Tag cloud for Java
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC