I have a treeView in my wizard application. Whenever i add or remove an object from my model , calling the update or refresh methods works as expected. What i want is , when i press a certain check button, two things may happen: If the new selection is false(unchecked) , i want to remove the treeView items , so they wont show in my UI, and when the new selection is true(checked) , i want to add the previously removed items (i have them stored in my application), so they can show up again. So i added this listener to my button :
oIsAuthorizableResourceButton.addSelectionListener(new SelectionAdapter() {
#Override
public void widgetSelected(SelectionEvent e) {
bIsResourceAuthorizable[intSelectedResourceIndex] = oIsAuthorizableResourceButton.getSelection();
//recursiveSetEnabled(grpPolicySetConfiguration,oIsAuthorizableResourceButton.getSelection());
if(!bIsResourceAuthorizable[intSelectedResourceIndex]){
System.out.println("Remove : " + oSelectedPolicySet.getHasResourceAccessPolicy().size());
oTreeViewer.remove(oSelectedPolicySet.getHasResourceAccessPolicy().toArray());
oTreeViewer.refresh(oSelectedPolicySet);
oTreeViewer.refresh();
}else{
System.out.println("Add : " + oSelectedPolicySet.getHasResourceAccessPolicy().size());
oTreeViewer.add(oSelectedPolicySet, oSelectedPolicySet.getHasResourceAccessPolicy().toArray());
oTreeViewer.refresh(oSelectedPolicySet);
oTreeViewer.refresh();
}
}
Well this code does absolutely nothing.Any help appreciated.
Implement a Viewer Filter, then add and remove it from your viewer: http://www.eclipse.org/articles/Article-TreeViewer/TreeViewerArticle.htm . What it filters, and whether it even cares about a specific property, is your choice.
Related
My task is to enable removing Jlist selected elements when alt is pressed and jlist is clicked. I did this by adding mouse listener to my jlist:
list.addMouseListener(new MouseAdapter(){
public void mouseClicked(MouseEvent e) {
java.util.List selectedItems = list.getSelectedValuesList();
if (e.isAltDown()){
for (Object o : selectedItems){
cm.removeElement(o); //cm is my custom model
}
}
}
});
My issue is that when there are two elements selected and I click the list with alt pressed only nearest element gets selected and is removed then. I have no clue how to remove several elements with this input combination.
The problem is that the mouse click clears all the previous selections and then selects the row you just clicked on. So therefore only that row is deleted.
So instead you should be handling a "right mouse" click and then use the right mouse button only for the deletion of the item.
if (e.isAltDown() && SwingUtilities.isRightMouseButton(e)) {
Or if you really want to do this on a left mouse click then you would probably need to use a ListSelectionListener. Every time the selection changes you would need to use the getSelectedValuesList() method and save the List returned from the method. Then in the MouseListener you would access the saved List instead of getting the currently selected List of items.
I don't like this approach because the logic is now contained in two separate listeners. Although I guess you could create a class that implement both the selection listener and the mouse listener.
This is not a perfect answer. But it solves the issue.
I just tried to see how the selection event works. When he selection happens an Mouse pressed event is triggered and then the Selection happens. So the MouseListeners which are already added to the component are responsible to make the selection. Removing the MouseListeners which are already in place would prevent the selection happen using mouse. So i did this.
MouseListener[] adapters = list.getMouseListeners();
for (int i = 0; i < adapters.length; i++) {
list.removeMouseListener(adapters[i]);
}
Now user will not be able to do the selection using mouse but he will be make the selection using keyboard. So the below would work.
list.addMouseListener(new MouseAdapter() {
#Override
public void mousePressed(MouseEvent e) {
java.util.List selectedItems = list.getSelectedValuesList();
if (e.isAltDown()){
for (Object o : selectedItems){
model.removeElement(o); //cm is my custom model
}
}
}
});
I think the answer given by camickr, should be followed.
I have a AppCompatAutoCompleteTextView , i have also set an adapter on my AppCompatAutoCompleteTextView . But I need to handle an event in which no items(filtered items ) is shown in my AppCompatAutoCompleteTextView .
I just don't know how to handle this specific event i have also read the docs but its not that simple . So kindly help me out
For Eg:
Case 1:
Suppose my string array contains 2 items Apple , Android.
Now whenever the user will type "A" , a drop-down list will appear showing filtered items
In this particular case the drop-down list would have 2 items : Apple and Android
Case 2:
When the user types "Ax" , now the drop-down list will not appear. At this specific event , when there is no SUGGESTION or drop-down list shown . I need to display a toast.
There is a method called onFilterComplete() in AutoCompleteTextView.
//count is the number of values computed by the filter
public void onFilterComplete (int count){
if(count==0){
//your code here
}
}
You should implement your code in the If block, As you want to handle things when the result is empty. ThankYou I hope this is helpful.
I suggest you adding a TextWatcher and check if isPopupShowing() in afterTextChanged like here
autocompleteTextView.addTextChangedListener(new TextWatcher() {
//---overridden beforeTextChanged and onTextChanged here----
#Override
public void afterTextChanged(Editable s) {
if(!autocompleteTextView.isPopupShowing()){
//Handle the event here if no suggestions are available
Toast.makeText(MainActivity.this, "no suggestions available", Toast.LENGTH_SHORT).show();
}
}
I have got ditched in a problem with Menu Contribution and PersistedState. I had no problem before removing the -clearPersistedState flag from the VM args.
Now, the app has a weird behaviour, the menu contribution starts to pile up a menu entry every time the code is executed.
Here it's the guilty snippet enclosed in a Processor:
MDirectMenuItem menuItem = MMenuFactory.INSTANCE.createDirectMenuItem();
menuItem.setLabel("Another Exit");
menuItem.setContributionURI("bundleclass://"
+ "com.telespazio.optsat.wizard/"
+ ExitHandlerWithCheck.class.getName());
if (!menu.getChildren().contains(menuItem))
menu.getChildren().add(menuItem);
The menu items you add to the application model will be persisted, so you need to check if they already exist in the menu. The contains check you currently have does not do this.
You need to check for a match of the label (or the contribution URI, or the id), something like:
List<MMenuElement> children = menu.getChildren();
boolean gotExisting = false;
for (MMenuElement child : children)
{
if ("Another Exit".equals(child.getLabel())
{
gotExisting = true;
break;
}
}
if (!gotExisting)
{
... add to menu
}
I m working with GWT 2.4 on an new application. I made a docklayoutpanel and I inserted a celllist on the west section of it. I need to create an event, every time a user clicks on an element of celllist on the west side of page a specific widget will load at the content of the docklayoutpanel.
Any suggestions?
Thank you
The following example should be self explanatory
// Create a cell to render each value.
TextCell textCell = new TextCell();
// Create a CellList that uses the cell.
CellList<String> cellList = new CellList<String>(textCell);
cellList.setKeyboardSelectionPolicy(KeyboardSelectionPolicy.ENABLED);
// Add a selection model to handle user selection.
final SingleSelectionModel<String> selectionModel = new SingleSelectionModel<String>();
cellList.setSelectionModel(selectionModel);
selectionModel.addSelectionChangeHandler(new SelectionChangeEvent.Handler() {
public void onSelectionChange(SelectionChangeEvent event) {
String selected = selectionModel.getSelectedObject();
if (selected != null) {
Window.alert("You selected: " + selected);
}
}
});
Instead of Window.alert("You selected: " + selected); you will need to change the widget shown on the eastern side of your panel.
This can be done in several ways, one of which is to expose the Dockpanel to the Selection Change Event either by declaring the Panel as a field to the class (not a local Variable in the constructor of the class) or as a final local variable in the constructor.
Another way is to do this by event handling. The eventBus methodology on the MVP design pattern is the proper way to do all thesee here for more information.
I have an ActionListener attached to a JComboBox(uneditable). Once an item from the JComboBox is selected, I have to make the next button in the frame visible.
The skeleton of the code looks like this:
public void actionPerformed(ActionEvent evt)
{
if(evt.getSource()==jComboBox){
if(jComboBox.getSelectedIndex()==-1)
//Display an alert message
else{
nextButton.setVisible(true);
//Do other actions
}
}
}
It is found that actionPerformed is called only when the second, third, fourth (and so on) items are selected. But actionPerformed is not called when the first item is selected the very first time. But if the first item is selected after selecting other items actioPerformed gets called and the code works fine.
This error appears on some systems and doesn't on other systems. Any help in this regard would be appreciated.
Thanks in Advance!!
This is the normal behavour. The ActionEvent is not fired when you reselect the same item. If you want the event to be fired when you create the combo box then your code should be something like:
JComboBox comboBox = new JComboBox(...);
comboBox.setSelectedIndex(-1); // remove automatic selection of first item
comboBox.addActionListener(...);
comboBox.setSelectedIndex(0);
or
JComboBox comboBox = new JComboBox();
comboBox.addActionListener(...);
comboBox.addItem(...);
comboBox.addItem(...);
Seems like you first condition is a little wrong.
If you want to execute certain code if no item is in your JComboBox, you should check content size : jComboBox.getItemCount()==0 instead of jComboBox.getSelectedIndex()==-1, because selected index can depend upon various conditions, while getItemCount() is only 0 when, well, combo box is empty :-)