Deselect selected row in Vaadin Grid - java

Can somebody give me a hint because I am stucked. And I haven't found the appropriate solution for my problem.
I have a Grid, with 1-3 rows. I click on the row -> the row is selected.
I want to have this row to be deselected after I click somewhere else (outside this row) but inside the grid.
Here is simple screenshot, to help you visialize it better.
What kind of listener should I use for this case? I have tried ItemClickListener -> haven't helped.

Try putting your grid in a separate layout and add LayoutClickListener to it:
gridLayout.addLayoutClickListener(new LayoutEvents.LayoutClickListener() {
#Override
public void layoutClick(LayoutEvents.LayoutClickEvent event) {
if(grid.getSelectedRow() != null) {
grid.deselectAll();
}
}
});

gridLayout.asSingleSelect().addSelectionListener(e->{
if(e.getFirstSelectedItem().isPresent()) {
system.out.println("selected");
}else {
// when deselected make some actions here
system.out.println("deSelected");
}
});
Selection:
If you click on the same row of data for the first time , it will print selected ,since there are values present on what you clicked .
Deselection:
Now try to click on the same selected row for the second time , now it will go to deselection mode. since there are no values when you deselected.Now no need to click anywhere on the grid ,you can click on the same row of data for two times for the selection and deselection.

Related

JavaFX - ListView Selection and Making Text Appear

This is my first post and I'm still a beginner in Java. The purpose of this program is to create a menu with food items in it and be able to select the items using ListView (which is the variable menu in this case). When I select the food items in the ListView, they are supposed to appear on the right side of the screen showing the text of what is selected (i.e. if Hamburger and Hotdog are selected from ListView, Hamburger and Hotdog appear as text on the right side of the screen). I got some pictures to include too, using ImageView and those images appear just fine without a problem. The problem is when I select from ListView, nothing appears because I'm not sure how to make it appear... I've tried get the index and then setting the text like that, getting the items, etc... I'm just really lost at the moment. I'm just not sure why it's not working because in my head the plan works fine, I get the index and then set that index to the ListView, getting the strings, but it's just not working. What am I doing wrong, or how should I be thinking about solving this?
Edit:
When I try to add the text from this line:
vBox.getChildren().add(foodItemsList[i]);
foodItemsList[i] has the error of
ArrayType expected, instead found
javafx.collections.ObservableList
This is my code:
menu.getSelectionModel().selectedItemProperty().addListener( e -> {
flowPane.getChildren().clear();
for (Integer i : menu.getSelectionModel().getSelectedIndices()) {
flowPane.getChildren().add(foodPics[i]);
}
});
// ### This just displays the current value and the value I last selected, if I selected 3+ values,
// the last 2 are the only ones that show.
menu.getSelectionModel().selectedItemProperty().addListener(new ChangeListener<String>() {
#Override
public void changed(ObservableValue<? extends String> observable, String oldValue, String newValue) {
item1.setText(newValue);
item2.setText(oldValue);
}
});
menu.getSelectionModel().selectedItemProperty().addListener( e -> {
flowPane.getChildren().clear();
for (String i : menu.getSelectionModel().getSelectedItems()) {
vBox.getChildren().add(foodItemsList[i]);
}
});

Setting tab cursor position in TableView JavaFX

I have a javafx tableview and I'm trying to write a keylistener so the user can quickly select rows and edit their contents.
So far I can focus the table with a key press and select/focus a row with a digit key press. However I then want the user to be able to tab through the row to select the cell he wants to edit.
My problem is whenever I press the tab when a row is selected, it always starts selecting cells from the first row.
Here is the code from my key listener:
....
else if(code.isDigitKey()){
TableView tv = scene.getSelectedTable();
if(tv != null){
tv.getSelectionModel().select(code.ordinal()-24);
tv.getFocusModel().focus(code.ordinal()-24);
//Something required here to set tab position to start of this row
}else{
System.out.println("null");
}
}
Is there a way to set a tab starting position?
SOLUTION:
Tab simply goes to the next JavaFX control element that it knows about. The TableView had focus so when I pressed tab it went to the next control element which just so happened to be inside the table. Tab does not traverse through table cells.
So the work around I came to was to call the edit method on the first cell in the selected row:
....
else if(code.isDigitKey()){
TableView tv = scene.getSelectedTable();
if(tv != null){
tv.getSelectionModel().select(code.ordinal()-24);
tv.getFocusModel().focus(code.ordinal()-24);
tv.edit(code.ordinal()-24, ((TableColumn)(tv.getColumns().get(0))));
}else{
System.out.println("null");
}
}
Then in my CellFactory for that column (It happens to be a Spinner), I override startEdit:
#Override
public void startEdit(){
this.spinner.requestFocus();
}
Note the column and table must be editable.

