I'm using StyledText widget in my SWT app. SWT by default appends "Input method" submenu to the end of existing context menu. Is there any way to remove it?
textBox.addListener(SWT.MenuDetect, new Listener() {
public void handleEvent(Event event)
{
StyledText s = (StyledText)event.widget;
Menu menu = new Menu (s.getShell(), SWT.POP_UP);
MenuItem item = new MenuItem (menu, SWT.PUSH);
item.setText("Click here");
s.setMenu(menu);
menu.setVisible(true);
event.doit = false;
}
});
Yes, you can ask the StyledText for its Menu (getMenu()), find the correct item (getItem(int)) and call dispose() on the item. That will delete it from the menu.
Related
Menu menu = new Menu (editor.getSite().getShell(), SWT.POP_UP);
MenuItem itemA = new MenuItem(menu, SWT.PUSH);
itemA.setText("ItemA"); menu.setVisible(true);
Then you will have enent, On click.
itemA.addListener(SWT.Selection, new Listener() {
public void handleEvent(Event event) {
}
});
I'd like to have a title for my MenuBar, but I'm not finding any methods in the Vaadin API for that. Just a small text to appear on the button which triggers the dropdown (like the user's name), instead of the triangle it shows now. Thanks!
MenuBar menu = new MenuBar();
menu.setWidth("70px");
MenuItem home = menu.addItem("Home", FontAwesome.HOME, new Command() {
public void menuSelected(MenuItem selectedItem) {doSomething();}});
MenuItem settings = menu.addItem("Settings", FontAwesome.COGS, new Command() {
public void menuSelected(MenuItem selectedItem) {doSomething();}});
MenuItem logout = menu.addItem("Sign out", FontAwesome.SIGN_OUT, new Command() {
public void menuSelected(MenuItem selectedItem) {doSomething();}});
If you remove the setWidth you will notice that all three menus (Home, Settings, Sign out) are horizontally aligned which is, I believe, not what you want. What you want to do is more something like this:
MenuBar menu = new MenuBar();
MenuItem rootItem = menu.addItem("User name", null);
rootItem.addItem("Home", FontAwesome.HOME, this::doSomething);
rootItem.addItem("Settings", FontAwesome.COGS, this::doSomething);
rootItem.addItem("Sign out", FontAwesome.SIGN_OUT, this::doSomething);
How to add clickHandler to a gwt checkbox, so that when the checkbox is checked, a listbox is shown and when it is unchecked, the listbox disappears?
Following is what I have so far. When I check the checkbox, the list appears, but when I uncheck it, the listbox does not disappear.
VerticalPanel vPanel = new VerticalPanel();
ListBox list = new listBox();
list.setVisible(false);
vPanel.add(list);
.....
.....
checkBox.addClickHandler(new ClickHandler()
{
public void onClick(ClickEvent event)
{
boolean checked =((CheckBox) event.getSource()).isEnabled();
if(checked==true)
{
list.addItem("a");
list.addItem("b");
list.addItem("c");
list.setVisible(true);
}
else if(checked==false)
{
componentList.setVisible(false);
}
}
});
Thanks so much in advance.
A better way to do it is to do the value changed handler. The user can even use the keyboards to check the checkbox!
CheckBox c = new CheckBox();
c.addValueChangeHandler(new ValueChangeHandler<Boolean>() {
#Override
public void onValueChange(ValueChangeEvent<Boolean> event) {
componentList.setVisible(event.getValue());
}
});
}
edit -
You should add the items in the list outside. And, depending on the default checkbox value,(checked or unchecked), set the component visible or hidden outside itself in the start.
enabled != checked! (use isChecked or getValue)
Otherwise, go with Bhat's code and advises.
I want to show a popup menu below a toolbar button when the user clicks this button. I've read about the SWT.DROP_DOWN style for a ToolItem but this seems very much limited to a simple list of items according to this sample. Instead, I want to show a popup menu with, e.g., checkbox and radio button menu items.
You can make MenuItem with styles SWT.CHECK, SWT.CASCADE, SWT.PUSH, SWT.RADIO, SWT.SEPARATOR
see javadoc..
So you can "hang" swt menu to selection of dropdown on toolbar item like this
public class Test {
private Shell shell;
public Test() {
Display display = new Display();
shell = new Shell(display, SWT.SHELL_TRIM);
shell.setLayout(new FillLayout(SWT.VERTICAL));
shell.setSize(50, 100);
ToolBar toolbar = new ToolBar(shell, SWT.FLAT);
ToolItem itemDrop = new ToolItem(toolbar, SWT.DROP_DOWN);
itemDrop.setText("drop menu");
itemDrop.addSelectionListener(new SelectionAdapter() {
Menu dropMenu = null;
#Override
public void widgetSelected(SelectionEvent e) {
if(dropMenu == null) {
dropMenu = new Menu(shell, SWT.POP_UP);
shell.setMenu(dropMenu);
MenuItem itemCheck = new MenuItem(dropMenu, SWT.CHECK);
itemCheck.setText("checkbox");
MenuItem itemRadio = new MenuItem(dropMenu, SWT.RADIO);
itemRadio.setText("radio1");
MenuItem itemRadio2 = new MenuItem(dropMenu, SWT.RADIO);
itemRadio2.setText("radio2");
}
if (e.detail == SWT.ARROW) {
// Position the menu below and vertically aligned with the the drop down tool button.
final ToolItem toolItem = (ToolItem) e.widget;
final ToolBar toolBar = toolItem.getParent();
Point point = toolBar.toDisplay(new Point(e.x, e.y));
dropMenu.setLocation(point.x, point.y);
dropMenu.setVisible(true);
}
}
});
shell.open();
while(!shell.isDisposed()) {
if(!display.readAndDispatch()) display.sleep();
}
display.dispose();
}
public static void main(String[] args) {
new Test();
}
}
I'm using a RichTextArea in a GWT app. I want to add a context menu to my RichTextArea:
public class MyRichTextArea extends RichTextArea implements HasContextMenuHandlers {
public HandlerRegistration addContextMenuHandler(ContextMenuHandler h) {
return addDomHandler(h, ContextMenuEvent.getType());
}
}
(...)
myRichTextArea.addContextMenuHandler(new ContextMenuHandler() {
public void onContextMenu(ContextMenuEvent event) {
contextMenu.show();
}
});
This works, however, the context menu only appears when I right-click on the border of the RichTextArea. If I right-click into the RichTextArea, e.g. on the contained text, the browser's default context menu is shown.
How can I display my own context menu?
Prevent default context memu:
myRichTextArea.addDomHandler(new ContextMenuHandler() {
#Override public void onContextMenu(ContextMenuEvent event) {
event.preventDefault();
event.stopPropagation();
// do what you want to do instead
}
}, ContextMenuEvent.getType());
I would go after a method that tells you that the rich text area has the focus, like hasfocus, or maybe better, an event listener (addFocusListener) to tell you when the focus is there on a mouse click for the right mouse button?
Does that make sense?