Right click on toggle button - javafx-2

I would like to run a stage by right mouse click on a toggle button,
ToggleButton tb = new ToggleButton();
what OnMouse method should I use?

try this for onPressed:
tb.setOnMousePressed(new EventHandler<MouseEvent>() {
#Override
public void handle(MouseEvent mouseEvent) {
if (mouseEvent.isSecondaryButtonDown()){
System.out.println("hi");
}
}
});
for onReleased:
tb.setOnMouseReleased(new EventHandler<MouseEvent>() {
#Override
public void handle(MouseEvent mouseEvent) {
if (mouseEvent.getButton().equals(MouseButton.SECONDARY)) {
System.out.println("hi");
}
}
});

Related

JavaFX - How to listen to tab down button when mouse clicked on tabpane?

How can I had an onMouseClicked listener on the Tab Down Button in a TabPane ?
We can add a MouseEvent handler to the TabPane and find the arrow if clicked there:
tabPane.addEventHandler(MouseEvent.MOUSE_CLICKED, new EventHandler<MouseEvent> () {
#Override
public void handle(MouseEvent mouseEvent) {
EventTarget eventTarget = mouseEvent.getTarget();
if (eventTarget instanceof StackPane) {
StackPane stackPane = (StackPane) eventTarget;
ObservableList<String> styleClasses = stackPane.getStyleClass();
for (String styleClass : styleClasses) {
if ("arrow".equals(styleClass) == true) {
//TODO
System.out.println("arrowEvent");
}
}
}
}
});

How to show context menu in FlowPane body