Questions about Jtable. Click within particular column perform event, is this possible?

I've added several rows to a Jtable, and I don't know if it's possible but I'd like if you click on any cell within a particular column then the connecting row is removed.
Are functions like this possible?
(I'm not asking anyone to do all the work for me. Just asking for information or a tutorial link) thanks :-)
attach a mouse listener to the table and when mouse click event occurs if column matches your particular column then remove that row.
tbl.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent e) {
int row = tbl.rowAtPoint(e.getPoint());
int col = tbl.columnAtPoint(e.getPoint());
if(col == SPECIFIC_COLUMN_INDEX){
((DefaultTableModel)tbl.getModel()).removeRow(row);
}
}
});

how to add a clickHandler on a subRow of celltable in GWT

I am using Custom Data Grid from GWT showcase example ..
http://gwt.googleusercontent.com/samples/Showcase/Showcase.html#!CwCustomDataGrid
Every thing is working fine ..I have sub Rows inside my rows in the cell table ..
I have anchor cell.. which are in the main row and in the sub row ..
the ClickHandler for the main row is working but not in the sub row ..
this is my code for that cell
// ViewDetail.
td = row.startTD();
td.className(cellStyles);
td.style().trustedColor("blue");
td.style().cursor(Cursor.POINTER);
if (isNetworkRow) {
//td.text("subRowsAnchor");
} else {
}
renderCell(td, createContext(19), viewDetailsColumn, rowValue);
I am rendering the cell in both cases , either its a row or sub row
so i can see the anchor and its clickHandler also works ..
Is there any way i can differentiate that which anchor is been clicked ,, main rows or sub row's.
I just tried to make a small work around . i.e changing the name of the anchor text if its a sub row .. as u can c in my code ..td.text..
but then get the error on renderCell...
Attributes cannot be added after appending HTML or adding a child element.
Any idea , what could be solution...
thanks
To distinguish between which row has been clicked (according to the showcase sample, but should be the same in general), simply rely on which row has been selected (provided that you haven't overridden/disabled the selection handling).
Set up a FieldUpdater to the column (that renders itself using your anchor cell) and check for the subrow selection using getKeyboardSelectedSubRow(). Something like:
yourColumn.setFieldUpdater(new FieldUpdater<T, String>() {
public void update(int index, T object, String value) {
if (yourGrid.getKeyboardSelectedRow() != -1 ) {
if (yourGrid.getKeyboardSelectedSubRow() > 0) {
// Subrow selected.
} else {
// Main row selected.
}
}
}
});

Java JTabbedPane, how can I select a tab from a button?

How can I select a tab as if it was clicked by clicking on a button?
I have googled and looked at all the actions but there are just just so many... :(
Anyone know off hand?
Thanks in advance!
Add an action listener to the button that calls setSelectedComponent, or setSelectedIndex on the JTabbedPane.
I'm not sure what you mean about the button, but you might be looking for setSelectedComponent or setSelectedIndex.
If your jtabbedpane's name is mytabbedpane it goes like this:
mytabbedpane.getSelectedIndex();
which returns the int of that tab (0,1 .. n) or
mytabbedpane.getSelectedComponent();
which returns the String of the tab's name ("Firts tab","Second tab",...).
If you want to use the "getSelectedComponent()" for boolean logic you should write something like:
if (mytabbedpane.getSelectedComponent().equals("First tab")) {
//code here
}
and for the "getSelectedIndex()" one is of course:
if (mytabbedpane.getSelectedIndex() == 0) {
//code here
}
Double click on button, put the follwing code
JTabbedPane.setSelectedIndex(1);
Tabs starts from 0 to N left to right order
Try this code:
tabbedPane.addTab(tabName, component);
int count = tabbedPane.getTabCount();
tabbedPane.setSelectedIndex(count-1);

Categories