How do we define the context menu in FXML?
up vote
0
down vote
favorite
While I could define the ContextMenu without the FXML, I do not find a good way to define the context menu in FXML:
In source file:
ContextMenu contextMenu = new ContextMenu();
contextMenu.getItems().addAll(someMenuItems);
// This runs perfectly
In FXML:
<ContextMenu fx:id="contextMenu">
</ContextMenu>
// This is incorrect in fxml. The Exception of "Unable to coerce javafx.scene.control.ContextMenu to class javafx.scene.Node" is thrown.
<MenuBar fx:id="menuBar">
//... Some Menu and items could be defined here
</MenuBar>
// This is correct in fxml
I understand that MenuBar works because it extends javafx.scene.control.Control that is a subclass of javafx.scene.Node but ContextMenu does not.
So is there any way to define properties of ContextMenu similarly in FXML?
java javafx fxml
add a comment |
up vote
0
down vote
favorite
While I could define the ContextMenu without the FXML, I do not find a good way to define the context menu in FXML:
In source file:
ContextMenu contextMenu = new ContextMenu();
contextMenu.getItems().addAll(someMenuItems);
// This runs perfectly
In FXML:
<ContextMenu fx:id="contextMenu">
</ContextMenu>
// This is incorrect in fxml. The Exception of "Unable to coerce javafx.scene.control.ContextMenu to class javafx.scene.Node" is thrown.
<MenuBar fx:id="menuBar">
//... Some Menu and items could be defined here
</MenuBar>
// This is correct in fxml
I understand that MenuBar works because it extends javafx.scene.control.Control that is a subclass of javafx.scene.Node but ContextMenu does not.
So is there any way to define properties of ContextMenu similarly in FXML?
java javafx fxml
I know some people prefer coding FXML manually to using Scene Builder, but if don't know how to do certain things manually then just use Scene Builder for that part and then see how the FXML is generated.
– Gnas
Nov 20 at 14:29
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
While I could define the ContextMenu without the FXML, I do not find a good way to define the context menu in FXML:
In source file:
ContextMenu contextMenu = new ContextMenu();
contextMenu.getItems().addAll(someMenuItems);
// This runs perfectly
In FXML:
<ContextMenu fx:id="contextMenu">
</ContextMenu>
// This is incorrect in fxml. The Exception of "Unable to coerce javafx.scene.control.ContextMenu to class javafx.scene.Node" is thrown.
<MenuBar fx:id="menuBar">
//... Some Menu and items could be defined here
</MenuBar>
// This is correct in fxml
I understand that MenuBar works because it extends javafx.scene.control.Control that is a subclass of javafx.scene.Node but ContextMenu does not.
So is there any way to define properties of ContextMenu similarly in FXML?
java javafx fxml
While I could define the ContextMenu without the FXML, I do not find a good way to define the context menu in FXML:
In source file:
ContextMenu contextMenu = new ContextMenu();
contextMenu.getItems().addAll(someMenuItems);
// This runs perfectly
In FXML:
<ContextMenu fx:id="contextMenu">
</ContextMenu>
// This is incorrect in fxml. The Exception of "Unable to coerce javafx.scene.control.ContextMenu to class javafx.scene.Node" is thrown.
<MenuBar fx:id="menuBar">
//... Some Menu and items could be defined here
</MenuBar>
// This is correct in fxml
I understand that MenuBar works because it extends javafx.scene.control.Control that is a subclass of javafx.scene.Node but ContextMenu does not.
So is there any way to define properties of ContextMenu similarly in FXML?
java javafx fxml
java javafx fxml
asked Nov 20 at 13:55
Dau Zi
143
143
I know some people prefer coding FXML manually to using Scene Builder, but if don't know how to do certain things manually then just use Scene Builder for that part and then see how the FXML is generated.
– Gnas
Nov 20 at 14:29
add a comment |
I know some people prefer coding FXML manually to using Scene Builder, but if don't know how to do certain things manually then just use Scene Builder for that part and then see how the FXML is generated.
– Gnas
Nov 20 at 14:29
I know some people prefer coding FXML manually to using Scene Builder, but if don't know how to do certain things manually then just use Scene Builder for that part and then see how the FXML is generated.
– Gnas
Nov 20 at 14:29
I know some people prefer coding FXML manually to using Scene Builder, but if don't know how to do certain things manually then just use Scene Builder for that part and then see how the FXML is generated.
– Gnas
Nov 20 at 14:29
add a comment |
2 Answers
2
active
oldest
votes
up vote
1
down vote
accepted
Try to define it in the corresponding Node context (no pun intended :) through the contextMenu
property:
<TextField fx:id="tf">
<contextMenu>
<ContextMenu fx:id="cmTF">
<items>
<MenuItem text="Add"/>
<MenuItem text="Remove"/>
<MenuItem text="Enhance"/>
</items>
</ContextMenu>
</contextMenu>
</TextField>
Thanks! I don't notice that contextMenu is in fact a property inside the Node!
– Dau Zi
Nov 20 at 14:32
add a comment |
up vote
1
down vote
Try this
<BorderPane fx:id="borderPane" fx:controller="sample.Controller" xmlns:fx="http://javafx.com/fxml">
<fx:define>
<ContextMenu fx:id="contextMenu">
<items>
<MenuItem text="Menu Item"/>
</items>
</ContextMenu>
</fx:define>
</BorderPane>
public class Controller {
@FXML
private ContextMenu contextMenu;
@FXML
private BorderPane borderPane;
@FXML
private void initialize() {
borderPane.setOnContextMenuRequested(event -> {
contextMenu.show(borderPane, event.getScreenX(), event.getScreenY());
});
}
}
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53394592%2fhow-do-we-define-the-context-menu-in-fxml%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
1
down vote
accepted
Try to define it in the corresponding Node context (no pun intended :) through the contextMenu
property:
<TextField fx:id="tf">
<contextMenu>
<ContextMenu fx:id="cmTF">
<items>
<MenuItem text="Add"/>
<MenuItem text="Remove"/>
<MenuItem text="Enhance"/>
</items>
</ContextMenu>
</contextMenu>
</TextField>
Thanks! I don't notice that contextMenu is in fact a property inside the Node!
– Dau Zi
Nov 20 at 14:32
add a comment |
up vote
1
down vote
accepted
Try to define it in the corresponding Node context (no pun intended :) through the contextMenu
property:
<TextField fx:id="tf">
<contextMenu>
<ContextMenu fx:id="cmTF">
<items>
<MenuItem text="Add"/>
<MenuItem text="Remove"/>
<MenuItem text="Enhance"/>
</items>
</ContextMenu>
</contextMenu>
</TextField>
Thanks! I don't notice that contextMenu is in fact a property inside the Node!
– Dau Zi
Nov 20 at 14:32
add a comment |
up vote
1
down vote
accepted
up vote
1
down vote
accepted
Try to define it in the corresponding Node context (no pun intended :) through the contextMenu
property:
<TextField fx:id="tf">
<contextMenu>
<ContextMenu fx:id="cmTF">
<items>
<MenuItem text="Add"/>
<MenuItem text="Remove"/>
<MenuItem text="Enhance"/>
</items>
</ContextMenu>
</contextMenu>
</TextField>
Try to define it in the corresponding Node context (no pun intended :) through the contextMenu
property:
<TextField fx:id="tf">
<contextMenu>
<ContextMenu fx:id="cmTF">
<items>
<MenuItem text="Add"/>
<MenuItem text="Remove"/>
<MenuItem text="Enhance"/>
</items>
</ContextMenu>
</contextMenu>
</TextField>
answered Nov 20 at 14:25
Sergey Grinev
28.7k4104122
28.7k4104122
Thanks! I don't notice that contextMenu is in fact a property inside the Node!
– Dau Zi
Nov 20 at 14:32
add a comment |
Thanks! I don't notice that contextMenu is in fact a property inside the Node!
– Dau Zi
Nov 20 at 14:32
Thanks! I don't notice that contextMenu is in fact a property inside the Node!
– Dau Zi
Nov 20 at 14:32
Thanks! I don't notice that contextMenu is in fact a property inside the Node!
– Dau Zi
Nov 20 at 14:32
add a comment |
up vote
1
down vote
Try this
<BorderPane fx:id="borderPane" fx:controller="sample.Controller" xmlns:fx="http://javafx.com/fxml">
<fx:define>
<ContextMenu fx:id="contextMenu">
<items>
<MenuItem text="Menu Item"/>
</items>
</ContextMenu>
</fx:define>
</BorderPane>
public class Controller {
@FXML
private ContextMenu contextMenu;
@FXML
private BorderPane borderPane;
@FXML
private void initialize() {
borderPane.setOnContextMenuRequested(event -> {
contextMenu.show(borderPane, event.getScreenX(), event.getScreenY());
});
}
}
add a comment |
up vote
1
down vote
Try this
<BorderPane fx:id="borderPane" fx:controller="sample.Controller" xmlns:fx="http://javafx.com/fxml">
<fx:define>
<ContextMenu fx:id="contextMenu">
<items>
<MenuItem text="Menu Item"/>
</items>
</ContextMenu>
</fx:define>
</BorderPane>
public class Controller {
@FXML
private ContextMenu contextMenu;
@FXML
private BorderPane borderPane;
@FXML
private void initialize() {
borderPane.setOnContextMenuRequested(event -> {
contextMenu.show(borderPane, event.getScreenX(), event.getScreenY());
});
}
}
add a comment |
up vote
1
down vote
up vote
1
down vote
Try this
<BorderPane fx:id="borderPane" fx:controller="sample.Controller" xmlns:fx="http://javafx.com/fxml">
<fx:define>
<ContextMenu fx:id="contextMenu">
<items>
<MenuItem text="Menu Item"/>
</items>
</ContextMenu>
</fx:define>
</BorderPane>
public class Controller {
@FXML
private ContextMenu contextMenu;
@FXML
private BorderPane borderPane;
@FXML
private void initialize() {
borderPane.setOnContextMenuRequested(event -> {
contextMenu.show(borderPane, event.getScreenX(), event.getScreenY());
});
}
}
Try this
<BorderPane fx:id="borderPane" fx:controller="sample.Controller" xmlns:fx="http://javafx.com/fxml">
<fx:define>
<ContextMenu fx:id="contextMenu">
<items>
<MenuItem text="Menu Item"/>
</items>
</ContextMenu>
</fx:define>
</BorderPane>
public class Controller {
@FXML
private ContextMenu contextMenu;
@FXML
private BorderPane borderPane;
@FXML
private void initialize() {
borderPane.setOnContextMenuRequested(event -> {
contextMenu.show(borderPane, event.getScreenX(), event.getScreenY());
});
}
}
answered Nov 20 at 14:30
mr mcwolf
1,3292614
1,3292614
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53394592%2fhow-do-we-define-the-context-menu-in-fxml%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
I know some people prefer coding FXML manually to using Scene Builder, but if don't know how to do certain things manually then just use Scene Builder for that part and then see how the FXML is generated.
– Gnas
Nov 20 at 14:29