I have a FlowPane which use used as stage for many BorderPane components. I created Context menu for the BorderPane components. I want to add second context menu also for the FlowPane but it turns out that I now have two context menus when I click on BorderPane. I need a way to exclude the second context menu from the BorderPane.
FlowPane flow;
public ScrollPane infrastructurePane()
{
flow = new FlowPane();
flow.setPadding(new Insets(5, 5, 5, 5));
flow.setVgap(15);
flow.setHgap(15);
flow.setAlignment(Pos.CENTER);
ScrollPane scroll = new ScrollPane();
scroll.setStyle("-fx-background-color:transparent;");
Rectangle2D primaryScreenBounds = Screen.getPrimary().getVisualBounds();
scroll.setPrefSize(primaryScreenBounds.getWidth(), primaryScreenBounds.getHeight());
scroll.setHbarPolicy(ScrollPane.ScrollBarPolicy.AS_NEEDED); // Horizontal scroll bar
scroll.setVbarPolicy(ScrollPane.ScrollBarPolicy.AS_NEEDED); // Vertical scroll bar
scroll.setContent(flow);
scroll.viewportBoundsProperty().addListener(new ChangeListener<Bounds>()
{
#Override
public void changed(ObservableValue<? extends Bounds> ov, Bounds oldBounds, Bounds bounds)
{
flow.setPrefWidth(bounds.getWidth());
flow.setPrefHeight(bounds.getHeight());
}
});
//flow.setPrefWrapLength(170); // preferred width allows for two columns
flow.setStyle("-fx-background-color: white;");
for (int i = 0; i < 28; i++)
{
flow.getChildren().add(generatePanel(flow));
}
scroll = makeTabContextMenu(scroll);
String cssURL = "/com/dx57dc/css/ButtonsDemo.css";
String css = this.getClass().getResource(cssURL).toExternalForm();
flow.getStylesheets().add(css);
return scroll;
}
public BorderPane generatePanel(FlowPane flow)
{
HBox thb = new HBox();
thb.setPadding(new Insets(10, 10, 10, 10));
thb.setStyle("-fx-background-color: #006699;");
HBox bhb = new HBox();
bhb.setPadding(new Insets(10, 10, 10, 10));
bhb.setStyle("-fx-background-color: #B0B0B0;");
DropShadow ds = new DropShadow();
ds.setOffsetY(3.0);
ds.setOffsetX(3.0);
ds.setColor(Color.GRAY);
BorderPane bp = new BorderPane();
bp.setEffect(ds);
bp.setCache(true);
bp.setPrefSize(320, 180);
bp.setMaxSize(320, 180);
bp.setId("app");
bp.setStyle("-fx-background-color: linear-gradient(to bottom, #f2f2f2, #d4d4d4);"
+ " -fx-border: 2px solid; -fx-border-color: white;");
bp.setTop(thb);
bp.setBottom(bhb);
ScrollPane sp = new ScrollPane();
bp = panelContextMenu(bp);
bp = mouseOver(bp);
return bp;
}
public BorderPane mouseOver(final BorderPane bp)
{
bp.setOnMouseEntered(new EventHandler<MouseEvent>()
{
#Override
public void handle(MouseEvent t)
{
bp.setStyle("-fx-border: 2px solid; -fx-border-color: black;");
}
});
bp.setOnMouseExited(new EventHandler<MouseEvent>()
{
#Override
public void handle(MouseEvent t)
{
bp.setStyle("-fx-border: 2px solid; -fx-border-color: white;");
}
});
bp.setOnMouseClicked(new EventHandler<MouseEvent>()
{
#Override
public void handle(MouseEvent me)
{
if (me.getButton().equals(MouseButton.PRIMARY) && me.getClickCount() % 1 == 0)
{
bp.setPrefSize(480, 280);
bp.setMaxSize(480, 280);
}
if (me.getButton().equals(MouseButton.PRIMARY) && me.getClickCount() % 2 == 0)
{
bp.setPrefSize(320, 180);
bp.setMaxSize(320, 180);
}
}
});
return bp;
}
private ScrollPane makeTabContextMenu(ScrollPane scroll)
{
ContextMenu contextMenu = new ContextMenu();
MenuItem item1 = new MenuItem("New");
item1.setOnAction(new EventHandler<ActionEvent>()
{
#Override
public void handle(ActionEvent e)
{
System.out.println("New");
flow.getChildren().add(generatePanel(flow));
}
});
// Rename
MenuItem rename = new MenuItem("Rename");
final TextField textField = new TextField();
rename.setOnAction(new EventHandler<ActionEvent>()
{
#Override
public void handle(ActionEvent e)
{
System.out.println("Rename");
//textField.setText(label.getText());
//tab.setGraphic(textField);
textField.selectAll();
textField.requestFocus();
}
});
textField.setOnAction(new EventHandler<ActionEvent>()
{
#Override
public void handle(ActionEvent event)
{
//label.setText(textField.getText());
//tab.setGraphic(label);
}
});
textField.focusedProperty().addListener(new ChangeListener<Boolean>()
{
#Override
public void changed(ObservableValue<? extends Boolean> observable,
Boolean oldValue, Boolean newValue)
{
if (!newValue)
{
//label.setText(textField.getText());
//tab.setGraphic(label);
}
}
});
// Orientation
Menu tabOrientation = new Menu("Orientation");
// Orientation sub menu - Top, Left, Right, Bottom
MenuItem tabOrientationSubTop = new MenuItem("Top");
tabOrientationSubTop.setOnAction(new EventHandler<ActionEvent>()
{
#Override
public void handle(ActionEvent e)
{
//tabPane.setSide(Side.TOP);
}
});
MenuItem tabOrientationSubLeft = new MenuItem("Left");
tabOrientationSubLeft.setOnAction(new EventHandler<ActionEvent>()
{
#Override
public void handle(ActionEvent e)
{
//tabPane.setSide(Side.LEFT);
}
});
MenuItem tabOrientationSubRight = new MenuItem("Right");
tabOrientationSubRight.setOnAction(new EventHandler<ActionEvent>()
{
#Override
public void handle(ActionEvent e)
{
//tabPane.setSide(Side.RIGHT);
}
});
MenuItem tabOrientationSubBottom = new MenuItem("Bottom");
tabOrientationSubBottom.setOnAction(new EventHandler<ActionEvent>()
{
#Override
public void handle(ActionEvent e)
{
//tabPane.setSide(Side.BOTTOM);
}
});
tabOrientation.getItems().addAll(tabOrientationSubTop, tabOrientationSubLeft, tabOrientationSubRight, tabOrientationSubBottom);
MenuItem item3 = new MenuItem("Close Tab");
item3.setOnAction(new EventHandler<ActionEvent>()
{
#Override
public void handle(ActionEvent e)
{
System.out.println("Close Tab");
// Close the Tab and remove it from the TabPane
//tabPane.getTabs().remove(tab);
// If there are no tabs into the TabPane fill all empty space by resizing the near components
// if (tabPane.getTabs().size() == 0)
// {
// ActionTabs.getMainPane().setManaged(false); // Exclude the panel to fill the empty space
// }
}
});
MenuItem item4 = new MenuItem("Close All Panels");
item4.setOnAction(new EventHandler<ActionEvent>()
{
#Override
public void handle(ActionEvent e)
{
System.out.println("Close All Panels");
flow.getChildren().clear();
}
});
contextMenu.getItems().addAll(item1, rename, tabOrientation, item3, item4);
scroll.setContextMenu(contextMenu);
return scroll;
}
public BorderPane panelContextMenu(final BorderPane bp)
{
final ContextMenu contextMenu = new ContextMenu();
MenuItem item1 = new MenuItem("About");
item1.setOnAction(new EventHandler<ActionEvent>()
{
#Override
public void handle(ActionEvent e)
{
System.out.println("About");
}
});
MenuItem item2 = new MenuItem("Preferences");
item2.setOnAction(new EventHandler<ActionEvent>()
{
#Override
public void handle(ActionEvent e)
{
System.out.println("Preferences");
}
});
MenuItem item3 = new MenuItem("Close");
item3.setOnAction(new EventHandler<ActionEvent>()
{
#Override
public void handle(ActionEvent e)
{
flow.getChildren().remove(bp);
}
});
contextMenu.getItems().addAll(item1, item2, item3);
bp.setOnContextMenuRequested(new EventHandler<ContextMenuEvent>()
{
#Override
public void handle(ContextMenuEvent event)
{
contextMenu.show(bp, event.getScreenX(), event.getScreenY());
}
});
bp.addEventHandler(MouseEvent.MOUSE_PRESSED, new EventHandler<MouseEvent>()
{
#Override
public void handle(MouseEvent event)
{
contextMenu.hide();
}
});
return bp;
}
Is there a way to show makeTabContextMenu() only visible for the FlowPane body?
Have you tried to set the event consumed?
#Override
public void handle(MouseEvent t)
{
...
t.consume()
...
}

