Results 1 to 3 of 3
  1. #1
    wdh321 is offline Senior Member
    Join Date
    Aug 2011
    Posts
    116
    Rep Power
    0

    Default How do you invoke methods with java beans?

    I have never used java beans until a day or so ago and i am having trouble calling methods.
    I have a circle class which contains the draw method for my circle and also a method to change the colour. I also have a circle component which creates the circle and also has a method calling the change colour method from circle.
    I have created a .jar file using the clean and build function in netbeans.
    I then create a JFrame using netbeans GUI builder and add the .jar file i created into the beans palette.
    I can add the the bean to the frame and it displays my circle.
    I have added a JButton that when clicked i want to call my change colour method.
    I have added an actionevent to the JButton where i though a call to my CircleComponent would work like this
    Java Code:
    CircleBeanComponent.setCircleColor();
    I get an error message saying that the non static method cannot be referenced from a static context.

    Circle class
    Java Code:
    package my.GraphicsBean;
    import java.awt.Color;
    import java.awt.Graphics;
    import java.awt.Graphics2D;
    import java.awt.geom.Ellipse2D;
    import javax.swing.JComponent;
    
    public class CircleBean extends JComponent
    {
        private String color = "green";
        public CircleBean(String aColor)
        {
            color = aColor;
        }
        
        public void paintComponent(Graphics2D g)
        {
            Graphics2D g2 = (Graphics2D) g;
    
            Ellipse2D.Double circle = new Ellipse2D.Double(0,0,200,200);
            g2.fill(circle);
        }
        
        
        public void setFillColor(Graphics2D g)
        {
            Graphics2D g2 = (Graphics2D) g;
    
            if(color.equals("green"))
            {
                g2.setColor(Color.RED);
                color = "red";
            }
            if(color.equals("red"))
            {
                g2.setColor(Color.ORANGE);
                color = "orange";
            }
            if(color.equals("orange"))
            {
                g2.setColor(Color.GREEN);
                color = "green";
            }
            
            super.paintComponent(g2);
            
        }
        
    }
    circleComponent
    Java Code:
    package my.GraphicsBean;
    import java.awt.Dimension;
    import java.awt.Graphics;
    import java.awt.Graphics2D;
    import javax.swing.JComponent;
    
    public class CircleBeanComponent extends JComponent
    {
        CircleBean circle;
        public void paintComponent(Graphics g)
        {
            Graphics2D g2 = (Graphics2D) g;
            
            circle = new CircleBean("green");
            circle.setPreferredSize(new Dimension(200,200));
            circle.paintComponent(g2);
        }
        
        public void setCircleColor()
        {
            circle.setFillColor();
        }
    }

  2. #2
    arieltal is offline Member
    Join Date
    Sep 2012
    Posts
    34
    Rep Power
    0

    Default Re: How do you invoke methods with java beans?

    you need to create an instance of CircleBeanComponent, then call the method.
    Java Code:
    CircleBeanComponent c = new CircleBeanComponent();
    c.setCircleColor();

  3. #3
    wdh321 is offline Senior Member
    Join Date
    Aug 2011
    Posts
    116
    Rep Power
    0

    Default Re: How do you invoke methods with java beans?

    Thank you, yes that did sort that issue.
    The button doesn't actually change the circle colour when pressed though. The action event i added to the button looks like this.
    I had to insert the Graphics2D parts to it for it to work. So now i get no error message, but it still doesn't work how i intended.
    Java Code:
    private void changeColor(java.awt.event.ActionEvent evt) {
            CircleBeanComponent c = new CircleBeanComponent();
            Graphics2D g = null;
            Graphics2D g2 = (Graphics2D) g;
            c.setCircleColor(g);
        }

Similar Threads

  1. Invoke methods
    By wdh321 in forum New To Java
    Replies: 3
    Last Post: 04-18-2012, 03:34 PM
  2. java Executables invoke in mainframes
    By jaggz in forum New To Java
    Replies: 0
    Last Post: 05-22-2011, 02:59 PM
  3. How to invoke a C API using Java program
    By mgopi in forum New To Java
    Replies: 6
    Last Post: 12-27-2008, 01:17 PM
  4. How to make Beans Lazily-instantiating beans
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 03-30-2008, 11:10 AM
  5. How to make Beans Lazily-instantiating beans
    By JavaBean in forum Java Tip
    Replies: 0
    Last Post: 09-26-2007, 09:41 PM

Posting Permissions

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