4 Replies - 2688 Views - Last Post: 30 November 2008 - 08:33 PM Rate Topic: -----

#1 tiiger   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 29-November 08

Key events not received when HContainer is added to HScene

Post icon  Posted 29 November 2008 - 12:11 AM

I am missing something simple here.. Please help...

I am creating an container with some buttons in them as follows,

HContainer Code:
		mRecListButton	 = new HTextButton("List", curX, curY);
		add(mRecListButton);
		
		// Setup a recording
		curY += V3Button.mHeight + gap;
		mRecButton = new HTextButton("Record", curX , curY);
		add(mRecButton);
		
		// Shows the list of scheduled recordings
		curY += V3Button.mHeight + gap;
		mScheduleRecButton = new HTextButton("Schedule", curX , curY);
		add(mScheduleRecButton);
		
		// Shows the list of Series Recordings
		curY += V3Button.mHeight + gap;
		mSeriesRecButton   = new HTextButton("Series", curX, curY);
		add(mSeriesRecButton);
		
		mCurrentSelectedButton  = LISTVALUE;
		
		mRecListButton.setFocus();


Then I am creating a scene,

HScene Code:
		HSceneFactory factory = HSceneFactory.getInstance();
		HSceneTemplate sceneTemplate = new HSceneTemplate();

		sceneTemplate.setPreference(HSceneTemplate.
						SCENE_PIXEL_DIMENSION,
						new Dimension(640, 400),
						HSceneTemplate.REQUIRED);

		sceneTemplate.setPreference(HSceneTemplate.
						SCENE_SCREEN_LOCATION,
						new HScreenPoint((float)0,(float)0),
						HSceneTemplate.REQUIRED);

		mScene = factory.getBestScene(sceneTemplate);
		mScene.requestFocus();
		mScene.addFocusListener(this);


Then, I add the above container to the mscene as follows,

HScene adds the HContainer object:
		mCurrentContainer = myContainer;
		mScene.add(mCurrentContainer);	// Keys Presses do not work if left uncommented
		mScene.setVisible(true);
		mScene.requestFocus();
		mScene.addKeyListener( mCurrentContainer);
		mCurrentContainer.setVisible(true);
		mCurrentContainer.requestFocus();


I see the buttons correctly and the "List" button is highlighted correctly. However, I do not get any key presses.

Now, if I comment out "mScene.add()" as follows,
		mCurrentContainer = myContainer;
//		mScene.add(mCurrentContainer);	// Now the key presses works correctly. 
		mScene.setVisible(true);
		mScene.requestFocus();
		mScene.addKeyListener( mCurrentContainer);
		mCurrentContainer.setVisible(true);
		mCurrentContainer.requestFocus();


The key presses works correctly. However, the buttons are not displayed now.

What am I missing?

Thanks

*Welcome at DIC Edited to add the [ /code] tags so it is readable In the future
:code:

This post has been edited by pbl: 29 November 2008 - 10:53 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Key events not received when HContainer is added to HScene

#2 pbl   User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8379
  • View blog
  • Posts: 31,956
  • Joined: 06-March 08

Re: Key events not received when HContainer is added to HScene

Posted 29 November 2008 - 10:59 PM

Not easy to answer without seing all your code
No idea what is an HTextButton. Does it have an ActionListener ?
No ActionListener no reason for the button to react.

I guess that you only play with KeyboardListener
The behaviour changes when you put a container into another one if I understand well.

So I guess that the component that has the focus (so react to the keyboard) is not the one you think
Was This Post Helpful? 0
  • +
  • -

#3 tiiger   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 29-November 08

Re: Key events not received when HContainer is added to HScene

Posted 29 November 2008 - 11:14 PM

Thanks pbl. Below is the code,

public class V3Button extends HTextButton
{

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	protected static int mWidth = 120;
	protected static int mHeight= 40;
	
	protected V3Button(String name, int x, int y)
	{
		super(name, x, y, mWidth, mHeight, new Font("Courier", Font.BOLD, 18), 
				Color.white, Color.gray , null);
		setBackgroundMode(HVisible.BACKGROUND_FILL);
	}
	
	public void setFocus()
	{	
		setForeground(Color.black);
		//repaint();
	}
	
	public void removeFocus()
	{
		setForeground(Color.white);
		//repaint();
	}
}

public class V3Container extends HContainer implements FocusListener, 
													   HItemListener, 
													   HActionListener,
													   HAdjustmentListener, 
													   KeyListener,
													   ControllerListener
{
	
V3Container(String title)
	{
		super(100, 100, 320, 300);
		mTitle = title;
		
	}
	
	
	public String getTitle()
	{
		return mTitle;
	}
	
	public void focusGained(FocusEvent e) {
		// TODO Auto-generated method stub
		
	}

..etc...etc...

}


public class RecordPage extends V3Container implements Xlet
{
	public RecordPage()
	{
	  super("Recording Screen");
		makeRecordPage();
	}