Implement drag-and-drop like in Scene Builder

I'm building an application in JavaFx 2.2 which consist in a splitpane with a panel of component on the left side and a working sheet on the right side. Basically what i would like to do is a simple wysiwyg editor where you drag component from the left to the right, then arrange them on the right side.
I've spent the last couple of days trying to implement the same drag-and-drop feature that has SceneBuilder, without luck..
Following the sample at http://docs.oracle.com/javafx/2/drag_drop/HelloDragAndDrop.java.html i've managed to get a drag-and-drop working but i can't find any way to change the default file icon appearing when you are dragging (and replace it with a snapshot of the component i'm dragging) and how to show the forbidden icon when you are over something you can't dropped on.
Any help (could be advice, code snippet, sample or else) would be greatly appreciated :)
Thanks !
[UPDATE]
Finally managed it myself:
/* The 'sceneRoot' object is the root Node of the scene graph
* stage.setScene(new Scene(sceneRoot, 1280, 1024));
*/
private ImageView dragImageView = new ImageView();
private Node dragItem;
_
rightPane.setOnMouseDragEntered(new EventHandler<MouseDragEvent>() {
public void handle(MouseDragEvent e) {
rightPane.setStyle("-fx-border-color:red;-fx-border-width:2;-fx-border-style:solid;");
e.consume();
}
});
rightPane.setOnMouseDragExited(new EventHandler<MouseDragEvent>() {
public void handle(MouseDragEvent e) {
rightPane.setStyle("-fx-border-style:none;");
e.consume();
}
});
rightPane.setOnMouseDragReleased(new EventHandler<MouseDragEvent>() {
public void handle(MouseDragEvent e) {
//TODO: add new instance of dragItem to rightPane
e.consume();
}
});
_
private void addGesture(final Node node) {
node.setOnDragDetected(new EventHandler<MouseEvent>() {
public void handle(MouseEvent e) {
SnapshotParameters snapParams = new SnapshotParameters();
snapParams.setFill(Color.TRANSPARENT);
dragImageView.setImage(node.snapshot(snapParams, null));
sceneRoot.getChildren().add(dragImageView);
dragImageView.startFullDrag();
e.consume();
}
});
node.setOnMouseDragged(new EventHandler<MouseEvent>() {
public void handle(MouseEvent e) {
Point2D localPoint = sceneRoot.sceneToLocal(new Point2D(e.getSceneX(), e.getSceneY()));
dragImageView.relocate(
(int)(localPoint.getX() - dragImageView.getBoundsInLocal().getWidth() / 2),
(int)(localPoint.getY() - dragImageView.getBoundsInLocal().getHeight() / 2)
);
e.consume();
}
});
node.setOnMouseEntered(new EventHandler<MouseEvent>() {
public void handle(MouseEvent e) {
node.setCursor(Cursor.HAND);
}
});
node.setOnMousePressed(new EventHandler<MouseEvent>() {
public void handle(MouseEvent e) {
dragItem = node;
dragImageView.setMouseTransparent(true);
node.setMouseTransparent(true);
node.setCursor(Cursor.CLOSED_HAND);
}
});
node.setOnMouseReleased(new EventHandler<MouseEvent>() {
public void handle(MouseEvent e) {
dragItem = null;
dragImageView.setMouseTransparent(false);
node.setMouseTransparent(false);
node.setCursor(Cursor.DEFAULT);
sceneRoot.getChildren().remove(dragImageView);
}
});
}
Maybe late, but with the setDragView option, is more simpple by now :)
// Cursor Display for Drag&Drop
source.setOnMouseEntered(e -> source.setCursor(Cursor.OPEN_HAND));
source.setOnMousePressed(e -> source.setCursor(Cursor.CLOSED_HAND));
source.setOnMouseReleased(e -> source.setCursor(Cursor.DEFAULT));
// Manage drag
source.setOnDragDetected(event -> {
/* drag was detected, start a drag-and-drop gesture*/
Dragboard db = source.startDragAndDrop(TransferMode.MOVE);
// Visual during drag
SnapshotParameters snapshotParameters = new SnapshotParameters();
snapshotParameters.setFill(Color.TRANSPARENT);
db.setDragView(source.snapshot(snapshotParameters, null));
/* Put a string on a dragboard */
ClipboardContent content = new ClipboardContent();
content.putString(source.getText());
db.setContent(content);
event.consume();
});

