How do I add a click handler to the GWT ButtonCell? - java

I created a ButtonCell and a Column for it:
ButtonCell previewButton = new ButtonCell();
Column<Auction,String> preview = new Column<Auction,String>(previewButton) {
public String getValue(Auction object) {
return "Preview";
}
};
How do I now add a click handler (e.g. ClickHandler) for this ButtonCell?

The Cell Sampler example includes use of clickable ButtonCells. Clicks on ButtonCells are handled by setting the FieldUpdater for the Column:
preview.setFieldUpdater(new FieldUpdater<Auction, String>() {
#Override
public void update(int index, Auction object, String value) {
// The user clicked on the button for the passed auction.
}
});

//Prevent mouse events for table cell
CellPreviewEvent.Handler<Auction > manager = DefaultSelectionEventManager.createBlacklistManager(4);//column number
cellTable.setSelectionModel(selectionModel, manager);
new Column<Auction , String>(new ButtonCell()){
#Override
public String getValue(Auction object) {
return "Preview";
}
#Override
public void onBrowserEvent(Cell.Context context, Element elem, Auction object, NativeEvent event) {
event.preventDefault();
//TODO implement event handling
}
}

Related

Checkbox clicking check other checkboxes too

When I click my first second and third checkboxes, it also checks last 3 checkboxes?
this is my adapter:
#Override
public void onBindViewHolder(#NonNull final NewGamePlayerViewHolder holder, int position) {
final NewGamePlayerItem currentItem = mNewGamePlayerList.get(position);
//in some cases, it will prevent unwanted situations
holder.mCheckBox.setOnCheckedChangeListener(null);
//if true, your checkbox will be selected, else unselected
holder.mCheckBox.setChecked(currentItem.isSelected());
holder.mCheckBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
//set your object's last status
currentItem.setSelected(isChecked);
}
});
holder.mName.setText(currentItem.getmText());
}
This is the item:
package com.example.frisbeecaddy;
public class NewGamePlayerItem {
private boolean mCheckBox;
private String mText;
public NewGamePlayerItem(boolean checkBox, String text) {
mCheckBox = checkBox;
mText = text;
}
public boolean getCheckBox() {
return mCheckBox;
}
public String getmText() {
return mText;
}
}
This is copied from here:
CheckBox in RecyclerView keeps on checking different items
but for me isSelected() and setSelected() it says: cannot resolve method...
First, you need to remember what the core meaning of RecyclerView, this sum it all (see RecyclerView glossary of terms):
Recycle (view): A view previously used to display data for a specific adapter position may be placed in a cache for later reuse to display the same type of data again later. This can drastically improve performance by skipping initial layout inflation or construction.
So, your First problem of:
When I click my first second and third checkboxes, it also checks last 3 checkboxes
means that your same RecyclerView item is being reused in another item. To tackle the problem, you need to add mechanism to hold the check state for each item. You could do it either by using a SparseBooleanArray or modifying your object to have a state variable.
The Second problem:
but for me isSelected() and setSelected() it says: cannot resolve method...
is because of the following code:
final NewGamePlayerItem currentItem = mNewGamePlayerList.get(position);
...
//set your object's last status
currentItem.setSelected(isChecked);
where you're trying to call a non-existed method in your NewGamePlayerItem.
You need to modify your object to something like this:
package com.example.frisbeecaddy;
public class NewGamePlayerItem {
private boolean mCheckBox;
private String mText;
private boolean mIsSelected;
public NewGamePlayerItem(boolean checkBox, String text, boolean isSelected) {
mCheckBox = checkBox;
mText = text;
mIsSelected = isSelected;
}
public boolean getCheckBox() {
return mCheckBox;
}
public String getmText() {
return mText;
}
// the added methods here
public boolean isSelected() {
return mIsSelected;
}
public void setSelected(boolean isSelected) {
mIsSelected = isSelected;
}
}

JavaFX can not clear items from listview

