Key Input in javaFx 2.0 - javafx-2

I am currently working on a javaFX 2.0 game and I want to know how to make it so when I type a specific key something will happen. Thank You for taking your time for reading this.

Make your root node focused and then write your logic in the setOnKeyPressed method.
Sample code:
root.setFocusTraversable(true);
root.setOnKeyPressed(new EventHandler<KeyEvent>() {
#Override
public void handle(KeyEvent kEvent) {
if (kEvent.getCode() == KeyCode.SPACE) {
System.out.println("Space Bar pressed");
}
}
});

Related

Implementation of the MvxBindableCollectionViewSource

I'm new to Mvvmcross framework and currently exploring the iOS part of it (ohh and also new to iOS development to draw a beautiful picture of my current situation ^^). I'm using the vNext version.
I've found references to implementation of UICollectionViewController (MvxTouchCollectionViewController and MvxBindableCollectionViewSource), but these classes seem to be only a skeleton for a future implementation (abstract class, missing a kind of MvxSimpleBindableCollectionViewSource at least). I haven't found a sample using this feature.
I've also found a blog post from Stuart which lets presume he's working on this part (Work In Progress - MvvmCross lists sample).
Does anybody already play with this part and know about an implementation or usage example?
I've took a look to the 10 first minutes of the xaminar mentioned by Stuart in its article and seems pretty interesting, a good starting point for me.
I've used the collection view controller in several customer apps, but don't think I've published any open source samples that use it.
In essence, the use of the collectionview is very similar to the use of the tableview and cell - which is shown in detail in: http://slodge.blogspot.co.uk/2013/01/uitableviewcell-using-xib-editor.html
In vNext, a sample controller might look like:
public class MyCollectionView : BaseCollectionView<MyCollectionViewModel>
{
private bool _needToCallViewDidLoadManually;
public HubView (MvxShowViewModelRequest request)
: base(request, new UICollectionViewFlowLayout (){
ItemSize= new System.Drawing.SizeF (100, 100),
MinimumInteritemSpacing = 20.0f,
SectionInset = new UIEdgeInsets (10,50,20,50),
ScrollDirection = UICollectionViewScrollDirection.Vertical,
})
{
if (_needToCallViewDidLoadManually) {
ViewDidLoad();
}
}
public override void ViewDidLoad ()
{
if (ShowRequest == null) {
_needToCallViewDidLoadManually = true;
return;
}
base.ViewDidLoad ();
_needToCallViewDidLoadManually = false;
var source = new CollectionViewSource(CollectionView);
this.AddBindings(
new Dictionary<object, string>()
{
{ source, "ItemsSource TheItems" }
});
CollectionView.Source = source;
CollectionView.ReloadData();
}
public class CollectionViewSource : MvxBindableCollectionViewSource
{
public CollectionViewSource (UICollectionView collectionView)
: base(collectionView, MyViewCell.Identifier)
{
collectionView.RegisterNibForCell(UINib.FromName(MyViewCell.Identifier, NSBundle.MainBundle), MyViewCell.Identifier);
}
}
}
If you are starting development now, then you might also benefit from considering the v3 branch which is just entering Beta.

org.eclipse.swt.browser.Browser does not open in Eclipse RAP application

Wonder if somebody can help me with this. I am trying to open an embedded browser in an Eclipse RAP applications. All examples I have seen look something like:
link.addSelectionListener(new SelectionAdapter() {
#Override
public void widgetSelected(SelectionEvent event) {
try {
Browser b = new Browser(parent, SWT.NONE);
b.setText("<html><body>This is Unicode HTML content from memory</body></html>");
} catch (SWTError e) {
// Error handling here
}
}
});
That doesn't do anything (visually) though. When I replace the Browser with ExternalBrowser like so:
link.addSelectionListener(new SelectionAdapter() {
#Override
public void widgetSelected(SelectionEvent event) {
try {
int browserStyle = ExternalBrowser.LOCATION_BAR;
ExternalBrowser.open( "myPage", "http://www.stackoverflow.com", browserStyle );
} catch (SWTError e) {
// Error handling here
}
}
});
It works. Although not exactly as desired.
I am using Eclipse RCP 1.4.2 on OS X 10.8.2.
Any insight is highly appreciated.
When you create a new widget, you have to trigger a re-layout to make it visible. Depending on your layout, it may be sufficient to call parent.layout(). If the parent is also contained in a layout and shrunken to its preferred size, you will have to call layout() on its parent. If unsure, layout the top-level shell.