Detecting the Secondary mouse button in the text area and table view using javafx

private void gotoRightClick() {
textArea.setOnMouseClicked(new EventHandler<MouseEvent>(){
#Override
public void handle(MouseEvent event) {
if(event.isSecondaryButtonDown())
{
System.out.println("right click pressed in text area");
}
}
});
tableView.setOnMouseClicked(new EventHandler<MouseEvent>(){
#Override
public void handle(MouseEvent e) {
if(e.isSecondaryButtonDown())
{
System.out.println("right click pressed in table view");
}
}
});
}
None of the event is detecting can any one help me?
Thanks Advance
Try this:
this.setOnMouseClicked(new EventHandler<MouseEvent>() {
#Override
public void handle(MouseEvent event) {
if (event.getButton().equals(MouseButton.SECONDARY)) {
//do something
}
}
});

ContextMenu (Popup) always on

I have a problem with a Context menu in JavaFx 2:it never disappers when I left click on the graph of the JFXPanel
Does anybody knows how to solve this problem?
Thanks
Here is my code
final ContextMenu cm = new ContextMenu();
MenuItem chartItem1 = new MenuItem("Chart Settings");
cm.getItems().add(chartItem1);
getScene().setOnMouseReleased(new EventHandler<MouseEvent>() {
#Override
public void handle(MouseEvent mouseEvent) {
if(cm.isShowing()){
cm.hide();
}
if(mouseEvent.getButton() == MouseButton.SECONDARY)
{
cm.show(getScene().getRoot(), mouseEvent.getScreenX(), mouseEvent.getScreenY());
}
}
});
chartItem1.setOnAction(new EventHandler<ActionEvent>() {
#Override
public void handle(ActionEvent t) {
dialogs.ChartFormat cs = new dialogs.ChartFormat(null, true);
cs.setLocationRelativeTo(null);
cs.setVisible(true);
}
});
Reproduced the described behavior. Don't know the reason but you can use ContextMenu#hide():
final ContextMenu cm = new ContextMenu();
MenuItem menuItem = new MenuItem("Item 1");
menuItem.addEventHandler(EventType.ROOT, new EventHandler<Event>() {
#Override
public void handle(Event t) {
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
JPanel messagePane = new JPanel();
messagePane.add(new JLabel("label"));
JDialog jDialog = new JDialog();
jDialog.getContentPane().add(messagePane);
jDialog.pack();
jDialog.setVisible(true);
}
});
}
});
cm.getItems().add(menuItem);
scene.setOnMouseReleased(new EventHandler<MouseEvent>() {
#Override
public void handle(MouseEvent mouseEvent) {
// if(cm.isShowing())
cm.hide();
if (mouseEvent.getButton() == MouseButton.SECONDARY) {
cm.show(lineChart, mouseEvent.getScreenX(), mouseEvent.getScreenY());
}
}
});
Also you can check out these links:
http://pixelduke.wordpress.com/2011/12/11/popupmenu-in-javafx/
http://javafx-jira.kenai.com/browse/RT-17853
http://javafx-jira.kenai.com/browse/RT-14899
Adding sample code to your question would be more descriptive.

Resources