I'm using a ListView in my project and wanted to add a context menu to each list item so that each can be removed individually. When using the following code this appears to work just fine:
postList.setCellFactory(lv -> {
ListCell<Result> cell = new ListCell<>();
ContextMenu contextMenu = new ContextMenu();
StringBinding stringBinding = new StringBinding() {
{
super.bind(cell.itemProperty().asString());
}
#Override
protected String computeValue() {
if (cell.itemProperty().getValue() == null) {
return "";
}
return cell.itemProperty().getValue().getTitle();
}
};
cell.textProperty().bind(stringBinding);
MenuItem deleteItem = new MenuItem();
deleteItem.textProperty().bind(Bindings.format("Delete item"));
deleteItem.setOnAction(event -> postList.getItems().remove(cell.getItem()));
contextMenu.getItems().addAll(openPermalink, openSubreddit, openURL, deleteItem);
cell.emptyProperty().addListener((obs, wasEmpty, isNowEmpty) -> {
if (isNowEmpty) {
cell.setContextMenu(null);
} else {
cell.setContextMenu(contextMenu);
}
});
return cell;
});
However, after clearing the post list - although the items appear to be removed - when another is added all of the removed items re-appear and the item to be added is not displayed.
Any items what could be causing this? It only happens when I set the cell factory and is fine otherwise.
I recorded a small gif to help better explain the issue:
Thank you!
Edit: It appears that the issue is mainly to do with this segment
StringBinding stringBinding = new StringBinding() {
{
super.bind(cell.itemProperty().asString());
}
#Override
protected String computeValue() {
if (cell.itemProperty().getValue() == null) {
return "";
}
return cell.itemProperty().getValue().getTitle();
}
};
As is seems that even though the items are there they have an empty display title
If you use ListCell.updateItem() workflow instead of the StringBinding it should work:
ListCell< Result > cell = new ListCell< Result >() {
#Override
protected void updateItem(String item, boolean empty) {
super.updateItem(item, empty);
if (item != null) {
setText(item.getValue());
} else {
setText("");
}
}
};
Your binding workflow seems to create an unnecessary dependency which blocks deletion.
P.S.: why do you use binding for static text in deleteItem? Just assign the value directly:
MenuItem deleteItem = new MenuItem();
deleteItem.setText("Delete item");

How to change listview item text color and image color from handler

I am new for android, I have ListView with custom adapter, I pass one string if matches in ListView item want to change list item text color from Activity.
Here my code:
MyActivity:
public void handleResult(String rawResult) { 
if(Utility.isNotNull(rawResult.getText().toString())) {
for(int i=0;i<listView.getAdapter().getCount();i++){
if(rawResult.equals(listItems.get(i).getStockItems())){
// listView.getChildAt(i).setBackgroundColor(ContextCompat.getColor(context, R.color.hint));
/* Here I want to change list item text color*/
adapter.notifyDataSetChanged();
 
}
}
}
}
Thanks in advance!
In your model class add one paramter like this
public class DataHolder{
private String StockItems;
private int isSelected;
public DataHolder(String StockItems, int isSelected) {
this.StockItems = StockItems;
this.isSelected = isSelected;
}
public String getStockItems() {
return StockItems;
}
public void setStockItems(String StockItems) {
this.StockItems = StockItems;
}
public int getiIsSelected() {
return isSelected;
}
public void setIsSelected(String isSelected) {
this.isSelected = isSelected;
}
}
initialise IsSelected zero
public void handleResult(String rawResult) {
if(Utility.isNotNull(rawResult.getText().toString())) {
for(int i=0;i<listView.getAdapter().getCount();i++){
if(rawResult.equals(listItems.get(i).getStockItems())){
listItems.get(i).setIsSelected(1);
adapter.notifyDataSetChanged();
}
}
}
}
In your cusom adapter class check
if(listItems.get(i).getiIsSelected()==1)
{
//set red text color
}
else
{
//set black text color
}
UI tasks can only be done on the UI Thread. If u want to run it from the handler, you have to define a runOnUiThread method. Take a look at this ans
how to use runOnUiThread
try this code:
for(int i=0;i<listView.getChildCount();i++) {
// yours code
View item = listView.getChildAt(i);
item.setBackgroundResource(R.drawable.your_image); //change image
((TextView)item.findViewById(R.id.text1)).setTextColor(Color.RED); //text1 is your cusotm listview item's text id
adapter.notifyDataSetChanged();?
}
I assume you use TextView , To change his text color, first you need to get him, when you create your Item, add id to TextView
with xml
<TextView
android:id="#+id/myId"...
Or if you use java
textView.setId(R.id.myId)
and in your code :
((TextView)listView.getChildAt(i).findViewById(R.id.myId)).setTextColor(ContextCompat.getColor(context, R.color.hint));
If you want to set the Item Background with your Drawble Image you can use
.setBackground(getResources().getDrawable(R.drawable.yourDrawble));
Make one method in your Adapter class to update text color and create one flag in Adapter that is initially false ,Use below method to do this
boolean isChangeColor = false;
String colorCode = "#FFFFFF";
private void updateTextColor(boolean isChangeColor , String colorCode) {
this.isChangeColor=isChangeColor;
this.colorCode=colorCode;
notifyDataSetChanged();
}
And in getView()
if(isChangeColor) {
textView.setTextColor(Color.parseColor(colorCode));
} else {
colorCode = "#FFFFFF";
textView.setTextColor(Color.parseColor(colorCode));
}

