Solved

Java Mouse Adapter - event handling -

Posted on 2009-03-29
2
1,506 Views
Last Modified: 2013-11-23
Hi i have a JPane containing 100 JButtons.  The initial rollover icon for each button is a right arrow.  I need to change the rollover icon to a down arrow if the user clicks the right-mouse button, back to right arrow if clicked again and so on.

I created an inner class "MouseHandler".  However it does not do anything when I click the right mouse button.  Can you guys let me know what I'm doing wrong?

Thanks.


MouseHandler class starts at line 101
 
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.InputEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
 
import javax.swing.ImageIcon;
 
public class HomeOcean extends Ocean implements ActionListener {
 
	private static final long serialVersionUID = 1L;
	final boolean home = true;
	boolean horizontal;
	Coordinate coordinate;
	Ship carrier, battleship, submarine, destroyer, patrolBoat;
	final ImageIcon arrowDown = new ImageIcon("images/arrow_down.png");
	final ImageIcon arrowRight = new ImageIcon("images/arrow_right.png");
	
	public HomeOcean() {
		super();
		horizontal = true;
		for(int x=1; x<=10; x++){
			for(int y=1; y<=10; y++){
				super.grid[x][y].setRollover(home);
			}
		}
		super.grid[0][0].setOceanIcon(home);
		carrier = new AircraftCarrier();
		battleship = new Battleship();
		submarine = new Submarine();
		destroyer = new Destroyer();
		patrolBoat = new PatrolBoat();
		addMouseListener(new MouseHandler());
	}
	
	private void repaintCursor(boolean horizontal){
			if(horizontal){
				for(int x=1; x<=10; x++){
				for(int y=1; y<=10; y++){
					grid[x][y].setRolloverIcon(arrowRight);
					//grid[x][y].setRolloverIcon(grid[x][y].arrowRight);
				}
			}
	}
			else{
				for(int x=1; x<=10; x++){
					for(int y=1; y<=10; y++){
						//grid[x][y].setRolloverIcon(grid[x][y].arrowDown);
						grid[x][y].setRolloverIcon(arrowDown);
					}
				}
			}
	}
	
	public void setCoordinate(Coordinate coord){
		this.coordinate = coord;
	}
	
	public void placeShip(Ship ship){
		ship.placeShip(coordinate, horizontal, this.validPosition(coordinate, ship.size, horizontal));
	}
	
	
	public boolean validPosition(Coordinate coordinate, int size, boolean isHorizontal){
		int x = coordinate.getX();
		int y = coordinate.getY();
		if(isHorizontal){
			if(x+size>10){
				return false;
			}
			boolean valid = true;
			int position = x;
			while(valid && position <= x+size){
				if(super.grid[position][y].isFree())
					position++;
				else
					valid = false;
			}
			return valid;
		}
		else{
			if(y+size>10){
				return false;
			}
			boolean valid = true;
			int position = y;
			while(valid && position <= y+size){
				if(super.grid[x][position].isFree())
					position++;
				else
					valid = false;
			}
			return valid;
		}
	}
	
 
	public void actionPerformed(ActionEvent e) {
	}
	
	private class MouseHandler extends MouseAdapter{
		public void mousePressed(MouseEvent e){
			if(e.getModifiers() == InputEvent.BUTTON3_MASK){
				horizontal = !horizontal;
				repaintCursor(horizontal);
				repaint();
				
			}
		}
	}
 
	}

Open in new window

0
Comment
Question by:ubuntuguy
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 92

Expert Comment

by:objects
ID: 24015357
i the mousePressed() method getting called?

0
 
LVL 1

Accepted Solution

by:
ubuntuguy earned 0 total points
ID: 24015610
I fixed... the mouse handler needed to be added to the buttons not the jpanel.
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
How do I remove an object from a 3 53
jsp insert to database example 2 60
Eclipse Help Java EE 5,6,7 Documentation, why not Java EE 8 8 42
Tagging and Merging on Branch 1 40
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:

756 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question