Java SWT: How to indicate a menu item is selected - java

Using SWT, what is the common way to indicate that a menu item (from a taskbar menu) is the currently active selection? Checkmark? Bold? How is this done with code?

Use the CHECK style during instantiation:
MenuItem menuItem = new MenuItem(menu, SWT.CHECK);
Use getSelection to check status:
boolean isSelected = menuItem.getSelection();

org.eclipse.swt.widgets.MenuItem setSelection(true) / getSelection()
The style of the selection depends on the style of the menu item: CHECK, CASCADE, PUSH, RADIO, SEPARATOR, as in:
(source: developpez.com)
(source: developpez.com)

MenuItem.getSelection()

Related

How to toggle "over" style on a button in LibGDX?

I want to create a menu using TextButton. The menu should be accessible by mouse and keyboard. For the correct hovering behaviour, I set the style as usual:
TextButton.TextButtonStyle textButtonStyle = new TextButton.TextButtonStyle();
textButtonStyle.up = skin.newDrawable("background", Color.GRAY);
textButtonStyle.down = skin.newDrawable("background", Color.DARK_GRAY);
textButtonStyle.checked = skin.newDrawable("background", Color.DARK_GRAY);
textButtonStyle.over = skin.newDrawable("background", Color.LIGHT_GRAY);
textButtonStyle.font = skin.getFont("default");
This works fine with mouse (the over style is toggled when the mouse is hovering). However, I want to be able to select a menu item by keyboard. The "currently selected" menu item should be selectable using the arrow keys and enter should confirm the item.
The keyboard interaction is not an issue here, but setting the "highlighted" mode on the button. I imagine selecting a button using the keyboard should behave the same as hovering over it with a mouse. But it looks like there is no way of "toggling" the state of the button using the API. There is an isOver() method, but no setOver() method.
Any ideas? Do I really have to create two styles and switch them out with setStyle()? I would consider that ugly..
Create a custom class for your actor and override your isOver method to return isFocused || super.isOver()
By the way, you might take a look at https://github.com/MrStahlfelge/gdx-controllerutils/wiki/Button-operable-Scene2d for not reinventing the wheel.
If you wish to programmatically make the button think that it has been hovered upon, send an InputEvent with type enter which signifies that the mouse has entered onto the button:
InputEvent event = new InputEvent();
event.setType(InputEvent.Type.enter);
event.setPointer(-1);
button.fire(event);
Use the exit InputEvent for defocusing it:
InputEvent event = new InputEvent();
event.setType(InputEvent.Type.exit);
event.setPointer(-1);
button.fire(event);
So basically, the easiest what you can do is just to retrieve the desired button style right from your skin and apply this to the button like this:
Skin skin = getYourSkin(); // or even button.getSkin() I believe should also work
button.setStyle(skin.get(styleName, ButtonStyle.class));
This way you don't need to create a style but you can just get it from your skin definition

How can i disable/remove context menu from combobox?

I have created an editable JComboBox. I want to add specific context menu but default context menu appears when right click.
Is there a way to remove it?
You can set the popup menu of the editor of the combo box by using:
ComboBoxEditor editor = comboBox.getEditor();
JTextField textField = (JTextField)editor.getEditorComponent();
textField.setComponentPopupMenu(...);

Action Bar Title Not Updating onNavigationItemSelected

I am trying to learn using Android Studio's built in templates. I have the Navigation Drawer variant selected currently, and I am trying to use the onNavigationItemSelected method determine the supportActionBar title using the following code:
public boolean onNavigationItemSelected(MenuItem item) {
// Handle navigation view item clicks here.
int id = item.getItemId();
getSupportActionBar().setTitle(item.getTitle()); ...
The issue is that once I start loading fragments, the title isn't updating whenever I select a new navigation item. I have checked to see if the method is firing every time an item in the drawer is selected, and it does, so I can't tell why this isn't updating.
Any help would be greatly appreciated!

Java plugin pop-up menu

I have a plugin which adds a view. This view contains a table which contains on each row some informations. I would like to have a popup menu when I press the right mouse click.
How can I add the extension org.eclipse.ui.menus and after creating the menuContribution to see it in the view ?
In your ViewPart use this code:
MenuManager contextMenu = new MenuManager();
contextMenu.setRemoveAllWhenShown(true);
getSite().registerContextMenu(contextMenu, viewer);
Control control = viewer.getControl();
Menu menu = contextMenu.createContextMenu(control);
control.setMenu(menu);
where viewer is your TableViewer.
The context menu this creates has the same id as your view so you contribute to it with:
<menuContribution
locationURI="popup:your.view.id">
....
</menuContribution>

Mouse Click event

I am using RCP with eclipse 3.6 and java 6.
The user needs to click with right mouse buttom then opened a menu where he makes a choice.
Which mouse event is that?
How to fill the menu with choices?.
Regards,
Haythem
Have a look at this article about RCP and eclipse 3.6. The section that I've linked to describes how to create a context menu (for a table), that will pop up when right clicking.
What I need is how to create mouseeventlistener from a menu and menuitem
Sectionstop in my code is the composite where the mouselistener will be added
Menu menu = new Menu (parent.getShell(), SWT.POP_UP);
MenuItem item = new MenuItem (menu, SWT.PUSH);
item.setText("Text 1");
MenuItem item2 = new MenuItem (menu, SWT.PUSH);
item2.setText("text 2");
sectionStop.setMenu (menu);
Since you are on your RCP, the basic question is where does the user right clicks. Is it on your view/editor or on an object that you contributed to some viewers? A better If you then you should look into contributing via the proper extension points. Either org.eclipse.ui.popupMenus or org.eclipse.ui.menus with locationURI "popup:org.eclipse.ui.popup.any"

Categories

Resources