943,945 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Unsolved
  • Views: 2121
  • Java RSS
Feb 15th, 2007
0

Action listener

Expand Post »
Hi,

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

Regards.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Light Poster
anti_genius is offline Offline
27 posts
since Nov 2006
Feb 15th, 2007
0

Re: Action listener

Just quick pseudo-code

Java Syntax (Toggle Plain Text)
  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
Moderator
Featured Poster
Reputation Points: 2786
Solved Threads: 873
Code tags enforcer
peter_budo is offline Offline
6,656 posts
since Dec 2004
Feb 15th, 2007
0

Re: Action listener

Click to Expand / Collapse  Quote originally posted by peter_budo ...
Just quick pseudo-code

Java Syntax (Toggle Plain Text)
  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.
Reputation Points: 10
Solved Threads: 0
Light Poster
anti_genius is offline Offline
27 posts
since Nov 2006
Feb 19th, 2007
0

Re: Action listener

Nobody knows any solution of the problem???
Reputation Points: 10
Solved Threads: 0
Light Poster
anti_genius is offline Offline
27 posts
since Nov 2006
Feb 20th, 2007
0

Re: Action listener

post some relevant code please
Reputation Points: 683
Solved Threads: 53
Posting Virtuoso
Infarction is offline Offline
1,580 posts
since May 2006
Feb 20th, 2007
0

Re: Action listener

"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).
Team Colleague
Reputation Points: 1658
Solved Threads: 331
duckman
jwenting is offline Offline
7,719 posts
since Nov 2004
Feb 21st, 2007
0

Re: Action listener

Click to Expand / Collapse  Quote originally posted by Infarction ...
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()
Reputation Points: 10
Solved Threads: 0
Light Poster
anti_genius is offline Offline
27 posts
since Nov 2006
Feb 21st, 2007
0

Re: Action listener

Click to Expand / Collapse  Quote originally posted by Infarction ...
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()


Java Syntax (Toggle Plain Text)
  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. //=========================================//
Reputation Points: 10
Solved Threads: 0
Light Poster
anti_genius is offline Offline
27 posts
since Nov 2006
Feb 21st, 2007
0

Re: Action listener

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.
Team Colleague
Reputation Points: 1658
Solved Threads: 331
duckman
jwenting is offline Offline
7,719 posts
since Nov 2004
Mar 22nd, 2011
-1
Re: Action listener
try having the action listener call a method that resizes the frame instead of resizing the frame directly
Reputation Points: 7
Solved Threads: 1
Newbie Poster
alastair1008 is offline Offline
13 posts
since Oct 2010

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: Lexical Analyzer
Next Thread in Java Forum Timeline: Removing (or preventing) Duplicates in ArrayList





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC