The Artima Developer Community
Sponsored Link

Java Answers Forum
Multiple event handler and how to select

4 replies on 1 page. Most recent reply: Nov 25, 2002 8:04 PM by Chris Reck

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 4 replies on 1 page
Chris Reck

Posts: 7
Nickname: creck
Registered: Nov, 2002

Multiple event handler and how to select Posted: Nov 21, 2002 9:13 AM
Reply to this message Reply
Advertisement
I created a non-modal dialog and placed three
event handlers in it.
mouseListener for reading buttons
actionListener for reading the menu
and ComponentListener.

They all do what I want with a little extra.
When I select an item from the menu the buttons that are
under the menu also fire off an event. I would like to find
out what I need to do to stop this from happening.

Thanks in advance


Matt Gerrans

Posts: 1153
Nickname: matt
Registered: Feb, 2002

Re: Multiple event handler and how to select Posted: Nov 21, 2002 11:21 AM
Reply to this message Reply
These are all separate listener instances, right?

Maybe you can post a little code to shed some light on the matter (be sure and use the java tag mentioned in the "Formatting Your Post" Box on the right" if you do).

Chris Reck

Posts: 7
Nickname: creck
Registered: Nov, 2002

Re: Multiple event handler and how to select Posted: Nov 22, 2002 4:15 AM
Reply to this message Reply
Here is the code in question. It's not real pretty but it's doing most of what I want.

   class IconButtonEdit extends JDialog implements MouseInputListener, ComponentListener, ActionListener {
    	Insets IBEinsets = null;
    	MouseEvent me = null;
    	int oldSize = 0;
    	String editMode = "Edit";
		JButton[] buttons = new JButton[32*32];
		Object pix = null;
		
		public IconButtonEdit(ImageTry mParent,  int iconW, int iconH, Insets MFinsets) {
			super (mParent, "Icon Editing panel", true);
			addWindowListener(new DialogWindowMonitor());
			setModal(false);
			setLocation(MFinsets.left, MFinsets.top+23);
			pix = iconPallet.pg.getPixels();
			JButton jb;
			for (int i = 0; i < iconW*iconH; i++) {
				jb = new JButton();
				jb.setBackground(new Color(((int[])pix)[i]));
				jb.setBorderPainted(false);
 				jb.addMouseListener(this);
	 			jb.setName(""+i);
				getContentPane().add(jb);
				buttons[i] = jb;
				pixels[i] = ((int[])pix)[i];
			}
 			addComponentListener(this);
 			//addMouseListener(this);
 			//addMouseMotionListener(this);
			
			JMenuBar editMenuBar = new JMenuBar();
			setJMenuBar(editMenuBar);
	
			JMenu icon = new JMenu("Icon");
			icon.setMnemonic('I');
			JMenu view = new JMenu("View");
			view.setMnemonic('V');
			
			JMenuItem clearIcon = new JMenuItem("Clear");
			clearIcon.addActionListener(this);
			JMenuItem reloadIcon = new JMenuItem("Reload");
			reloadIcon.addActionListener(this);
			JMenuItem getColorIcon = new JMenuItem("Get Color");
			getColorIcon.addActionListener(this);
			JMenuItem saveIcon = new JMenuItem("Save");
			saveIcon.addActionListener(this);
			JMenuItem saveAtIcon = new JMenuItem("Save-At");
			saveAtIcon.addActionListener(this);
			JMenuItem exitIcon = new JMenuItem("Exit");
			exitIcon.addActionListener(this);
			
			icon.add(clearIcon);
			icon.add(reloadIcon);
			icon.add(getColorIcon);
			icon.add(saveIcon);
			icon.add(saveAtIcon);
			icon.add(exitIcon);
			
			editMenuBar.add(icon);			
		
			getContentPane().setLayout(new GridLayout(iconW,iconH));
			pack();
			IBEinsets = getInsets();
			editSize = screenHeight-cp.getHeight()-MFinsets.top-MFinsets.bottom-80;
			editSize = (editSize/32)*32;
			oldSize = editSize;
			setSize(editSize+IBEinsets.left+IBEinsets.right, editSize+IBEinsets.top);
	    	setVisible(true);
	    }
	    
 
		public void actionPerformed(ActionEvent ae) {
			
			System.out.println(ae.getActionCommand());
			if(ae.getActionCommand() == "Clear")
			{
				for (int i = 0; i < 32*32; i++) {
					pixels[i] = Color.black.getRGB();
				}
				icon = createImage(new MemoryImageSource(32, 32, pixels, 0, 32));
				iconPV.repaint();					
			}
 
			if(ae.getActionCommand() == "Reload")
			{	
				for (int i = 0; i < 32*32; i++) {
					buttons[i].setBackground(new Color(((int[])pix)[i]));
					pixels[i] = ((int[])pix)[i];
				}
				icon = createImage(new MemoryImageSource(32, 32, pixels, 0, 32));
				iconPV.repaint();					
			}
 
			
			if(ae.getActionCommand() == "Get Color")
			{
				editMode = "getColor";
			}
			
			if(ae.getActionCommand() == "Save")
			{
				gImg.drawImage(icon,
					iconOriginX,
					iconOriginY,
					iconOriginX+32,
					iconOriginY+32,
					0,0,32,32,null);
				img = backImg;
				try {
					GIFEncoder encode = new GIFEncoder(img);
					OutputStream output = new BufferedOutputStream(
					    new FileOutputStream("test1.dat"));
					encode.Write(output);
				}
				catch (AWTException awte) {}
				catch (FileNotFoundException FNF) {}
				catch (IOException ie) {}
			}
								
			if(ae.getActionCommand() == "Exit")
			{	
				showIconPallet();
				setVisible(false);
				dispose();
				closeIconPreview();
				closeColorPanel();
			}					
		}
			
	    public void mouseEntered(MouseEvent me) {
		  	System.out.println("test");
		  	if ((me.getModifiers() & InputEvent.BUTTON1_MASK) !=0 )
			{	c = ColorPanel.getPriColor();
				me.getComponent().setBackground(c);
				pixels[Integer.valueOf(me.getComponent()
					.getName()).intValue()] = c.getRGB();
				icon = createImage(new MemoryImageSource(32, 32, pixels, 0, 32));
				iconPV.repaint();
			}
		  	if ((me.getModifiers() & InputEvent.BUTTON3_MASK) !=0 )
	    	{	c = ColorPanel.getSecColor();
				me.getComponent().setBackground(c);
				pixels[Integer.valueOf(me.getComponent()
					.getName()).intValue()] = c.getRGB();
				icon = createImage(new MemoryImageSource(32, 32, pixels, 0, 32));
				iconPV.repaint();
	    	}
		}
 
	    public void mousePressed(MouseEvent me) {
			if(editMode == "Edit") {
		  		if ( (me.getModifiers() & InputEvent.BUTTON1_MASK) !=0 )
		       		c = ColorPanel.getPriColor();
				
			   	else
			   		c = ColorPanel.getSecColor();
			    		
				me.getComponent().setBackground(c);
				pixels[Integer.valueOf(me.getComponent()
					.getName()).intValue()] = c.getRGB();
				icon = createImage(new MemoryImageSource(32, 32, pixels, 0, 32));
				iconPV.repaint();
			}
			
	
			if(editMode == "getColor") {
	      		if ( (me.getModifiers() & InputEvent.BUTTON1_MASK) !=0 ) {
	           		ColorPanel.setPriColor(me.getComponent().getBackground()) ;
	           		ColorPanel.preView1.setBackground(ColorPanel.getPriColor());
	           	}
		    	else {
		    		ColorPanel.setSecColor(me.getComponent().getBackground());
	           		ColorPanel.preView2.setBackground(ColorPanel.getSecColor());
	           	}
		    		
		    	editMode = "Edit";
		  	}
		}
    	public void mouseMoved(MouseEvent me) {}
 
	    public void mouseClicked(MouseEvent me) {}
	
    	public void mouseReleased(MouseEvent me) {}
 
    	public void mouseDragged(MouseEvent me) {}
 
    	public void mouseExited(MouseEvent me) {}
 
		
 
		public void componentResized(ComponentEvent ce) {
			if(getWidth() != oldSize+IBEinsets.left+IBEinsets.right)
			{
				editSize = getWidth();
				if (editSize > screenHeight-(int)(screenHeight*.1))
					editSize = screenHeight-(int)(screenHeight*.1);
				editSize = (editSize/32)*32;
				oldSize = editSize;
			}
			else if(this.getHeight() != oldSize+IBEinsets.top)
			{	
				editSize = getHeight();
				if (editSize > screenHeight-(int)(screenHeight*.1))
					editSize = screenHeight-(int)(screenHeight*.1);
				editSize = (editSize/32)*32;
				oldSize = editSize;
			}
	
			setSize(editSize+IBEinsets.left+IBEinsets.right, editSize+IBEinsets.top-5);
		}
	    
	    public void componentMoved (ComponentEvent ce) {}
 
		public void componentShown (ComponentEvent ce) {}
 
		public void componentHidden (ComponentEvent ce) {}
		
	}

Matt Gerrans

Posts: 1153
Nickname: matt
Registered: Feb, 2002

Re: Multiple event handler and how to select Posted: Nov 25, 2002 6:22 PM
Reply to this message Reply
Well, I tried to compile your code, but it was hopeless, since some parts are missing.

Based on looking at what you have here, I don't immediately see the problem, but I would suggest using inner class instances (either anonymous or not) as handlers for the events instead of jumbling everything into a main class that implements a bevy of listener interfaces (where does this idiom come from, I've seen a lot of people do this here, but it seems to me a bad habit that fosters unnecessarily complicated and tightly-coupled code).

Chris Reck

Posts: 7
Nickname: creck
Registered: Nov, 2002

Re: Multiple event handler and how to select Posted: Nov 25, 2002 8:04 PM
Reply to this message Reply
> Well, I tried to compile your code, but it was hopeless,
> since some parts are missing.
>
> Based on looking at what you have here, I don't
> immediately see the problem, but I would suggest using
> inner class instances (either anonymous or not) as
> handlers for the events instead of jumbling everything
> into a main class that implements a bevy of listener
> interfaces (where does this idiom come from, I've seen a
> lot of people do this here, but it seems to me a bad
> habit that fosters unnecessarily complicated and
> tightly-coupled code).

Sorry to mislead you, this is just the module that displays this JDialog. There is quite abit of other code for the rest of the program. This does work but the actionEvent and the mouseEvent both see the same event and act upon it.

Buttons that are under the menu when it open see the event that I meant just for the menu.

As for the bad habit please point me to information that helps me correct the problem. I know that it is a problem, but I don't understand what I need to do to fix it. If you would like to look at the whole program let me know how is the best way to do that.

thanks again

Flat View: This topic has 4 replies on 1 page
Topic: urgent excercise help -please :) Previous Topic   Next Topic Topic: alternative to Keyboard.cs1

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use