Multiple class instances ??? But how ???
Hello all,
I have been facing a problem. I have several ( and i mean several ) class that contains swing elements, implements mouse listener and have a routine called draw. instead of creating new instances of them i declare them in a class as public static and just clear redraw screen. All was fine until suddenly i noticed after redrawing several times they cause multiple event, like a button click does the work of three or more work of the same button click ( tested using system.println()). whats the issue here ?? i can't present the whole source code 'coz it is of 17 files and about 4000 lines altogather (hey not boasting). But i can present u an overview, plz some1 give a solution. thanx in advance.
Code java:
class A implements MouseListener{
/* GUI Elements */
public A(){ .... }
draw(){ /* draws the GUI elements on the screen */ }
public void mouseClicked(){
CommonData.clearScreen() ;
CommonData.a.draw() ;
CommonData.b.draw() ;
.... so on ....
}
}
class B implements MouseListener{
/* GUI Elements */
public A(){ .... }
public draw(){ /* draws the GUI elements on the screen */ }
public void mouseClicked(){
CommonData.clearScreen() ;
CommonData.a.draw() ;
CommonData.b.draw() ;
.... so on ....
}
}
class CommonData{
public static A a ;
public static B b ;
clearScreen() { ... }
}
class StarttingClass{
public StarttingClass(){
CommonData.a = new A() ;
CommonData.b = new B() ;
CommonData.a.draw() ;
}
}
class MainClass{
public static void main(String [] args){
javax.swing.SwingUtilities.invokeLater(new Runnable(){
public void run(){
new StarttingClass() ;
}) ;
}
}
}
i hope u can understand this mess.
Re: Multiple class instances ??? But how ???
If you want help, you'll have to provide an SSCCE that demonstrates the problem. Otherwise we're just guessing.
But a good general rule to know is: You have no control over how many times a GUI Component's paint or paintComponent methods are called.
If that's not what you mean, then post that SSCCE, and we'll go from there.
Re: Multiple class instances ??? But how ???
Yes and No. I know when and what to paint but yes it depends on user interaction a very variable ummm thing.
Any way i am not a proffessional just a student trying to learn, what is SSCCE ? and how to post. I am willing to share of course.
Re: Multiple class instances ??? But how ???
Re: Multiple class instances ??? But how ???
thanx...and sorry for late reply. couldn't get the time to finish the gui link yet but on to it. anyway here is the SSCCE yeah could do it with a single class but my real structure is something like this. plz help.
Code java:
import javax.swing.* ;
import java.awt.event.* ;
import java.awt.* ;
class sampledemo{
public static void main(String [] args){
javax.swing.SwingUtilities.invokeLater(new Runnable(){
public void run(){
new sample() ;
}
}) ;
}
}
class sample{
JFrame win = null ;
public sample(){
win = new JFrame("sample") ;
win.setLayout(null) ;
CommonData.a = new A(win) ;
CommonData.b = new B(win) ;
CommonData.a.draw() ;
CommonData.b.draw() ;
win.setSize(710, 710) ;
win.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE) ;
win.setVisible(true) ;
}
}
class CommonData{
public static A a ;
public static B b ;
public static void clearWindow(JFrame win){
Component [] c = win.getRootPane().getContentPane().getComponents() ;
for (int i = 0 ; i < c.length ; i++){
c[i].addMouseListener(null) ;
win.remove(c[i]) ;
}
win.invalidate() ;
win.validate() ;
win.repaint() ;
}
}
class A implements MouseListener{
JButton click = new JButton("Click Me Several Times") ;
JFrame win = null ;
public A(JFrame win){
this.win = win ;
}
public void draw(){
click.setBounds(10, 10, 200, 25) ;
click.addMouseListener(this) ;
win.add(click) ;
}
public void mouseClicked(MouseEvent me){
if (me.getSource() == click){
CommonData.clearWindow(win) ;
CommonData.a.draw() ;
CommonData.b.draw() ;
System.out.println("click") ;
}
}
public void mousePressed(MouseEvent me){}
public void mouseEntered(MouseEvent me){}
public void mouseExited(MouseEvent me){}
public void mouseReleased(MouseEvent me){}
}
class B implements MouseListener{
JButton hello = new JButton("Click Me Later") ;
JFrame win = null ;
public B(JFrame win){
this.win = win ;
}
public void draw(){
hello.setBounds(50, 50, 200, 25) ;
hello.addMouseListener(this) ;
win.add(hello) ;
}
public void mouseClicked(MouseEvent me){
if (me.getSource() == hello){
CommonData.clearWindow(win) ;
CommonData.a.draw() ;
CommonData.b.draw() ;
System.out.println("hello world") ;
}
}
public void mousePressed(MouseEvent me){}
public void mouseEntered(MouseEvent me){}
public void mouseExited(MouseEvent me){}
public void mouseReleased(MouseEvent me){}
}
Re: Multiple class instances ??? But how ???
That's not an SSCCE, so I can't really run it. What exactly is the problem? What is happening that shouldn't be happening? What is supposed to happen when you click a button?
Hint: calling addMouseListener(null) does not remove any of the already added MouseListeners. You're adding redundant MouseListeners (why not use ActionListeners?) every time you click a button.
Re: Multiple class instances ??? But how ???
well sorry sir for not understanding SSCCE properly. well the problem is after clicking the first button several times ( redrawing several times ) the hello button click prints loads of "hello world" which should print it exactly once, but thank u, ur hint should work as a charm. :D thanx again.