I'm trying to implement a reusable method for displaying a node as a lightweight dialog. The node is added to the top of a StackPane while the background is then blurred. There are 2 problems:
1) Controls in the background node of the stackpane are still able to receive focus.
2) How do I give focus to the top-level node. I know there is a requestFocus method on Node, but I need to give it to a control nested within the node. Since this is intended to be a reusable method, I can't reference the controls directly.
(if I can sidestep the whole problem by finding an existing implementation, that would be best, but I haven't found a 3rd party solution yet)
Thanks
For:
1) 3 alternative suggestions,
a- Add a key event handler to the dialogbox pane to catch the Tab key pressings
dialogbox.addEventFilter(KeyEvent.KEY_PRESSED, new EventHandler<KeyEvent>() {
#Override
public void handle(javafx.scene.input.KeyEvent event) {
if (event.getCode() == KeyCode.TAB) {
System.out.println("TAB pressed");
event.consume(); // do nothing
}
}
});
b- Temporarily disable all children of the main StackPane except the lastly added dialogbox child.
// Assuming dialogbox is at last in the children list
for(int i=0; i<mainPane.getChildren().size()-1; i++){
// Disabling will propagate to sub children recursively
mainPane.getChildren().get(i).setDisable(true);
}
c- Same as b but manually disable focusing to controls by node.setFocusTraversable(false) (spot kick). Surely this will not be your choice..
2) Tell the dialogbox which node should take the focus after the dialog is shown (through constructor or setter method).
// init dialogbox and show it then
dialogbox.setFocusTo(node_in_dialogbox);
Related
In my app, one scene having the popup dialog which consists of some fields and buttons. If you click on the button then I want to dismiss the popup dialog as well as update the some fields in the scene. Indirectly I want to refresh scene. is it possible?
I used the following code.Here what I did is, I get the controller of that scene and then update the field using id. but it doesn't work.
URL location = AdmincontrolController.class.getResource("admincontrol.fxml");
FXMLLoader fxmlLoader = new FXMLLoader();
fxmlLoader.setLocation(location);
fxmlLoader.setBuilderFactory(new JavaFXBuilderFactory());
try {
Parent root = (Parent) fxmlLoader.load(location.openStream());
AdmincontrolController controller = fxmlLoader.getController();
System.out.println("AdmincontrolController: "+controller);
controller.setEmail(item.getEmail());
} catch (IOException ex) {
Logger.getLogger(Add_loginController.class.getName()).log(Level.SEVERE, null, ex);
}
Scenario:
scene
Popup - If we clicks on the add then we need to dismiss that dialog and change the email text on the previous scene.
As Alexander mentioned above, updating the underlying text property of the object you are using to display the email should Just Work. You need to make sure that you are working with the property (see Oracle Java FX Property Tutorial for more info). As a concrete example:
FXML
<Text fx:id="email" />
<TextField fx:id="emailInput" />
<Button onAction="#doSetEmail" text="Set Email"/>
In your controller, use the #FXML annotation to inject concrete instances of objects and set the handler to adjust the text:
Controller
#FXML
Text email;
#FXML
TextField emailInput;
#FXML
public void doSetEmail(ActionEvent ae) {
email.setText(emailInput.getText());
}
Alternatively, you could just bind the email text property to the email label property so that changes would automatically get propagated:
email.textProperty().bind(emailInput.textProperty());
You could do this in your controller initialize() method.
Now, the caveat to all this working depends on how you are handling the event and what you are doing in this. You still haven't posted the code for that as requested by the first answer, so you may be having issues there. Namely, if you are starting threads and then trying to update UI elements on the JavaFX thread from a worker thread, then you can get into trouble (potentially) with things not updating. This depends substantially on the structure of your objects, and you have not given enough information to comment in any meaningful way on that.
chooks
Everytime you have the Feeling that you manually want to update a Scene you should probably use a backgroundWorker Thread to do the work.
This way your UI Thread can use the time to update Labels etc.
JavaFX is built so that you don't need to directly call the scene update routine. All you need - update properties of scene components, and they will be updated on the nearest pulse.
So, all you need is to update properties. Or, is there any real trouble?
refreshing scene its not possible without closing...but if you can do class level declaration for control..i.e make them static its may work...
try this..
make a function in main file.
MainPanel.java
public static void SetMail(String email)
{
txtmail.setText(email);
}
LoginPanel.java
btnclear.setOnAction(new EventHandler<ActionEvent>() {
#Override
public void handle(ActionEvent t) {
MainPanel.SetMail(txtEmail.getText());
}
});
In Swing you have paint or update(Graphics g) method for each UI component which executes every frame. You can put your drawing logic in the overrided method and draw whatever you want. What is the paint method for JavaFX2 UI?
What I want to do is my UI receives control information from a socket server (another thread). Whenever a control information is received, I should update the UI.
There are two problems:
1. The control information is received from a different thread, it can not access the rendering thread directly.
2. How to update the UI constantly?
For Q1, I have a solution if I know where the update function is (Q2).
I can declare a List object, and insert the new control command received from the socket into the list. In the rendering loop, I can just observe the List object, retrieve the unprocessed command, and delete the already processed command.
However, where can I find such a rendering loop function? I guess maybe I can also do it with javax.concurrent.Task, but I don't find a way to do it.
I think i find the method, I use the following method and it can work. I don't know if there is a better solution
final Duration oneFrameAmt = Duration.millis(1000/10);
final KeyFrame oneFrame = new KeyFrame(oneFrameAmt,new EventHandler<ActionEvent>() {
#Override
public void handle(ActionEvent actionEvent) {
//put UI update here
}
});
TimelineBuilder.create().cycleCount(Animation.INDEFINITE)
.keyFrames(oneFrame)
.build()
.play();
I have a simple iPhone app with a simple view and a custom view as a child. The child is just a square painted on the main view.
I need to track a touch event that enters this child view but from a touch that started outside the view.
What I've tried so far is to add the TouchesBegin/TouchesMoved events to the parent view. Also tried to add the to the child controls directly but that doesn't track any touches that are not initiated within that control.
The questions are:
a) can I get the control from Position somehow?
b) is this the best way doing this, or is there another way?
Again, I include this video of GamePlay for the game I'm trying to port (on my spare time). It's not a promotion attempt but illustrates what I'm trying to accomplish. I wrote the Win8 and WP7 version of the game so I'm not trying to copy another persons work here. :) Don't watch it if you don't want to know what game it is. The question is still valid without watching this.
http://www.youtube.com/watch?v=13IczvA7Ipo
Thanks
// Johan
I stumbled on the answer myself. In the parent I override TouchesMoved (and Begin and Ended not shown here).
I then iterate the subviews and chech if it's the view I'm looking for by type and then check if the Frame contains the point. The code below is just concept of course.
public override void TouchesMoved (NSSet touches, UIEvent evt)
{
base.TouchesMoved (touches, evt);
UITouch touch = touches.AnyObject as UITouch;
if (touch != null) {
var rp = touch.LocationInView (touch.View);
foreach(var sv in this.View.Subviews)
{
if(!(sv is LetterControl))
continue;
if(sv.Frame.Contains (rp))
{
Console.WriteLine("LetterControl found");
}
}
}
}
I need to handle multiple panels, containing variuous data masks. Each panel shall be visible using a TreeView control.
At this time, I handle the panels visibility manually, by making the selected one visible and bring it on top.
Actually this is not much confortable, especially in the UI designer, since when I add a brand new panel I have to resize every panel and then design it...
A good solution would be using a TabControl, and each panel is contained in a TabPage. But I cannot find any way to hide the TabControl buttons, since I already have a TreeView for selecting items.
Another solution would be an ipotethic "StackPanelControl", where the Panels are arranged using a stack, but I couldn't find it anywhere.
What's the best solution to handle this kind of UI?
You need a wee bit of Win32 API magic. The tab control sends the TCM_ADJUSTRECT message to allow the app to adjust the tab size. Add a new class to your project and paste the code shown below. Compile. Drop the new control from the top of the toolbox onto your form.
You'll get the tabs at design time so you can easily switch between pages. The tabs are hidden at runtime, use the SelectedIndex or SelectedTab property to switch between "views".
using System;
using System.Windows.Forms;
class StackPanel : TabControl {
protected override void WndProc(ref Message m) {
// Hide tabs by trapping the TCM_ADJUSTRECT message
if (m.Msg == 0x1328 && !DesignMode) m.Result = (IntPtr)1;
else base.WndProc(ref m);
}
}
A good solution would be using a TabControl, and each panel is contained in a TabPage.
But I cannot find any way to hide the TabControl buttons, since I already have a
TreeView for selecting items.
For the above,
You need to set the following properties of TabControl.
tabControl.Multiline = true;
tabControl.Appearance = TabAppearance.Buttons;
tabControl.ItemSize = new System.Drawing.Size(0, 1);
tabControl.SizeMode = TabSizeMode.Fixed;
tabControl.TabStop = false;
I want to create a JavaFX table that, in the cells of one column, allows the user to edit XHTML text. I only need very basic formatting capabilities like bold, italic, striketrough.
I have already managed to implement this by using my own subclass of TableCell and using a WebView for each cell (HTMLEditor would of course have been another choice, but my guess is that for my requirements, WebView should be sufficient).
However, to make editing comfortable for the user, I need the following features:
1. The cell height needs to resize if the user enters multi-line text.
2. A context menu (or, if not possible, some other menu or button) should allow formatting parts of the text in a cell as described above (bold, italic..)
Has anybody been successful in implementing something similar ? I have seen suggestions on the web, but they rarely included code samples.
I have succedded doing something similar.
I figured I can share some of the basic clues that allowed me to achieve it.
Resize the whole WebView. For that, the whole WebView must be an editable html page. You achive that by setting contenteditable to true:
<body contenteditable='true' id='content'></body>
You can have a context menu over a webview. But it is something tricky, as you must first disable the original context menu associated to it.
WebView editView;
...
EventDispatcher originalDispatcher = editView.getEventDispatcher();
editView.setEventDispatcher(new WebmenuEventDispatcher(originalDispatcher));
And this is the event dispatcher class:
public class WebmenuEventDispatcher implements EventDispatcher {
private EventDispatcher originalDispatcher;
public WebmenuEventDispatcher(EventDispatcher originalDispatcher) {
this.originalDispatcher = originalDispatcher;
}
#Override
public Event dispatchEvent(Event event, EventDispatchChain tail) {
if (event instanceof MouseEvent) {
MouseEvent mouseEvent = (MouseEvent) event;
if (MouseButton.SECONDARY == mouseEvent.getButton()) {
mouseEvent.consume();
// Show our own menu
cmEdit.show(editView.getScene().getWindow(), mouseEvent.getScreenX(), mouseEvent.getScreenY());
}
}
return originalDispatcher.dispatchEvent(event, tail);
}
}
Now, for setting the font from within that menu, you need a bidirectional Java<->javascript bridge and use some javascript in the webview side.