Click here to Skip to main content
15,885,757 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
My UI consists of 8 rounded rectangle shapes and each shape when chosen by keyboard's navigation buttons (Up and Down) should display a distinct frame.

When I tried to add key listener to the shapes..

((Component)homeShape).addKeyListener(new KeyListener(){

    @Override
    public void keyTyped(KeyEvent e) {}

    @Override
    public void keyPressed(KeyEvent e) {}

    @Override
    public void keyReleased(KeyEvent e) {}});


1. The shape needs to be typecasted to a component.

2. It is a tedious job to add keyListener to "n" number of shapes in the UI.

What I have tried:

I used mouse listener and it worked perfectly well.
@Override
	public void mouseClicked(MouseEvent e) {
	if ((e.getButton() == 1) && homeShape.contains(e.getX(),e.getY()) ) {
			System.out.println("homebutton clicked");
			//code that displays new frame in the screen
		}
}


The Mouse Listener detected the shape clicked with the boundaries (e.getX() and e.getY()).

But I want to do it with keyboard now, just with up,down,left,right arrow buttons.

1. How will the KeyListener detect which shape it is navigating through ?

2. How should I use the KeyListener for navigating through shapes ?
Posted
Updated 9-Jan-17 1:54am

1 solution

1. The KeyListener will not know which shape you are referring to, you can only capture that information via the mouse.
2. You would need to know where the focus is, i.e what shape was last clicked. You also need to decide how many pixels of a shape constitute each key press.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900