How to keep a TableView's selection when focus is lost? - javafx-2

How can I keep a TableView's selection when focus is lost to the Windows?
When a the focus to a window is lost, the selected item is still visibly selected in the window; however, I cannot find a way to access the selected object from its controllers.
I have tried using the TableView's methods getSelectionModel().selectedItemProperty().get() and getSelectionModel().getSelectedItem() but both of these return null if the focus to the window and/or table is lost.

Use a variable and set it in a change listener. I use labels for debugging.
table.getSelectionModel().getSelectedCells().addListener(new ListChangeListener<TablePosition>() {
#Override
public void onChanged(ListChangeListener.Change<? extends TablePosition> c) {
label1.setText(String.valueOf(c.getList().get(0).getRow()));
}
});
java 8
table.getSelectionModel().getSelectedCells().addListener((ListChangeListener.Change<? extends TablePosition> c) -> {
label1.setText(String.valueOf(c.getList().get(0).getRow()));
});

Related

How to get all selected rows data in javafx

there is a problem!!
In javafx table view i applied multiple selected mode by Shift+mouseClick or Clt+MouseClick. By This
tblViewCurrentStore.setOnMouseClicked(new EventHandler<MouseEvent>() {
#Override
public void handle(MouseEvent event) {
tblViewCurrentStore.getSelectionModel().setSelectionMode(SelectionMode.MULTIPLE);
}
});
it's ok on GUI but problem is, if i use this code it give me the last selection cell's value,
private void btnDeleteOnAction(ActionEvent event) {
System.out.println(tblViewCurrentStore.getSelectionModel().getSelectedItem().getProductName().toString());
}
Out Put SAMSUNG HDD
but when i use this code it give this!
private void btnDeleteOnAction(ActionEvent event) {
System.out.println(tblViewCurrentStore.getSelectionModel().getSelectedItems().toString());
}
It Give me This types of output
[List.ListProduct#3a22ea22, List.ListProduct#6d99efa2, List.ListProduct#40fd0f67]
But i need when i select multiple row then press delete it will show all selected data like first one.
Hear is my GUI(With multiple selection)
You can even use this :
ArrayList<YourModel> products = new ArrayList<>(table.getSelectionModel().getSelectedItems());
for (YourModel model : models) {
System.out.println(model);
}
//OR
final List<YourModel> collect = table.getSelectionModel().getSelectedItems().stream().collect(Collectors.toList());
There are multiple problems with your code:
tblViewCurrentStore.getSelectionModel().setSelectionMode(SelectionMode.MULTIPLE);only needs to be set once (thus its a setter). Do it after your TableView has been initialized and not on every click.
SelectionModel#getSelectedItem() clearly says what it does:
Returns the currently selected object (which resides in the selected index position). If there are multiple items selected, this will return the object contained at the index returned by getSelectedIndex() (which is always the index to the most recently selected item).
And finally SelectionModel#getSelectedItems returns all selected objects (as in Java Objects).
So if you want the names, you can something like this:
List<String> names = tblViewCurrentStore.getSelectionModel().getSelectedItems().stream()
.map(ListProduct::getProductName)
.collect(Collectors.toList());

Resizing GXT Window When Browser Window Resizes

Developing using GXT 2.2.5 on GWT 2.3.0.
The application I'm working on displays a resizable Window with auto scrollbars. I want to make sure the Window size does not exceed the size of the browser window it gets displayed in. To that effect, I added the following code:
addWindowListener(new WindowListener()
{
public void windowActivate(WindowEvent we)
{
super.windowActivate();
Window window = we.getWindow();
window.layout(true);
int width = window.getWidth();
int height = window.getHeight();
int maxWidth = XDOM.getViewportWidth();
int maxHeight = XDOM.getViewportHeight();
window.setSize(Math.min(width, maxWidth), Math.min(height, maxHeight));
window.center();
}
};
This manages sizing the Window to fit in the browser when it gets opened quite nicely.
My problem is that if the user then resizes the browser window, the open Window does not adjust and ends up being clipped.
Is there some way for me to either force the Window to stay within the boundaries of the browser, or capture the resize event so that I can tell the Window to resize accordingly?
Thanks.
You need to add the listener (actually handler -- listeners got depreciated) to the browser window.
So if you have several different windows you show at different times that all need to be resized with the window, you need to have a listener for each window and add it to the browser window when the window is shown.
To add a listener to the browser Window, use:
com.google.gwt.user.client.Window.addResizeHandler(handler);
So for example to resize ContentPanel cp:
com.google.gwt.event.logical.shared.ResizeHandler handler = new ResizeHandler() {
#Override public void onResize(com.google.gwt.event.logical.shared.ResizeEvent event) {
cp.setWidth(event.getWidth());
cp.setHeight(event.getHeight());
}
};
So then if you switch to a new view or ContentPanel you need to register that...
com.google.gwt.user.client.Window.addResizeHandler(handler2);
com.google.gwt.event.logical.shared.ResizeHandler handler2 = new ResizeHandler() {
#Override public void onResize(com.google.gwt.event.logical.shared.ResizeEvent event) {
cp2.setWidth(event.getWidth());
cp2.setHeight(event.getHeight());
}
};
NOTE:
You can't do event.getWindow() for the ResizeEvent you need to handle that another way.
Actually I tend to do it in a constructor like:
com.google.gwt.event.logical.shared.ResizeHandler handler2;
public MyDialog(){
handler2 = new ResizeHandler() {
#Override public void onResize(com.google.gwt.event.logical.shared.ResizeEvent event) {
setWidth(event.getWidth());
setHeight(event.getHeight());
}
};

