Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 7 of 7

Thread: Multiple class instances ??? But how ???

  1. #1
    Junior Member
    Join Date
    Nov 2010
    Location
    Bangladesh
    Posts
    27
    Thanks
    3
    Thanked 1 Time in 1 Post

    Exclamation 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.
    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.
    Last edited by dumb_terminal; December 1st, 2010 at 03:41 AM. Reason: [highlight = java] parsing failed due to spaces ;


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,529
    My Mood
    Hungover
    Thanks
    144
    Thanked 647 Times in 552 Posts

    Default 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.

  3. #3
    Junior Member
    Join Date
    Nov 2010
    Location
    Bangladesh
    Posts
    27
    Thanks
    3
    Thanked 1 Time in 1 Post

    Default 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.

  4. #4
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,529
    My Mood
    Hungover
    Thanks
    144
    Thanked 647 Times in 552 Posts

    Default Re: Multiple class instances ??? But how ???

    This is a link: SSCCE
    So is this: Painting in AWT and Swing

  5. #5
    Junior Member
    Join Date
    Nov 2010
    Location
    Bangladesh
    Posts
    27
    Thanks
    3
    Thanked 1 Time in 1 Post

    Default 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.
    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){}
    }

  6. #6
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,529
    My Mood
    Hungover
    Thanks
    144
    Thanked 647 Times in 552 Posts

    Default 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.

  7. The Following User Says Thank You to KevinWorkman For This Useful Post:

    dumb_terminal (December 2nd, 2010)

  8. #7
    Junior Member
    Join Date
    Nov 2010
    Location
    Bangladesh
    Posts
    27
    Thanks
    3
    Thanked 1 Time in 1 Post

    Default 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. thanx again.

Similar Threads

  1. use multiple gui displays
    By jonwymore in forum AWT / Java Swing
    Replies: 1
    Last Post: November 19th, 2010, 10:11 AM
  2. Multiple instances of linked list
    By thedolphin13 in forum What's Wrong With My Code?
    Replies: 5
    Last Post: October 11th, 2010, 08:48 PM
  3. Multiple Queues
    By fh84 in forum Threads
    Replies: 1
    Last Post: December 3rd, 2009, 02:28 PM
  4. creating a controller to allow instances to be created from keyboard
    By ss7 in forum File I/O & Other I/O Streams
    Replies: 4
    Last Post: November 2nd, 2009, 01:30 PM
  5. Replies: 6
    Last Post: May 15th, 2009, 06:06 PM