When a user clicks a 'Add Node' button above a tree and the program adds a tree item below the selected node, I would like to insert the new tree item with the text highlight and ready for editing by the user... like labels in GMail. Any ideas?
--Kirt
Are you using the GWT default TreeItem? If so, when you add the node, you could add the TreeItem with a Widget which you write which contains a TextBox and a Button to save.
When the save button is clicked, it calls setText() on the tree item with the text box's text, thus removing the widgets from the tree item.
It may be an even better idea to subclass TreeItem to encapsulate this logic and provide more functionality.
edit: Here, just because I'm feeling generous...
public class EditableTreeItem extends TreeItem {
public EditableTreeItem() {
super();
TextBox textBox = new TextBox();
Button saveButton = new Button("Save");
saveButton.addClickHandler(new ClickHandler() {
#Override
public void onClick(ClickEvent e) {
if (!textBox.getText().isEmpty()) {
EditableTreeItem.this.setText(textBox.getText());
}
}
});
}
}
Related
So am trying to make a project where the user can select from a tree a TreeItem called Category , and by that category, a table will sort so it matches it.
The problem is that am using WindowBuilder in Eclipse and I don't see an event that says so, or a way to make it that the point where the cursor is aiming at, is the Item selected (when clicking of course) .
The other thing is that, is there any way to use Jforms using WindowBuilder? I think there is a big difference between them.
And for information, I already searched in the net and all I found is solutions about TreeView which first I am not using and second I tested it and still it doesn't work.
This is the Code I used so I can detect the selected TreeItem when I click but it does not work either:
Tree tree = new Tree(Frame, SWT.BORDER);
tree.addSelectionListener(new SelectionAdapter() {
#Override
public void widgetSelected(SelectionEvent e) {
TreeItem item =
tree.getItem(Display.getCurrent().getCursorLocation());
if(item != null) {
...
You can get the selected TreeItem from the item property of SelectionEvent:
tree.addSelectionListener(new SelectionAdapter() {
#Override
public void widgetSelected(SelectionEvent e) {
// get the selected item
TreeItem item = (TreeItem) e.item;
if(item != null) {
...
I'm currently made an Form with JavaFX.
Always i press a Button, i call the "addAnswer()"-Method.
In that I create a RadioButton, a Label and a delete-Button, which i bundle in a HBox. All that HBoxes i pack in a vBox.
The Problem now is the delete-Button. I want to delte just THAT HBox in which the clicked Button is.
Here is my code:
public void addAnswer() {
this.rB = new RadioButton();
checkAnswer.getToggles().add(rB);
hBox = new HBox();
tF = new TextField();
delAnswer = new Button("Löschen");
delAnswer.setId(Integer.toString(counter));
hBox.getChildren().addAll(rB, tF, delAnswer);
hBox.setId(Integer.toString(counter));
delAnswer.setOnAction(e -> delAnswer(Integer.parseInt(hBox.getId())));
System.out.println(delAnswer.getId());
vBox.getChildren().addAll(hBox);
counter++;
}
public void delAnswer(int e){
vBox.getChildren().remove(delAnswer.getId());
}
i tried this one above but i realized, that all the delAnswers-Buttons have the same ID: the number of how often i pressed the add-Button.
Is there any solution where i can just select that one i pressed with that dynamic way? Cause i don't kow how often somebody will press or delete something.
Thanks
hbox is a field and this is why always the HBox last added is used. (hBox is evaluated, when lambda body is executed, not at the time of the lambda creation). This would be different, if you used a (effectively) final local variable:
final HBox hBoxLocal = hBox;
delAnswer.setOnAction(e -> delAnswer(Integer.parseInt(hBoxLocal.getId())));
However I'd like to present a different solution which would allow you to use the same EventHandler<ActionEvent> for all delete Buttons:
You can get the Node that triggered the event using getSource. From this Node you can get the parent, which is the HBox. You can remove this from the VBox using the remove(Object) method
delAnswer.setOnAction(e -> {
// get button
Node source = (Node) e.getSource();
// remove parent of button from VBox
vBox.getChildren().remove(source.getParent());
});
I think your problem is that you give the same event to all your button,Begin by creating a list that stores your buttons and then increments the value of the ID after affecting it to an item :
List<Button> buttons = new ArrayList<>();
/*
Create Button and call IDEvt method to create new event
for each button
*/
private void IDEvt(Button btn){
btn.setId(String.valueOf(IDRank));
btn.setOnMousePressed(new EventHandler<MouseEvent>() {
#Override
public void handle(MouseEvent event) {
System.out.println(btn.getId());
}
});
IDRank++;
}
I have a cell table showing some data. For each row, I want to have two columns which contain edit / delete buttons. When each button is clicked, it should be able to notify a listener which button was clicked (and preferably also be able to pass in the object that row is associated with).
How can I do this? Specifically, I know how to render a button, but how can I process the on-click event and pass in the object which the user clicked to edit or delete?
This is the standard approach:
myTable.addCellPreviewHandler(new Handler<MyObject>() {
#Override
public void onCellPreview(CellPreviewEvent<MyObject> event) {
if ("click".equals(event.getNativeEvent().getType())) {
if (event.getColumn() == 0 || event.getColumn() == 1) {
MyObject object = event.getValue();
Window.alert("Column clicked: " + event.getColumn());
}
}
}
});
This is a more efficient solution, because you only have one handler attached to a table, instead of trying to attach a handler to each button in each row.
I think you can make a foreach through all the rows in the celltable (I never worked with celltables)
And then you can add your own ClickHandler to the Button.
Something like that (not tested):
final int row = myrow; // add the row value or a object identifier or similar
Button delete_button = new Button("delete");
delete_button.addClickHandler(new ClickHandler(){
#Override
public void onClick(ClickEvent event) {
// Insert your delete funciton
delete(row);
}
});
You mentioned a Listener, Listener are depreciated, use Handler instead.
I am using Vaadin 7.1.7.
I have a Table which has a few TextFields and a Button called "delete".
On click of the delete button, that particular row is to be deleted.
As i understand, I could remove table item as follows:
table.removeItem(itemID);
Unfortunately, I am unable to fetch the itemID of the row to remove it from the table.
Since, I used table.addItem(o, null); to addItems to it, how could I get the rowID/itemID on the click of the button inside buttonClickListener?
My trys so far have been:
#Override
public void buttonClick(ClickEvent event) {
Table t = (Table) event.getButton().getParent();
}
This has got me to the parent table but not to that particular item.
Thanks in advance
.
You could for example use setData(rowID) when you create the buttons.
The onClick you retrieve the associated data of the button and have the correct row id.
Provide a row id, override Button.ClickListener, and use the id in the click listener.
Object rowId = new Object();
Button button = new Button("Delete");
button.addClickListener(new RowDeleteListener(rowId));
//populate cells in the row, add the button & whatever
table.addItem(row, rowId);
public class RowDeleteListener implements Button.ClickListener {
Object rowId;
public RowDeleteListener(Object rowId) {
this.rowId = rowId;
}
public void buttonClick(ClickEvent event) {
table.removeItem(rowId);
}
}
Or André Schild’s solution, which is to use setData(rowId) on the button.
Button button = new Button("Delete");
button.addClickListener(new Button.ClickListener() {
public void buttonClick(ClickEvent event) {
table.removeItem(getData());
}
});
//Populate row stuff.
button.setData(table.addItem(row, null));
I like the first solution slightly better because it's more obvious what's going on, and also because the button has the correct row id before it gets added to the table instead of after.
Or, if you feel like creating something obnoxious: you could use the Button object as the id for the row.
Button button = new Button("Delete");
button.addClickListener(new Button.ClickListener() {
public void buttonClick(ClickEvent event) {
table.removeItem(this);
}
});
//populate row stuff, including adding the button to the row.
table.addItem(row, button);
I didn't test or compile any of these so... you know...
I use JFace DialogCellEditor to show a button in a cell of a row of my JFace TableViewer which triggers a dialog when activated. This behaviour works well with the following code but the button only appears when the cell of the table hosting the button is explicitly selected.
public class CompareDialogCellEditor extends DialogCellEditor {
public CompareDialogCellEditor(Composite parent) {
super(parent);
}
#Override
protected Button createButton(Composite parent) {
Button button = super.createButton(parent);
button.setText("");
button.setImage(AbstractUIPlugin.imageDescriptorFromPlugin(Application.PLUGIN_ID, IImageKeys.COMPARE_ICON).createImage());
return button;
}
#Override
protected Object openDialogBox(Control cellEditorWindow) {
MessageDialog.openInformation(cellEditorWindow.getShell(), "Test", "It works");
return null;
}
}
Is there a way to force the button to always appear in the table and not only when the cell is selected? (the same behaviour goes for a label set by the overridden method setContents(...) )
Thanks
You can only edit one Viewer cell at a time. Viewer won't support editing multiple cells at a time unless you do some customization.
I can think of following solutions.
Paint widget ( button, text, combo..etc) like image on table cell and invoke
CellEditor when user activates it.
You can find some examples here about how to paint on Table Cell.
http://www.eclipse.org/articles/article.php?file=Article-CustomDrawingTableAndTreeItems/index.html
I posted an answer about how to show button in table cell here. you can following the same concept with CellEditor
SWT - Tableviewer adding a remove button to a column in the table