media metadata observable map is null in java fx media

Hi guys I tried to play a song and print its metadata using java fx 2.1 api using the following code
Media media = new Media(UIandControls.class.getResource("/assets/testData/mom.mp3").toExternalForm());
MediaPlayer mediaPlayer = new MediaPlayer(media);
ObservableMap list=media.getMetadata();
System.out.print(list);
mediaPlayer.play();
for some reason getMetadata() is empty observable Map.
Output looks like this
{}
plz help me.. Thank you.
Need to call mediaPlayer.play() before retrieving metadata.
I had troubles with this at first but I found that using this worked.
mediaPlayer.getMedia().getMetadata().addListener(new MapChangeListener<String, Object>(){
#Override
public void onChanged(Change<? extends String, ?> change) {
if(change.wasAdded()){
if(change.getKey().equals("artist"))
setArtist(change.getValueAdded().toString());
else if(change.getKey().equals("title"))
setTitle(change.getValueAdded().toString());
else if(change.getKey().equals("year"))
setYear(change.getValueAdded().toString());
}
}
});
Also I read that the media data won't always be there right after initialization, therefore calling the mediaPlayer.setOnReady() method can make sure that the data is there.
Example:
mediaPlayer.setOnReady(new Runnable(){
#Override
public void run() {
setTrackLength(mediaPlayer.getMedia().getDuration().toMinutes());
mediaPlayer.currentTimeProperty().addListener(progressChangeListener);
}
});

GXT ContentPanel's header align to the right

I'm using gxt to my application.
I won't to align the ContentPanel's header ToolButtons to the left instaed to the right (which is the defualt)
there is no method such as setHorizontalAlogment.
any suggestions how to do that?
I don't see any method offered by GXT to achieve this. But I found a way to do this. Here is the code that demonstrates how you can achieve this.
public class CustomPanel extends ContentPanel {
public CustomPanel() {
super();
addListener(Events.Render, new Listener<BaseEvent>() {
#Override
public void handleEvent(final BaseEvent be) {
final HorizontalPanel panel = getWidgetPanel(getHeader());
panel.setStyleAttribute("float", "left");
}
});
}
//widgetPanel is private. It can be accessed using JSNI
private native HorizontalPanel getWidgetPanel(Component header)/*-{
return header.#com.extjs.gxt.ui.client.widget.Header::widgetPanel;
}-*/;
}
try to set the position through the style .. float: left

How to access the Keyboard in an Eclipse RCP / LWJGL app?

I am working my way through the NeHe OpenGL examples, using the LWJGL for the OpenGL binding inside an Eclipse RCP application.
My OpenGL graphics are displayed inside the RCP canvas, not in a separate window.
Lesson 07 shows how to use the keyboard. If I try to do a:
Keyboard.create();
I get an error that the (OpenGL) "Display" has not been created.
If I create an OpenGL "Display" with org.lwjgl.opengl.Display.create(), then I get a new Window.
So how do I access the Keyboard without creating a new Window?
You cannot use the Keyboard without a Display, because of how LWJGL works behind the scenes. The best way is to just use AWT events. You can write your own input class, that could go something like this.
public class Input implements KeyListener {
private boolean aDown; //is the A key down?
//Ect, for all needed keys
public void keyPressed(KeyEvent ke) {
switch (ke.getKeyCode()) {
case KeyEvent.VK_A: aDown = true; break;
//and so on for all other needed keys.
}
}
public void keyReleased(KeyEvent ke) {
switch (ke.getKeyCode()) {
case KeyEvent.VK_A: aDown = false; break;
//and so on for all other needed keys.
}
}
public void keyTyped(KeyEvent ke) {} //Do nothing
public void isADown() {return aDown;}
}

Resources