PopupDateField Listener

I've got a PopupDateField with a ValueChangeListener.
Is there a way to differentiate if the Event was fired from an .setValue()-Call or an User-Input?
I want the Event to be excuted only if the user changes the value, not if it was changed by program.
I've made a dirty Workaround by removing the ValueChangListener before setting a new Value and adding it afterwards, but I'm thankfull for better solutions...
private static void setDateFieldValue(final PopupDateField dateField, Date value) {
Collection<Property.ValueChangeListener> listeners = (Collection<Property.ValueChangeListener>)dateField.getListeners(Property.ValueChangeEvent.class);
listeners.forEach(new Consumer<Property.ValueChangeListener>() {
#Override
public void accept(Property.ValueChangeListener listener) {
dateField.removeValueChangeListener(listener);
}
});
dateField.setValue(value);
listeners.forEach(new Consumer<Property.ValueChangeListener>() {
#Override
public void accept(Property.ValueChangeListener listener) {
dateField.addValueChangeListener(listener);
}
});
}

Modify context menu in JavaFX WebView

I'm trying to add some options to the context menu in a JavaFX WebView when the user right clicks a link, however I can't figure out how to do it.
I found I could add my own context menu using:
final ContextMenu contextMenu = new ContextMenu();
view.addEventHandler(MouseEvent.MOUSE_PRESSED, new EventHandler<MouseEvent>() {
#Override
public void handle(MouseEvent mouseEvent) {
System.out.println(mouseEvent.getEventType());
if(mouseEvent.isSecondaryButtonDown()) {
System.out.println("Secondary");
contextMenu.show(view, mouseEvent.getSceneX(), mouseEvent.getSceneY());
} else if (mouseEvent.isPrimaryButtonDown()) {
System.out.println("Primary");
} else if (mouseEvent.isMiddleButtonDown()) {
System.out.println("Middle");
}
}
});
javafx.scene.control.MenuItem menuItem1 = new javafx.scene.control.MenuItem("View Source");
menuItem1.setOnAction(new EventHandler<javafx.event.ActionEvent>() {
#Override
public void handle(javafx.event.ActionEvent actionEvent) {
System.out.println("Link: " + rightClickURL);
System.out.println("You clicked view source");
}
});
contextMenu.getItems().add(menuItem1);
Unfortunately, when I do that both menus appear:
If I use view.setContextMenuEnabled(false); then the default menu disappears. Unfortunately doing that also prevents me from detecting which link was right clicked. Here is the code I'm using for that:
engine.getLoadWorker().stateProperty().addListener(new ChangeListener<Worker.State>() {
#Override
public void changed(ObservableValue ov, Worker.State oldState, Worker.State newState) {
if (newState == Worker.State.SUCCEEDED) {
EventListener listener = new EventListener() {
#Override
public void handleEvent(org.w3c.dom.events.Event evt) {
String domEventType = evt.getType();
if (domEventType.equals(EVENT_TYPE_CLICK)) {
String href = ((Element)evt.getTarget()).getAttribute("href");
} else if (domEventType.equals(EVENT_TYPE_RIGHT_CLICK)) {
String href = ((Element)evt.getTarget()).getAttribute("href");
rightClickURL = href;
System.out.println(href);
}
}
};
Document doc = engine.getDocument();
NodeList nodeList = doc.getElementsByTagName("a");
for (int i = 0; i < nodeList.getLength(); i++) {
((EventTarget) nodeList.item(i)).addEventListener(EVENT_TYPE_CLICK, listener, false);
((EventTarget) nodeList.item(i)).addEventListener(EVENT_TYPE_RIGHT_CLICK, listener, false);
}
}
}
});
So my question is this: how can I access the default ContextMenu so I can customize it? I've scoured the docs but cannot find any method that allows you to access the default ContextMenu. It seems like there must be a way to do this but I'm stumped as to how.
If customizing the default ContextMenu is not possible, does anyone know how I can show a custom context menu when right clicking a link and also capture which link was clicked? I've been trying to achieve this for days with absolutely no luck...
Currently that's not possible. There is an open issue on JavaFX for that:
https://javafx-jira.kenai.com/browse/RT-20306
If you just want to add a different context menu, then you could do that
webView.setOnMouseClicked(new EventHandler<MouseEvent>() {
#Override
public void handle(MouseEvent mouse) {
if (mouse.getButton() == MouseButton.SECONDARY) {
menu = new ContextMenu();
//add some menu items here
menu.show(this, mouse.getScreenX(), mouse.getScreenY());
} else {
if (menu != null) {
menu.hide();
}
}
}
});

Categories