	private void makeRecordPage()
	{
		int gap = 3;
		int curX, curY;
		
		// Shows the list of recordings
		this.setFont(new Font("Courier", Font.BOLD, 18));
		

		curX = 10;
		curY = 10;
		mRecListButton	 = new V3Button("List", curX, curY);
		add(mRecListButton);
		
		// Setup a recording
		curY += V3Button.mHeight + gap;
		mRecButton = new V3Button("Record", curX , curY);
		add(mRecButton);
		
		// Shows the list of scheduled recordings
		curY += V3Button.mHeight + gap;
		mScheduleRecButton = new V3Button("Schedule", curX , curY);
		add(mScheduleRecButton);
		
		// Shows the list of Series Recordings
		curY += V3Button.mHeight + gap;
		mSeriesRecButton   = new V3Button("Series", curX, curY);
		add(mSeriesRecButton);
		
		mCurrentSelectedButton  = LISTVALUE;
		
		mRecListButton.setFocus();
	}

	public void keyPressed(KeyEvent arg0) 
	{
		char c	= arg0.getKeyChar();
		int code  = arg0.getKeyCode();

		System.out.println("RecordPage.keyPressed c=" + c + " code=" + code);
				  }

	public void startXlet() throws XletStateChangeException {
		// TODO Auto-generated method stub
		HSceneFactory factory = HSceneFactory.getInstance();
		HSceneTemplate sceneTemplate = new HSceneTemplate();

		sceneTemplate.setPreference(HSceneTemplate.
						SCENE_PIXEL_DIMENSION,
						new Dimension(640, 400),
						HSceneTemplate.REQUIRED);

		sceneTemplate.setPreference(HSceneTemplate.
						SCENE_SCREEN_LOCATION,
						new HScreenPoint((float)0,(float)0),
						HSceneTemplate.REQUIRED);

		mScene = factory.getBestScene(sceneTemplate);
		//mScene.requestFocus();
		mScene.addFocusListener(this);
		//mScene.addKeyListener(this);
		
		mRecPage = new RecordPage();		
		mScene.add(mRecPage);
		mScene.setVisible(true);
		//mScene.requestFocus();
		
		mRecPage.setVisible(true);		
		mRecPage.addKeyListener(this);
		mRecPage.requestFocus();
	}

}





If mScene gets the focus, I do not get any key press events. I would like to get the key press events in RecordPage so I can traverse between the buttons.

Let me know. Thanks again.
Was This Post Helpful? 0
  • +
  • -

#4 pbl   User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8379
  • View blog
  • Posts: 31,956
  • Joined: 06-March 08

Re: Key events not received when HContainer is added to HScene

Posted 29 November 2008 - 11:20 PM

View Posttiiger, on 29 Nov, 2008 - 10:14 PM, said:

If mScene gets the focus, I do not get any key press events. I would like to get the key press events in RecordPage so I can traverse between the buttons.

Buttons have ActionListener why not using them ?
Not a good architecture at all to use KeyListener to react to button event (actually you don't have button event)
You will depend on which component the user clicked with the mouse the last time... really bad design
Was This Post Helpful? 0
  • +
  • -

#5 tiiger   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 29-November 08

Re: Key events not received when HContainer is added to HScene

Posted 30 November 2008 - 08:33 PM

Good point pbl.

Wouldn't this work? I am planning to set the focus to the buttons myself using the following code. Basically, there are four buttons, I just want to navigate thru each of these buttons. If they select a specific button, I bring up another container and that container's keylistener is now active. This keeps code a little simple on my end. More interested in the end results of the working code.

Let me know what you think. Thanks.

	public void keyPressed(KeyEvent arg0) 
	{
		char c	= arg0.getKeyChar();
		int code  = arg0.getKeyCode();

		System.out.println("RecordPage.keyPressed c=" + c + " code=" + code);
						
		switch(code)
		{
			case HRcEvent.VK_RIGHT:
			{
				System.out.println("RecordPage.keyPressed VK_ENTER");
				ButtonPressed(mCurrentSelectedButton ); // bring up another container...
				break;
			}
			case HRcEvent.VK_DOWN:
				System.out.println("RecordPage.keyPressed VK_DOWN - " + mCurrentSelectedButton);
				removeFocus(mCurrentSelectedButton);
				mCurrentSelectedButton++;
				if (mCurrentSelectedButton >= INVALID_BUTTON )
				{
					mCurrentSelectedButton = LISTVALUE;  
				}
				setFocus(mCurrentSelectedButton);
				break;

			case HRcEvent.VK_UP:
			{
				System.out.println("RecordPage.keyPressed VK_UP");
				removeFocus(mCurrentSelectedButton);
				mCurrentSelectedButton--;
				if (mCurrentSelectedButton <= 0)
				{
					mCurrentSelectedButton = LISTVALUE;  
				}
				setFocus(mCurrentSelectedButton);
				break;
			}
		}
	}	


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1