Is it possible to forbid selection in TextField?

I have a small problem: my text field keeps selecting itself, for example if I alt-tab from and to application.
For my application, text selection is not needed and will not be used - so I want to disallow this annoying behavior. Actually, just setting selection color to transparent or white will work fine.
Is there some way to do this?
The following css fixed the problem for me:
-fx-highlight-fill: null;
-fx-highlight-text-fill: null;
You can disable text selection for key events:
myTextField.addEventFilter(KeyEvent.KEY_TYPED, new EventHandler<KeyEvent>() {
#Override
public void handle(KeyEvent inputevent) {
if (!myTextField.getSelectedText().isEmpty()) {
myTextField.deselect();
}
}
});
For mouse events you could also use:
myTextField.addEventFilter(MouseEvent.MOUSE_DRAGGED, new EventHandler<MouseEvent>() {
#Override
public void handle(MouseEvent event) {
if (!myTextField.getSelectedText().isEmpty()) {
myTextField.deselect();
}
}
});
Super late for an answer, but I have a better solution.
Instead of disabling the style for selected text or managing mouse events, it's better to directly manage the selectedTextProperty().
The advantages of this solution are:
The selection properties will always be empty selection
Double clicks are also covered, not only mouse drag events
The code...
textField.selectedTextProperty().addListener((observable, oldValue, newValue) -> {
if (!newValue.isEmpty()) textField.deselect();
});

Winforms MDI and TreeView

I am currently working on a winforms application. In one of requirements I need to make sure that I can add a node to a treeView which is contained in a child form , when i click on a tabstrip button of the mdi parent.
if someone can please help me with this, it would be awesome and well appreciated..
Thanks and regards
GJ
In your parent form, keep a refernce to the child form around.
In the child form, add a public method or something that adds a node to the tree view. And when you click that tab strip button, just call that method on the child reference you have.
public Window
{
ChildForm childForm;
public Window()
{
childForm = new ChildForm();
childForm.Show();
}
public OnTabStripClicked()
{
childForm.AddNode();
}
}
public ChildForm
{
public void AddNode()
{
treeView.Nodes.Add();
}
}

Monotouch-Dialog ReloadData does not reloads data?

I want to use monotouch dialog as not editable data display for some numeric values. But calling DialogViewController.ReloadData does not updates data from binded object.
class AccountFormModel
{
[Section("Account data", "")]
[Caption("Balance")]
public string balance;
}
...
private void InitComponents()
{
accountFormModel = new AccountFormModel();
accountFormModel.balance = "TestTestTest";
bc = new BindingContext(this, accountFormModel, "AccountData");
dialogViewController = new DialogViewController(bc.Root);
dialogViewController.Autorotate = true;
}
private void RefreshData()
{
string b = SomeDatasource.Account.Balance.ToString("N4");
accountFormModel.balance = "$" + b;
dialogViewController.ReloadData();
}
Debugging shows that accountFormModel.balance in refreshData method is set to right value, but nothing changes on form in simulator (stays TestTestTest). What i'm doing wrong?
DialogViewController when using reflection does the binding once initially, and only when you FetchData() is the data transferred back to your class.
What happens is that the BindingContext will basically create the model from your data (balance in this case) effectively making a copy of your data at this point. When you call ReloadData() this is reloading the data from the copy, and that is why you do not see a change. Although this could be changed to have some method on the BindingContex to repopulate the data, this is not currently the case.
The Reflection API for MonoTouch.Dialog is very limited, I strongly advise you that for anything non-trivial, you use the Elements API. Most of the samples in MonoTouch.Dialog use that API, as it gives you full control of the dialog.

Resources