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.
}
}
}
});
Related
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.
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.
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);
}
}
});
My intention is to use the ListCellRenderer in order to highlight red cells that contain links that have been visited(or clicked) and green those which have not been visited this works partially but not quite. It seems that the renderer works as far as it concerns marking the cells red. If I however add more rows, they come all red colored thereafter. In addition if I mark two cells that are not adjacent then it marks them all red as well.
I have a class Feed, where I initially had a boolean variable, but I have modified the code so that the m_isRead variable is in the listModel here is the constructor:
public Feed (URL url, String urlName) {
this.m_urlName = urlName;
this.m_observers = new ArrayList<Observer>();
this.m_isRead = isRead;
}
Now this instance variable is set to false in the listModel Class which is the one that contains the renderer.
m_isRead = false.
When using the ListCellRenderer which I now have adjusted so that it does not require this method:
m_feeds.get(index).getM_urlName();
I proceed as follows:
class MyCellRenderer extends JLabel implements ListCellRenderer {
public MyCellRenderer() {
setOpaque(true);
}
public Component getListCellRendererComponent(JList list,
Object value,
int index,
boolean isSelected,
boolean cellHasFocus) {
setText(value.toString());
Color background = Color.GREEN;
Color foreground = Color.BLACK;
//find out if the specific has been read or not
if (m_feeds.get(index).isM_isRead() == true) {
background = Color.RED;
foreground = Color.WHITE;
} else {
background = Color.GREEN;
foreground = Color.BLACK;
};
setBackground(background);
setForeground(foreground);
return this;
}
}
Then I have another inner class with a method which I use to get the selected item, at that point I set m_isRead to true (to read) this is now independent from the Feed class and the code which related to it has been commented out:
public class ChangeSelectedIndex implements ListSelectionListener {
#Override
public void valueChanged(ListSelectionEvent e) {
for (int i = 0; i < m_listModel.size(); i++) {
if (m_listModel.getElementAt(i) != null) {
m_urlName = m_list.getSelectedValue();
initiateParsing();
m_updateFeedButton.setEnabled(true);
// TODO fix behavior for cell renderer
//this sets the value of the feed being clicked to true
// m_feeds.get(i).setM_isRead(true);
m_isRead = true;
}
}// end for
}
}
Now the result is the same, if I add the rows they are green and that is correct, if I click on each row each turns read provided that I have clicked the adjacent rows to the first one I click but if I, for example, have four rows and I click the first row and the last row, all the rows, including those in between (which I have not clicked) turn red. Likewise, if I add new rows they come in red. That is if I click even one of those rows then the ones I add thereafter will be red.
Can anybody help?
Thank you in advance,
Cheers
After a while thinking about it I have concluded that there was nothing wrong my original cell renderer, it has had to do with the list model itself. The JList simply did not support multiple NON contiguous selection without clicking the Ctrl button right out of the box. This is what triggered further searching on my side on how to emulate the Ctrl click down; which I found here on answer number 8 (working code):
Individual and not continuous JTable's cell selection
The interesting here is adding the mouse event to the list. This mouse event emulates a Ctrl down event, which the ListSelectionModel which is used by JList as well as JTable is set to MULTPLE_SELECTION_INTERVAL it behaves as desired. That is, the user now able to click on whatever feed, even if it is not contiguous, and it will color the desired feed or feeds without coloring whatever unclicked feed may lay in between.
As for the renderer, it would suffice to use the isSelected parameter which comes in through with its method getListCellRenderer(). However, in my case, what I had done has the same effect with the addition that I was using an array to add all the statuses of the feeds, meaning, read or unread. Proceeding this way, I had in mind that if I closed the program and save the feed list, including its isRead parameter set to either true or false, then later on upon retrieving the feed list, the same feed status would be restored from, for example, a file, or at least that is what I had in mind.
I have two TreeViewer objects on a page (2 columns, one TreeViewer in each column), and I want to vertically align a tree when the other is scrolled or selected.
import org.eclipse.jface.viewers.TreeViewer;
import org.eclipse.swt.widgets.Tree;
I think the solution should look something like
treeViewer.addSelectionChangedListener(new ISelectionChangedListener() {
#Override
public void selectionChanged(SelectionChangedEvent arg0) {
TreeViewer mirrorTree = (treeViewer == treeVwrSource ? treeVwrTarget : treeVwrSource);
// find position of selected element (element x) in treeViewer
// set position of element x in mirrorTree, it is already selected.
}
});
Any advice?
From what I've read, scrolling tables or trees isn't directly possible (unless that has changed in the meantime). Programmatically changing the position of the thumb of the scrollbar would not change the viewport of the table/tree.
But you could try if the following snippet works for you:
Tree tree1 = ...;
Tree tree2 = ...;
int topIndex = tree1.indexOf(tree1.getTopItem());
tree2.setTopItem(tree2.getItem(topIndex));
You would call that code in a SelectionListener registered to the vertical scrollbar of your tree (tree.getVerticalBar()).
Synchronizing the selection is fairly easy (if both tree viewers display the same input/model):
viewer.setSelection(otherViewer.getSelection)
(called by the ISelectionChangedListener from your question).
For a complete example of how to synchronize two tables see this SWT Snippet.