Results 1 to 3 of 3
- 11-06-2012, 05:28 PM #1
Senior Member
- Join Date
- Aug 2011
- Posts
- 116
- Rep Power
- 0
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
I get an error message saying that the non static method cannot be referenced from a static context.Java Code:CircleBeanComponent.setCircleColor();
Circle class
circleComponentJava 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); } }
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(); } }
- 11-06-2012, 05:32 PM #2
Member
- Join Date
- Sep 2012
- Posts
- 34
- Rep Power
- 0
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();
- 11-06-2012, 06:51 PM #3
Senior Member
- Join Date
- Aug 2011
- Posts
- 116
- Rep Power
- 0
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
-
Invoke methods
By wdh321 in forum New To JavaReplies: 3Last Post: 04-18-2012, 02:34 PM -
java Executables invoke in mainframes
By jaggz in forum New To JavaReplies: 0Last Post: 05-22-2011, 01:59 PM -
How to invoke a C API using Java program
By mgopi in forum New To JavaReplies: 6Last Post: 12-27-2008, 12:17 PM -
How to make Beans Lazily-instantiating beans
By Java Tip in forum Java TipReplies: 0Last Post: 03-30-2008, 10:10 AM -
How to make Beans Lazily-instantiating beans
By JavaBean in forum Java TipReplies: 0Last Post: 09-26-2007, 08:41 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks