I've been trying to add a contextmenu to a marker, but I can't figure out how to get the contextmenu to appear.
marker.addEventListener("rightclick", new MapMouseEvent() {
#Override
public void onEvent(MouseEvent event) {
contextMenu.show(
marker, marker.getPosition().getLat(),
marker.getPosition().getLng()
);
}
});
I tried to cast marker as Node, but that didn't work, help?
To display a popup menu on JxMaps you have to do the next actions:
Switch JxMaps to the LIGHTWEIGHT mode (). In the HEAVYWEIGHT mode, the popup menu can be displayed under the map.
Add the code that shows the PopupMenu to "click" the event handler.
Please take a look at the following example:
JPopupMenu popup = new JPopupMenu();
popup.add(new JMenuItem("Test"));
MapView mapView = new MapView(new MapViewOptions(MapComponentType.LIGHTWEIGHT));
mapView.setOnMapReadyHandler(new MapReadyHandler() {
#Override
public void onMapReady(MapStatus status) {
final Map map = mapView.getMap();
map.setCenter(new LatLng(35.91466, 10.312499));
map.setZoom(2.0);
map.addEventListener("rightclick", new MapEvent() {
#Override
public void onEvent() {
java.awt.Point pos = MouseInfo.getPointerInfo().getLocation();
SwingUtilities.convertPointFromScreen(pos, mapView);
popup.show(mapView, pos.x, pos.y);
}
});
}
});
Related
I am trying to build a simple planner app using JavaFX. My current goal is to be able to:
click on a panel of the calendar (already implemented)
type in a task, hit enter and have it show up as a Label (already implemented)
click on the currently placed labels and remove them from the calendar. (issue)
Step 3 is where I am having most trouble. I am confident that I am setting up my mouse event for the label correctly but when I click on one of the labels it runs the mouse event for the panel. I need a way to override the pane's mouse event so I can use the labels mouse event, but I'm not too sure how to go about that. Any feedback would be great!
this.setOnMouseClicked(e ->
{
TextField field = new TextField();
this.getChildren().add(field);
//sets field as a label
field.setOnKeyPressed(new EventHandler<KeyEvent>() {
#Override
public void handle(KeyEvent key) {
KeyCode k = key.getCode();
if ((k.equals(KeyCode.ENTER))) {
Label lab = new Label(field.getText());
getChildren().add(lab);
getChildren().remove(field);
}
}
});
//removes textfield and label
field.setOnKeyPressed(new EventHandler<KeyEvent>() {
#Override
public void handle(KeyEvent ke) {
KeyCode kc = ke.getCode();
if ((kc.equals(KeyCode.ESCAPE))) {
getChildren().remove(field);
}
}
});
});
if(lab != null)
{
lab.setOnMouseEntered(new EventHandler<MouseEvent>() {
#Override
public void handle(MouseEvent e) {
setStyle("-fx-background-color: #00FF00;");
}
});
}
I just want to drag and drop an label from tree to a widget. I am using the below code and it is not getting dropped as a widget. Can you please help me how to display a widget as a dropped item.
final FlowLayoutContainer dropContainer = new FlowLayoutContainer();
dropContainer.setBorders(true);
DropTarget target = new DropTarget(dropContainer) {
#Override
protected void onDragDrop(DndDropEvent event) {
super.onDragDrop(event);
Widget window = (Widget) event.getData();
dropContainer.add(window);
}
};
target.setGroup("test");
target.setOverStyle("drag-ok");
Instead of creating widget, you can instantiate new window like below and set the properties of it.
final FlowLayoutContainer dropContainer = new FlowLayoutContainer();
dropContainer.setBorders(true);
DropTarget target = new DropTarget(dropContainer) {
#Override
protected void onDragDrop(DndDropEvent event) {
super.onDragDrop(event);
Window window = new Window();
window.setClosable(true);
window.setHeight(200);
window.setWidth(200);
};
target.setGroup("test");
target.setOverStyle("drag-ok");
}
I'm studying the GWT framework and I'm trying to create a custom widget: this widget is a button that contain inside a menu of operations.
if you click in the area of the triangle I want a menu with some options (that are possible operations) and if I click in the other parts of the buttons I want that the operation is the first from the list.
I have put a ListBox widget inside a Button widget and I want 2 different clickListener. The problem is that the listener of the listBox inside the button don't work.
Do you know why?
Following the code
public class MyClass extends Composite {
private ListBox options;
private Button saveButton;
private HorizontalPanel savePanel;
private int indexHandler;
public MyClass(String label, List<String> operationList, final List<Command> commandList) {
savePanel = new HorizontalPanel();
initWidget(savePanel);
options = new ListBox();
saveButton = new Button(label);
for(String operation : operationList){
options.addItem(operation);
}
options.sinkEvents(Event.ONCLICK);
options.addClickHandler(new ClickHandler() {
#Override
public void onClick(ClickEvent event) {
System.out.println("Test1");
indexHandler = options.getSelectedIndex();
commandList.get(indexHandler).execute();
options.setItemSelected(0, true);
}
});
saveButton.getElement().appendChild(options.getElement());
saveButton.addClickHandler(new ClickHandler() {
#Override
public void onClick(ClickEvent event) {
System.out.println("Test2");
commandList.get(0).execute();
options.setItemSelected(0, true);
}
});
savePanel.add(saveButton);
}
}
Don't use ClickHandler on the ListBox. Use ChangeHandler instead.
Also, I don't think you need to mess with Elements here. Simply add your Button widget and your ListBox widget to a container (i.e. some panel). You can add button on top of ListBox, if you want.
I have a JTextField for which I'm hoping to suggest results to match the user's input. I'm displaying these suggestions in a JList contained within a JPopupMenu.
However, when opening the popup menu programmatically via show(Component invoker, int x, int y), the focus is getting taken from the JTextField.
Strangely enough, if I call setVisible(true) instead, the focus is not stolen; but then the JPopupMenu is not attached to any panel, and when minimizing the application whilst the box is open, it stays painted on the window.
I've also tried to reset the focus to the JTextField using requestFocus(), but then I have to restore the caret position using SwingUtilities.invokeLater(), and the invoke later side of things is giving the user a slight margin to mess around with the existing contents / overwrite it / or do other unpredictable things.
The code I've got is effectively:
JTextField field = new JTextField();
JPopupMenu menu = new JPopupMenu();
field.addKeyListener(new KeyAdapter() {
public void keyTyped(KeyEvent e) {
JList list = getAListOfResults();
menu.add(list);
menu.show(field, 0, field.getHeight());
}
});
Can anyone suggest the best avenue to go down to show the JPopupMenu programmatically whilst preserving the focus on the JTextField?
The technical answer is to set the popup's focusable property to false:
popup.setFocusable(false);
The implication is that the textField has to take over all keyboard and mouse-triggered actions that are normally handled by the list itself, sosmething like:
final JList list = new JList(Locale.getAvailableLocales());
final JPopupMenu popup = new JPopupMenu();
popup.add(new JScrollPane(list));
popup.setFocusable(false);
final JTextField field = new JTextField(20);
Action down = new AbstractAction("nextElement") {
#Override
public void actionPerformed(ActionEvent e) {
int next = Math.min(list.getSelectedIndex() + 1,
list.getModel().getSize() - 1);
list.setSelectedIndex(next);
list.ensureIndexIsVisible(next);
}
};
field.getActionMap().put("nextElement", down);
field.getInputMap().put(
KeyStroke.getKeyStroke("DOWN"), "nextElement");
As your context is very similar to a JComboBox, you might consider having a look into the sources of BasicComboBoxUI and BasicComboPopup.
Edit
Just for fun, the following is not answering the focus question :-) Instead, it demonstrates how to use a sortable/filterable JXList to show only the options in the dropdown which correspond to the typed text (here with a starts-with rule)
// instantiate a sortable JXList
final JXList list = new JXList(Locale.getAvailableLocales(), true);
list.setSortOrder(SortOrder.ASCENDING);
final JPopupMenu popup = new JPopupMenu();
popup.add(new JScrollPane(list));
popup.setFocusable(false);
final JTextField field = new JTextField(20);
// instantiate a PatternModel to map text --> pattern
final PatternModel model = new PatternModel();
model.setMatchRule(PatternModel.MATCH_RULE_STARTSWITH);
// listener which to update the list's RowFilter on changes to the model's pattern property
PropertyChangeListener modelListener = new PropertyChangeListener() {
#Override
public void propertyChange(PropertyChangeEvent evt) {
if ("pattern".equals(evt.getPropertyName())) {
updateFilter((Pattern) evt.getNewValue());
}
}
private void updateFilter(Pattern newValue) {
RowFilter<Object, Integer> filter = null;
if (newValue != null) {
filter = RowFilters.regexFilter(newValue);
}
list.setRowFilter(filter);
}
};
model.addPropertyChangeListener(modelListener);
// DocumentListener to update the model's rawtext property on changes to the field
DocumentListener documentListener = new DocumentListener() {
#Override
public void removeUpdate(DocumentEvent e) {
updateAfterDocumentChange();
}
#Override
public void insertUpdate(DocumentEvent e) {
updateAfterDocumentChange();
}
private void updateAfterDocumentChange() {
if (!popup.isVisible()) {
popup.show(field, 0, field.getHeight());
}
model.setRawText(field.getText());
}
#Override
public void changedUpdate(DocumentEvent e) {
}
};
field.getDocument().addDocumentListener(documentListener);
It looks straight forward to me. Add the following
field.requestFocus();
after
menu.add(list);
menu.show(field, 0, field.getHeight());
Of course, you will have to code for when to hide the popup etc based on what is going on with the JTextField.
i.e;
menu.show(field, field.getX(), field.getY()+field.getHeight());
menu.setVisible(true);
field.requestFocus();
You may take a look to JXSearchField, which is part of xswingx
Hiho,
currently I have a working popup menu which appears when I click on a treeview item.
But I want to show different popups for different tree view entries. I don't get a idea how to do so...
Here is my code for creating the menu:
MenuManager menuMgr = new MenuManager("#PopupMenu");
menuMgr.setRemoveAllWhenShown(true);
menuMgr.addMenuListener(new IMenuListener() {
#Override
public void menuAboutToShow(IMenuManager manager) {
Action action = new Action() {
public void run() {
// So something
}
};
action.setText("Set as working file");
manager.add(action);
}
});
Menu menu = menuMgr.createContextMenu(getTree());
getTree().setMenu(menu);
You should propably use a MouseListener on the tree:
final Tree tree = new Tree(parent, ...);
tree.addMouseListener(new MouseAdapter() {
#override
public void mouseDown(MouseEvent me) {
if(tree.getSelection() instanceof MySpecificTreeNode) {
// create menu...
}
}
});
Two ideas. For both you need to listen to selections on the TreeView, because that's the only way to determine which Menu (or special content) you want to show.
Then you could either set the correct menu to the the tree right after you know which one to use or contribute the needed items to the existing menu (that's how it's done in the eclipse framework).