Results 1 to 20 of 31
Thread: Swing components not working
- 11-02-2009, 07:10 PM #1
Swing components not working
I am trying to implement an interface which responds to mouseclick and with buttons Circle, Rectangle and Quit. On pressing Circle a Circle is drawn on the frame and so on. I cant get this code to work. Your help would be much appreciated.
Java Code:import java.awt.*; import java.awt.event.*; import java.awt.event.ActionListener; import java.util.EventListener; import javax.swing.*; interface ActionListener extends EventListener{ public void actionPerformed(ActionEvent ae); } public class india extends JFrame implements ActionListener { JButton b1, b2, b3; JLabel lbl; String isCircle="N", isRectangle="N"; public india(String strname){ super(strname); getContentPane().setLayout(new FlowLayout()); JPanel p = new JPanel(); b1 = new JButton("Rectangle"); b2 = new JButton("Circle"); b3 = new JButton("Quit"); p.setLayout(new FlowLayout()); p.add(b1); p.add(b2); p.add(b3); getContentPane().add(p); b1.addActionListener(this); b2.addActionListener(this); b3.addActionListener(this); } public void actionPerformed(ActionEvent ae){ String s = ae.getActionCommand(); if("Rectangle".equals(s)){ isRectangle = "Y"; repaint(); } if("Circle".equals(s)){ isCircle = "Y"; repaint(); } if("Quit".equals(s)){ System.exit(0); } } public void paint(Graphics g){ if(isRectangle == "Y"){ g.drawRect(10, 10, 50, 50); } if(isCircle == "Y"){ g.drawOval(10, 10, 50, 50); } } public static void main(String args[]){ india t = new india("Paint"); t.setSize(300, 300); t.setVisible(true); } }Last edited by primalpop; 11-03-2009 at 01:43 AM.
- 11-02-2009, 07:49 PM #2
Senior Member
- Join Date
- Jul 2009
- Posts
- 1,143
- Rep Power
- 5
Use the "Code" button, not the "Quote" button when posting code.
Never override the paint() method of a JFrame.
Custom paint should be done by overriding the paintComponent() method of JPanel or JComponent. Read the section from the Swing tutorial on Custom Painting for more information.
- 11-02-2009, 10:48 PM #3
Senior Member
- Join Date
- Aug 2009
- Location
- Pittsburgh, PA
- Posts
- 282
- Rep Power
- 4
Does this code compile without error? Does actionPerformed ever get called?
The reason I ask is that the code defines its own version of ActionListener:
This interface is distinct from the one in java.awt.event.Java Code:interface ActionListener extends EventListener{ public void actionPerformed(ActionEvent ae); }
So it seems to me the addListener lines should get compile errors
and actionPerformed should never be called.
BTW: The code uses Strings where boolean values are appropriate.
But this should not affect functionality.
- 11-02-2009, 11:02 PM #4
Member
- Join Date
- Oct 2009
- Posts
- 7
- Rep Power
- 0
pls check code
u correct code
import java.awt.*;
import java.awt.event.*;
import java.util.EventListener;
import javax.swing.*;
/*interface ActionListener extends EventListener {
public void actionPerformed(ActionEvent ae);
}*/
public class india extends JFrame implements ActionListener {
JButton b1, b2, b3;
JLabel lbl;
String isCircle = "N", isRectangle = "N";
public india(String strname) {
super(strname);
getContentPane().setLayout(new FlowLayout());
JPanel p = new JPanel();
b1 = new JButton("Rectangle");
b2 = new JButton("Circle");
b3 = new JButton("Quit");
p.setLayout(new FlowLayout());
p.add(b1);
p.add(b2);
p.add(b3);
getContentPane().add(p);
b1.addActionListener( this);
b2.addActionListener(this);
b3.addActionListener(this);
}
public void actionPerformed(ActionEvent ae) {
String s = ae.getActionCommand();
if ("Rectangle".equals(s)) {
isRectangle = "Y";
repaint();
}
if ("Circle".equals(s)) {
isCircle = "Y";
repaint();
}
if ("Quit".equals(s)) {
System.exit(0);
}
}
public void paint(Graphics g) {
if (isRectangle == "Y") {
g.drawRect(10, 10, 50, 50);
}
if (isCircle == "Y") {
g.drawOval(10, 10, 50, 50);
}
}
public static void main(String args[]) {
india t = new india("Paint");
t.setSize(300, 300);
t.setVisible(true);
}
}
-
Is that supposed to be "correct" code for the original poster? It ignores camickr's recommendations and has too many errors to mention.
To the original poster, please disregard arulmozs' post. If you still have problems, please post your code (with code tags) and ask any questions you may have. Best of luck.
- 11-03-2009, 01:55 AM #6
-
I would for one delete your interface "class":
get rid of it as it's only messing you up.Java Code:interface ActionListener extends EventListener{ public void actionPerformed(ActionEvent ae); }
- 11-03-2009, 02:16 AM #8
Thank you. It worked
Now the problem is when I press Rectangle or Circle nothing gets drawn on the Frame. Heres the edited code
Java Code:import java.awt.*; import java.awt.event.*; import javax.swing.*; public class india extends JFrame implements ActionListener { JButton b1, b2, b3; JLabel lbl; String isCircle="N", isRectangle="N"; public india(String strname){ super(strname); getContentPane().setLayout(new FlowLayout()); JPanel p = new JPanel(); b1 = new JButton("Rectangle"); b2 = new JButton("Circle"); b3 = new JButton("Quit"); p.setLayout(new FlowLayout()); p.add(b1); p.add(b2); p.add(b3); getContentPane().add(p); b1.addActionListener(this); b2.addActionListener(this); b3.addActionListener(this); } public void actionPerformed(ActionEvent ae){ String s = ae.getActionCommand(); if("Rectangle".equals(s)){ isRectangle = "Y"; repaint(); } if("Circle".equals(s)){ isCircle = "Y"; repaint(); } if("Quit".equals(s)){ System.exit(-1); } } public void paintComponent(Graphics g){ if(isRectangle == "Y"){ g.setColor(Color.RED); g.drawRect(10, 10, 50, 70); } if(isCircle == "Y"){ g.setColor(Color.RED); g.drawOval(10, 10, 30, 30); } } public static void main(String args[]){ india t = new india("Paint"); t.setSize(300, 300); t.setVisible(true); } }
-
You're still trying to draw in a JFrame, and this shouldn't be done (I think that camickr has already stated this). Shoot, JFrame doesn't even have a paintComponent method to override, so your method is simply ignored. Instead, draw in a class that extends JPanel (or other JComponent) and override IT's paintComponent method.
Also, don't try to compare Strings with ==. Use the equals method.
-
Also, as noted above, you're still using Strings (for example, "Y" or "N") where you should be using boolean variables (for example true and false). The suggestion was a good one, and strongly urge you to either make the change or tell us what about the suggestion you don't understand or don't agree with.
- 11-03-2009, 04:04 AM #11
Senior Member
- Join Date
- Jul 2009
- Posts
- 1,143
- Rep Power
- 5
I gave you a link to the Swing tutorial in my first reply.Wat all changes should I make to implement the JPanel?
You can lead a horse to water but you can't force it to drink!
- 11-14-2009, 02:32 PM #12
Many things kept me busy last week. Today I tried the java program again.
The assignment was to create a program such that rectangle class and oval circle inherits from the shape class. And square and circle from rectangle and oval respectively.
The pgm compiled and showed the frame with buttons and text fields. But when I click any of the buttons it gets stuck altogether. Any help would be much appreciated.
Java Code:import java.awt.*; import java.awt.event.*; import javax.swing.*; public class shape extends JFrame implements ActionListener{ JButton b1, b2, b3; JTextField t1, t2, t3, t4, t5, t6, t7, t8; Boolean isCircle, isRectangle; int a,b,c,d,e,f,j,h; String tx1, tx2, tx3, tx4, tx5, tx6, tx7, tx8; public shape(){ getContentPane().setLayout(new FlowLayout()); JPanel p = new JPanel(); b1 = new JButton("Rectangle"); b2 = new JButton("Circle"); b3 = new JButton("Exit"); t1 = new JTextField("100", 5); t2 = new JTextField("50", 5); t3 = new JTextField("50", 5); t4 = new JTextField("50", 5); t5 = new JTextField("50", 5); t6 = new JTextField("50", 5); t7 = new JTextField("50", 5); t8 = new JTextField("50", 5); p.setLayout(new FlowLayout()); p.add(b1); p.add(t1); p.add(t2); p.add(t3); p.add(t4); p.add(b2); p.add(t5); p.add(t6); p.add(t7); p.add(t8); getContentPane().add(p); b1.addActionListener(this); b2.addActionListener(this); b3.addActionListener(this); t1.addActionListener(this); t2.addActionListener(this); } public void actionPerformed(ActionEvent ae){ String s = ae.getActionCommand(); String tx1 = t1.getText(); String tx2 = t2.getText(); String tx3 =t3.getText(); String tx4 = t4.getText(); String tx5 = t5.getText(); String tx6 = t6.getText(); String tx7 = t7.getText(); String tx8 = t8.getText(); a = Integer.parseInt(tx1); b = Integer.parseInt(tx2); c = Integer.parseInt(tx3); d = Integer.parseInt(tx4); e = Integer.parseInt(tx5); f = Integer.parseInt(tx6); j = Integer.parseInt(tx7); h = Integer.parseInt(tx8); if("Rectangle".equals(s)){ rectangle r = new rectangle(); } else if("Circle".equals(s)){ oval o = new oval(); } if("Quit".equals(s)){ System.exit(-1); } } public static void main(String args[]){ JFrame frame = new JFrame("Paint"); shape t = new shape(); t.setSize(1000, 1000); t.setVisible(true); } } class rectangle extends shape{ public rectangle(){ if(a==b){ square s = new square(); } else { repaint(); } } public void paint(Graphics g){ g.drawRect(c, d, a, b); } } class square extends rectangle{ public square(){ repaint(); } public void paint(Graphics g){ g.drawRect(c, d, a, b); } } class oval extends shape{ public oval(){ if(e == f){ circle q = new circle(); } else{ repaint(); } } public void paint(Graphics g){ g.drawOval(j, h, e, f); } } class circle extends oval{ public circle(){ repaint(); } public void paint(Graphics g){ g.drawOval(j, h, e, f); } }
-
You are inadvertently causing recursion to occur, to wit you are causing code to be called over and over again without stop.
Note that when you construct rectangle the rectangle constructor creates a square, but square extends rectangle, so this calls the rectangle constructor again which creates another square which again calls the rectangle constructor again which... ad infinitum until you run out of memory.
The fix: don't do this.
-
Also, where is the JPanel where you are supposed to be doing your custom painting? As recommended by camickr, you really should read the custom painting tutorial. here's a better link: custom painting in swing
- 11-15-2009, 03:29 AM #15
Thank you.
So I have to define a function and not use a constructor of the class if I am inheriting I suppose.
The drawing of figures worked perfectly wen I did this pgm with no inheritance. So is there any need to use JPanel?Last edited by primalpop; 11-15-2009 at 03:31 AM.
-
There are other serious issues with your inheritance. For one your main class shape extends JFrame and then you use this as the parent object for all of your other shapes. Why would you want these classes to inherit from JFrame -- it doesn't make any sense to do this. You want Shape (note that class names should be capitalized) to be a simple class that encapsulates a shape's behaviors and do nothing else. Your JFrame should not be tied into this at all.
- 11-15-2009, 03:50 AM #17
-
No, you really really don't want to do this.
Your Shape class (again, the first letter of a class should be capitalized) should just have methods that are shared by all objects that subclass Shape. Period.
Your events, buttons, and all that should go in your GUI class, call it ShapeGUI if you desire.
You may wish to post your exact assignment instructions to help us better understand your needs and instructor-demanded limitations.
Much luck!
- 11-15-2009, 05:46 AM #19
First of all you need an awt Canvas to paint stuff you should change your y and n to true and false, and for that matter you could check for only the first character if you use a String buffer R C Q. and if you are new to swing you should get rid of the flow layout and use a gridlayout and a combination of panels
flow layout is the default layout and you don't even have to ask for itLast edited by aaroncarpet; 11-15-2009 at 05:47 AM. Reason: other thoughts
-
I strongly disagree with this. Stick with Swing here and use a JPanel. Do not revert to Canvas or even worse try to mix AWT components such as Canvas and Swing components such as JFrames. Also, I would think that a BorderLayout would better suit the OP's purposes with the BorderLayout.NORTH JPanel holding the controls (JButtons and JTextFields) and the BorderLayout.CENTER holding the drawing JPanel.
aaroncarpet, thanks for trying to be helpful, but please be careful with your advice here. If you are not very familiar with Swing graphics when answering a Swing question, let the original poster know this, else they'll believe you blindly.Last edited by Fubarable; 11-15-2009 at 05:54 AM.
Similar Threads
-
Problem In Swing Components
By SANDY_INDIA in forum AWT / SwingReplies: 1Last Post: 07-19-2008, 10:23 PM -
Swing Components Placing
By ne2000 in forum EclipseReplies: 2Last Post: 06-23-2008, 08:00 AM -
How to print Swing components
By Java Tip in forum java.awtReplies: 0Last Post: 06-22-2008, 11:04 PM -
Tab order on swing components
By ashvin@projectdemo.biz in forum AWT / SwingReplies: 1Last Post: 05-31-2008, 10:06 AM -
HTML on Swing Components
By Java Tip in forum Java TipReplies: 0Last Post: 11-27-2007, 09:51 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks