Function setonkeypress is not working with datepicker - javafx

In JavaFX the setOnKeyPressed event is not hitting in datepicker field through the program whereas for other fields its working properly.
It's not hitting,
dateOfBirth.setOnKeyPressed(new EventHandler<KeyEvent>(){
#Override
public void handle(KeyEvent event) {
if (event.getCode() == KeyCode.TAB) {
event.consume();
nationality.requestFocus();
}
}
});
I wanna get to know the reason and solution.

Related

TreeCell key events not firing

I have TreeView control with CellFactory:
tVMain.setCellFactory(new Callback<TreeView<ITVItem>, TreeCell<ITVItem>>()
{
#Override
public TreeCell<ITVItem> call(TreeView<ITVItem> p)
{
return new TextFieldTreeCellImpl();
}
});
TextFieldTreeCellImpl:
private static class TextFieldTreeCellImpl extends TextFieldTreeCell<ITVItem>
{
private TextField textField;
public TextFieldTreeCellImpl()
{
this.setOnKeyPressed((e) -> {
startEdit();
});
}
public void startEdit()
{
super.startEdit();
if (textField == null)
{
textField = new TextField();
}
textField.setText(getString());
setText(null);
setGraphic(textField);
textField.selectAll();
textField.requestFocus();
}
#Override
public void cancelEdit()
{
super.cancelEdit();
setText(((ITVItem) getItem()).toString());
setGraphic(getTreeItem().getGraphic());
}
}
The problem is that the OnKeyPressed event never fires. I tried with OnKeyReleased but the result is the same - no event.
TextFieldTreeCellImpl also has OnMouseReleased events (not shown in code for brevity) which do work fine.
LAST INFO: When TextFieldTreeCellImpl is in edit mode (startEdit() has been called and textField is visible) the events are triggered. But when the editing is cancelled and the textField removed, events are not firing again.
How can I get the key events to fire in non editing mode?
EDIT:
In response to the comment from James_D: I also tried adding
this.addEventFilter(KeyEvent.KEY_PRESSED, e -> {
//SOMETHING
});
instead of OnKeyReleased but that still does not work.

JavaFX contextmenu not hiding

I'm trying to show a context menu only when there is something selected in the list view.
So I called hide in its on showing event. However, this is not working. The context menu still shows up. Is this a bug, or not its intended use? Because JavaFX api seems to suggest hide() is suppose to do this.
Anyway this is the code.
ContextMenu menu = new ContextMenu();
menu.setOnShowing(new EventHandler<WindowEvent>() {
#Override
public void handle(final WindowEvent event) {
menu.hide();
}
});
It will probably work if you do
public void handle(final WindowEvent event) {
Platform.runLater(new Runnable() {
#Override
public void run() {
menu.hide();
}
});
}
but that really seems like a horrible hack.
Why not just set the context menu only if something is selected?
final ListView<T> listView = ... ;
final ContextMenu menu = new ContextMenu();
listView.getSelectionModel().selectedItemProperty().addListener(new ChangeListener<T>() {
#Override
public void changed(ObservableValue<? extends T> obs, T oldValue, T newValue) {
if (newValue == null) {
listView.setContextMenu(null);
} else {
listView.setContextMenu(menu);
}
}
});
(obviously replace T with whatever type your ListView is displaying).
private ContextMenu menu; private MenuItem deleteItem;
table.setOnContextMenuRequested(new EventHandler<ContextMenuEvent>() {
#Override
public void handle(ContextMenuEvent event) {
if (table.getSelectionModel().getSelectedIndex() != -1) {
deleteItem.setVisible(true);
deleteItem.setText("delete: " + table.getSelectionModel().getSelectedItem().getName());
contextMenu.show(table, event.getScreenX(), event.getScreenY());
} else {
deleteItem.setVisible(false);
}
event.consume();
}
});
primaryStage.getScene().addEventHandler(MouseEvent.MOUSE_ENTERED, new EventHandler<MouseEvent>() {
#Override
public void handle(MouseEvent event) { menu.hide(); }});

How Stop Sound when back or home button is pressed

My question is how to stop the media player when the user presses the back or the home button? I really need help..Can some one please give a code and tell me where to incorporate it in the activity.
This is my code:
MY JAVA CODE:
public class fbactivity extends Activity {
private MediaPlayer myMediaPlayer;
private MediaPlayer my1MediaPlayer;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
myMediaPlayer = MediaPlayer.create(fbactivity.this, R.raw.milyonlarcataraftaryanyana);
//Button related to play btn
Button myButtonOne = (Button) findViewById(R.id.magnumsilah);
myButtonOne.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
myMediaPlayer.start();
}
});
//Button related to stop btn
Button myButtonTwo = (Button) findViewById(R.id.Button03);
myButtonTwo.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
myMediaPlayer.pause();
}
});
my1MediaPlayer = MediaPlayer.create(fbactivity.this, R.raw.sevmeksenideligibiyurekister);
//Button related to play btn
Button myButtonOne1 = (Button) findViewById(R.id.Button02);
myButtonOne1.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
my1MediaPlayer.start();
}
});
//Button related to stop btn
Button myButtonTwo2 = (Button) findViewById(R.id.Button01);
myButtonTwo2.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
my1MediaPlayer.pause();
}
});
Button butongeridon=(Button) findViewById(R.id.geridon);
butongeridon.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent i=new Intent(fbactivity.this,MainActivity.class); // the names of activity as per you program.
startActivity(i);
finish();
}
});
}
}
You inflate two buttons, one from R.id.magnumsilah, and another from R.id.Button03. However, the ClickListener is bound to only one of them, and no listener is bound to the back button.
It may well be worth it to be more thorough in you research before posting questions. Just a thought.

setOnMouseEntered and setOnMouseExited foreverloop

I use a PopOver from controlsfx to display detail-data from a TableCell. I use a tableCellFactory to create individual PopOvers. I have a problem with this code:
public class PopupTableCell extends TableCell {
#Override
protected void updateItem(final Object item, boolean empty) {
super.updateItem(item, empty);
if (empty){
setText(null);
} //if
else {
setText(item.toString());
// PopUp Anzeigen.
final PopOver PopUp = new PopOver();
PopUp.setContentNode(new Label(item.toString()));
setOnMouseEntered(new EventHandler<MouseEvent>() {
#Override
public void handle(MouseEvent mouseEvent) {
Point MouseLocation = MouseInfo.getPointerInfo().getLocation();
PopUp.show(PopupTableCell.this, MouseLocation.getX(), MouseLocation.getY());
System.out.println("entered");
}
});
setOnMouseExited(new EventHandler<MouseEvent>() {
#Override
public void handle(MouseEvent mouseEvent) {
System.out.println("exited");
PopUp.hide();
}
});
}
}
When I place the cursor over a cell, it begins firing the entered and the exited events forever (even with a non moving mouse cursor.
How can I achieve the wanted behavior:
Mouse inside table cell -> popover.show()
Mouse leaves table cell -> popover.hide()
This is how I solved the same problem for me.
owner.setOnMouseMoved(evt -> {
if (owner.getLayoutBounds().contains(evt.getX(), evt.getY())) {
if (!popOver.isShowing()) {
popOver.show(owner);
}
}
});
owner.setOnMouseExited(evt -> {
if (owner.getLayoutBounds().contains(evt.getX(), evt.getY())) {
if (!popOver.isShowing())
popOver.show(owner);
} else {
popOver.hide();
}
});

Set Focus on FlowPane in Javafx

i tried
public void ChangeFocus(Button browse, final FlowPane mFlowPane)
{
browse.addEventHandler(KeyEvent.KEY_PRESSED, new EventHandler<KeyEvent>() {
#Override
public void handle(KeyEvent event)
{
if (event.getCode() == KeyCode.TAB)
{
System.out.println("TAB pressed");
mFlowPane.requestFocus();
event.consume(); // do nothing
}
}
});
}
in above code i set one ImageView inside Flowpane but when i press TAB button on my browse button i can't get focus on imageview how can i solve it?
FlowPane is not focustraversable by default. Call mFlowPane.setFocusTraversable(true) to make it part of the traverse story
some more detail to solve it. instead of focusing the flowpane you need to set the focus on whats in the flowpane, for example a textfield:
flowPane.setOnMouseClicked(new EventHandler<MouseEvent>()
{
public void handle(MouseEvent mouseEvent)
{
textfield.requestFocus();
}
});

Resources