Page 1 of 2 12 LastLast
Results 1 to 20 of 22
Like Tree1Likes

Thread: Action/Change Listeners

  1. #1
    Conor-Magee is offline Member
    Join Date
    Nov 2011
    Posts
    16
    Rep Power
    0

    Default Action/Change Listeners

    Alright guys I have a horrible problem... Each week in University we're building towards creating a program going through each section one at a time... Im at the final hurdle and I am completely stuck...

    We need to create a program with 3 JMenuItems: Square, Circle and Triangle... When you click on each of these it should create the shapes on the main panel... Theres also a slider and 2 text boxes (Area and Boundary Length) when I move the slider it should calculate the area and blength and also adjust the size of the drawn shape... Ive got the slider working so that the text boxes move from 0-100 but not with the equations...

    Ive commented out some code Ive tried.. If someone could take a look at it, and if their able to supply code that will work.. Id like to see how action and change listeners are used as well as using it to change the size of specified shapes...

    Thanks

    -Conor

    (Wouldnt let me attach the Netbeans package to this thread, If you post your email I could send you it and you could possibly get back to me via email or here?)

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Action/Change Listeners

    Create a jar file with NetBeans that includes your source code, then change the file extension from jar to zip and then you can post it. Better still, create and post an SSCCE.

  3. #3
    Conor-Magee is offline Member
    Join Date
    Nov 2011
    Posts
    16
    Rep Power
    0

    Default Re: Action/Change Listeners

    How do you complile a program to a jarfile using netbeans including the source code... I've never had to do that before :/

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Action/Change Listeners

    The help files are quite useful (as always). Search on "building a jar file" or something similar. You will need to read section in this tutorial on how to include the .java files in the jar as well.

  5. #5
    Conor-Magee is offline Member
    Join Date
    Nov 2011
    Posts
    16
    Rep Power
    0

    Default Re: Action/Change Listeners

    Because I use winRar to compress my files , It wont let me compress it to a normal Zip file... I will be able to e-mail if anyone would kindly take a look at it?

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Action/Change Listeners

    If you can create a jar file, you've already got a zip file since jar uses zip as its underlying structure. Again, create a jar that contains the source files (again, the NetBeans help files will tell you all you need to know, and as always if you get stuck on a step, ask), and then simply change the jar's extension to zip and upload it here.

  7. #7
    Conor-Magee is offline Member
    Join Date
    Nov 2011
    Posts
    16
    Rep Power
    0

    Default Re: Action/Change Listeners

    Ok I think I've uploaded it there... The Jar file is in the dist folder in the main folder... that has all the classes and that involved there
    Attached Files Attached Files

  8. #8
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Action/Change Listeners

    Some suggestions:

    • Give MyControlPanel a public method setShapes(Shapes s) and use it to change the value of its Shapes field (which should not be capitalized -- shapes).
    • That field should most definitely *not* be static.
    • MyControlPanel's paintComponent method currently is hard coded to draw an oval. Give it an some if and if/else blocks and have it use the value of the shapes variable to decide what to draw. First though have it test that shapes is not null and if it is, draw nothing.
    • paintComponent is currently hard-coded to draw an oval of 75 by 75 size. Change this so that it uses the value from the JSlider to decide how big a shape to draw.
    • After inside of setShapes(...) call repaint() after setting the shapes field.
    • Same for the slider. In its listener call repaint() as its last method.
    • Make Shapes an abstract class since it needs to be extended to be useful.
    • Get rid of MyFrame's static Shapes variable. There are no need for static variables and certainly no need for MyFrame to even have a Shapes variable.
    • In your menu's ActionListener code, call setShapes on your MyControlPanel variable.

  9. #9
    Conor-Magee is offline Member
    Join Date
    Nov 2011
    Posts
    16
    Rep Power
    0

    Default Re: Action/Change Listeners

    Im confused, I'm proper beginner... right I've started creating the method such as "public void setShapes(Shapes s)" and what not I dont understand what you mean by use it to change the value of its shapes field?

  10. #10
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Action/Change Listeners

    Quote Originally Posted by Conor-Magee View Post
    Im confused, I'm proper beginner... right I've started creating the method such as "public void setShapes(Shapes s)" and what not I dont understand what you mean by use it to change the value of its shapes field?
    Have you ever had a class that had "setter" and "getter" methods (also known as accessor and mutator methods)? That's all I'm asking. That you again, make the shapes variable non-static, and then give it a setter method, called setShapes(...) that sets the variable. The method will be just slightly different from other setter methods in that it will have two lines not one: 1) set the instance's shapes variable with the one passed into the method via its parameter (as is typical of most all setter methods), and 2) call repaint(). That's it.

    For example here's a class with an int variable, foo, and a setFoo and getFoo setter and getter method pair:

    Java Code:
    public Class Example {
      private int foo;
    
    
      // setter method
      public void setFoo(int foo) {
          this.foo = foo;
      }
    
      // getter method
      public int getFoo() {
          return foo;
      }
    
    }

  11. #11
    Conor-Magee is offline Member
    Join Date
    Nov 2011
    Posts
    16
    Rep Power
    0

    Default Re: Action/Change Listeners

    No sorry, Ive never heard of the terms setter and getter methods :/

    So at the top I will have: "Shapes shape;"
    And I will have the method:

    setShapes(Shapes s)
    {
    Shapes s = shape;
    repaint();
    }

  12. #12
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Action/Change Listeners

    Quote Originally Posted by Conor-Magee View Post
    No sorry, Ive never heard of the terms setter and getter methods :/

    So at the top I will have: "Shapes shape;"
    And I will have the method:

    setShapes(Shapes s)
    {
    Shapes s = shape;
    repaint();
    }
    Close but not quite. In your method above you are declaring a Shapes variable, s, inside of the method, and per the rules of scoping, this variable only exists within this method. Once the flow of code leaves this method, s disappears. You must use a setter method to set a class field, a Shapes variable that has been declared in the class, and again that isn't static.

  13. #13
    Conor-Magee is offline Member
    Join Date
    Nov 2011
    Posts
    16
    Rep Power
    0

    Default Re: Action/Change Listeners

    Java Code:
    public Class MyControlPanel {
      private Shapes Shape;
    
    
      // Set the Shape.
      public void setShape(Shapes shape) {
          this.shape = shape;
      }
    
      // Get the shape.
      public void getShape() {
          return shape;
      }
    
    }
    Do I need to use a Getter method in my own code? And more importantly, does that code above look as if im moving in the right direction, I wanna try and get this functioning as I need by tonight or tomorrow morning.

  14. #14
    Conor-Magee is offline Member
    Join Date
    Nov 2011
    Posts
    16
    Rep Power
    0

    Default Re: Action/Change Listeners

    What I want working firstly though is when I move my slider the numbers of the text boxes go up based on the length and area equations... The most progress I've got is:

    Java Code:
    public class MyChangeAction implements ChangeListener
            {  @Override
                public void stateChanged(ChangeEvent e)
                {                
                        int currentValue = slider.getValue();
                        String val = Integer.toString(currentValue);
                        area.setText(val);
                        blength.setText(val);
                }
            }
    This actually works, but the completely wrong way I need it to... When I move the slider the value of both boxes go from 0-100 with the slider... I can see how this happens... Ive tried implementing the following code to bring my equations into it... By cross-referencing my code I uploaded maybe you can help point out where I've went wrong, I think its something silly thats preventing it from working...

    Java Code:
    public class MyChangeAction implements ChangeListener
            {  @Override
                public void stateChanged(ChangeEvent e)
                { 
                       JSlider source = (JSlider)e.getSource();
                       if (source.getValueIsAdjusting()) return;
                        int currentValue = slider.getValue();
                        MyControlPanel.Shape=MyFrame.Shape;
                        area.setText(Double.toString(Shape.Area(currentValue)));
                        blength.setText(Double.toString(Shape.Area(currentValue)));
                       }
            }
    Thats the code that doesn't work when entered... Its calling from the shapes class... :S

  15. #15
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Action/Change Listeners

    Quote Originally Posted by Conor-Magee View Post
    Java Code:
    public Class MyControlPanel {
      private Shapes Shape;
    
    
      // Set the Shape.
      public void setShape(Shapes shape) {
          this.shape = shape;
      }
    
      // Get the shape.
      public void getShape() {
          return shape;
      }
    
    }
    Do I need to use a Getter method in my own code? And more importantly, does that code above look as if im moving in the right direction, I wanna try and get this functioning as I need by tonight or tomorrow morning.
    This won't compile since you call your variable Shape in the class and this.shape in the method. Both variables need to start lower-case.

  16. #16
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Action/Change Listeners

    Quote Originally Posted by Conor-Magee View Post
    What I want working firstly though is when I move my slider the numbers of the text boxes go up based on the length and area equations... The most progress I've got is:

    Java Code:
    public class MyChangeAction implements ChangeListener
            {  @Override
                public void stateChanged(ChangeEvent e)
                {                
                        int currentValue = slider.getValue();
                        String val = Integer.toString(currentValue);
                        area.setText(val);
                        blength.setText(val);
                }
            }
    This actually works, but the completely wrong way I need it to... When I move the slider the value of both boxes go from 0-100 with the slider... I can see how this happens... Ive tried implementing the following code to bring my equations into it... By cross-referencing my code I uploaded maybe you can help point out where I've went wrong, I think its something silly thats preventing it from working...

    Java Code:
    public class MyChangeAction implements ChangeListener
            {  @Override
                public void stateChanged(ChangeEvent e)
                { 
                       JSlider source = (JSlider)e.getSource();
                       if (source.getValueIsAdjusting()) return;
                        int currentValue = slider.getValue();
                        MyControlPanel.Shape=MyFrame.Shape;
                        area.setText(Double.toString(Shape.Area(currentValue)));
                        blength.setText(Double.toString(Shape.Area(currentValue)));
                       }
            }
    Thats the code that doesn't work when entered... Its calling from the shapes class... :S
    Will the slider's ChangeListener by inside of the MyControlPanel class or will it be a separate class?
    Either way, again -- do not use a static Shape variable variable. If this listener is inside of MyContorlPanel, then what you want the slider to do is call repaint on the MyControlPanel instance that's it. Then in paintComponent get the value from the JSlider and use this to decide how large the drawing should be.

  17. #17
    Conor-Magee is offline Member
    Join Date
    Nov 2011
    Posts
    16
    Rep Power
    0

    Default Re: Action/Change Listeners

    I dont understand what you mean, dunno if were on the same page here...

    How would I get the value of my Boundary Length and Area text boxes to display the Length and Area instead of the same value the slider is sitting at...
    For example take my 'Circle' class:

    Java Code:
    public class Circle extends Shapes 
    {
        public double Area(int L)
            {
                return L*L*Math.PI;
            }
     
        public double Boundary(int L)
            {
                return 2*L*Math.PI;
            }
    }
    I need the variable 'L' in this code to be assigned the value of the slider, so the equation is calculated and put into the BLength and Area text boxes respectively... You know where Im coming from?

  18. #18
    Conor-Magee is offline Member
    Join Date
    Nov 2011
    Posts
    16
    Rep Power
    0

    Default Re: Action/Change Listeners

    I would probably find it helpful if you had the time to provide example code rather than a description... Im more of a visual learner, like how you would code a slider to redraw a shape based on the 2 text box values.

  19. #19
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Action/Change Listeners

    OK, I'll show you this one bit of code, but all else you're on your own:

    Java Code:
    import java.awt.*;
    import javax.swing.*;
    import javax.swing.event.*;
    
    public class SliderEg extends JPanel {
       private static final int PREF_W = 600;
       private static final int PREF_H = PREF_W;
       private JSlider slider = new JSlider(0, 100, 50);
    
       public SliderEg() {
          slider.setPaintLabels(true);
          slider.setPaintTicks(true);
          slider.setMajorTickSpacing(20);
          slider.setMinorTickSpacing(5);
          slider.addChangeListener(new ChangeListener() {
             public void stateChanged(ChangeEvent changeEvent) {
                int value = slider.getValue();
                // do whatever you need with slider's value here
                // but also call repaint!
                repaint();
             }
          });
          slider.setOpaque(false);
          
          setLayout(new BorderLayout());
          add(slider, BorderLayout.SOUTH);
       }
       
       @Override
       protected void paintComponent(Graphics g) {
          super.paintComponent(g);
          
          // extract the slider's value and use it however 
          // you see fit to set the length of your graphic
          // here it's scaled such that when slider hits 100
          // the oval fills the JPanel
          int length = (getWidth() * slider.getValue()) / 100;
          g.drawOval(0, 0, length, length);
       }
       
       @Override
       public Dimension getPreferredSize() {
          return new Dimension(PREF_W, PREF_H);
       }
    
       private static void createAndShowGui() {
          SliderEg mainPanel = new SliderEg();
    
          JFrame frame = new JFrame("Slider Eg");
          frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
          frame.getContentPane().add(mainPanel);
          frame.pack();
          frame.setLocationByPlatform(true);
          frame.setVisible(true);
       }
    
       public static void main(String[] args) {
          SwingUtilities.invokeLater(new Runnable() {
             public void run() {
                createAndShowGui();
             }
          });
       }
    }

  20. #20
    Conor-Magee is offline Member
    Join Date
    Nov 2011
    Posts
    16
    Rep Power
    0

    Default Re: Action/Change Listeners

    Thanks for your help but this is way over my head :/ I give up

    My heads gonna explode

Page 1 of 2 12 LastLast

Similar Threads

  1. action listeners
    By Ike in forum New To Java
    Replies: 13
    Last Post: 11-07-2011, 10:31 AM
  2. Adding Action Listeners
    By mamoonrizwan in forum Advanced Java
    Replies: 2
    Last Post: 08-07-2011, 06:25 PM
  3. Applets issue with Action Listeners
    By aadem in forum New To Java
    Replies: 2
    Last Post: 03-19-2011, 06:17 PM
  4. Creating a GUI events with action listeners
    By sidd0123 in forum AWT / Swing
    Replies: 8
    Last Post: 04-02-2010, 11:32 PM
  5. Action Event and Listeners
    By lost1 in forum New To Java
    Replies: 3
    Last Post: 11-14-2007, 04:26 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •