JAVA WorldWind LayerTree with Radio Buttons - java

WorldWind LayerTree uses checkboxes for default configuration. Is there any way to change checkboxes to radio-buttons?
Screenshot

I could not find any solution for replacing check boxes with radio-buttons. So, I decided to try another approach and it works for me. I catch the property change event of LayerTree and reset the selected nodes other than the newly selected one.
LayerTree layerTree = new LayerTree();
layerTree.addPropertyChangeListener(new PropertyChangeListener(){
#Override
public void propertyChange(PropertyChangeEvent evt) {
for (Iterator<TreeNode> treeNode = layerTree.getModel().getRoot().getChildren().iterator(); treeNode.hasNext(); ) {
LayerTreeNode layerTreeNode = LayerTreeNode.class.cast(treeNode.next());
if(evt.getSource() instanceof LayerTreeNode && evt.getSource() != layerTreeNode)
layerTreeNode.setSelected(false);
}
}
});

Related

Java SWT get bounds of custom element in TreeView

I´ve created a TreeView with a ContentProvider and custom tree elements.
I also have a ISelectionChangedListener added to the TreeView.
I want to add an MouseListener, do detect if an element of the tree is right-clicked and show a popup menu.
If the white area around the tree is clicked, i don´t want to display the popup menu.
The menu is added via Extensions in the plugin.xml.
How can I now evaluate if a tree element is right-clicked, so I can show the popup menu (maybe with visibleWhen in the plugin.xml) ?
I also want to clear the selection, if the right-click is detected in the white area of the TreeView.
Ok, i did not realized that i can still use tree.getItem(...).
So here is my full MouseListener:
treeOPCUA.addMouseListener(new MouseListener()
{
#Override
public void mouseUp(MouseEvent e)
{
if(e.button == 3 && rightMouseClicked == true)
rightMouseClicked = false;
}
#Override
public void mouseDown(MouseEvent e)
{
if(e.button == 3 && rightMouseClicked == false)
rightMouseClicked = true;
if(treeOPCUA.getItem(new Point(e.x, e.y)) == null)
viewer.setSelection(null);
}
#Override
public void mouseDoubleClick(MouseEvent e)
{
viewer.setExpandedState(e.getSource(), true);
}
});
With the boolean variable "rightMouseClicked" I detect in my ISelectionChangedListener if the right mouse is clicked:
if(event.getSelection() instanceof IStructuredSelection && !rightMouseClicked)
I hope this answer helps anyone.

Event handling in Java (JTree + JButton)

private void createEvents()
{
menuFileExit.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent arg0)
{
System.exit(0);
}
});
////// Events on tree selection
jtStoryViewer.addTreeSelectionListener(new TreeSelectionListener()
{
public void valueChanged(TreeSelectionEvent arg0)
{
DefaultMutableTreeNode selection = (DefaultMutableTreeNode) jtStoryViewer.getLastSelectedPathComponent();
Object nodeObject = selection.getUserObject();
////// Checks if selected node is a String (only story title is a string)
if(selection.getUserObject().getClass().getName() == "java.lang.String" )
{
tfTitle.setText(nodeObject.toString());
////// Action listener for Change Button
btnChange.addActionListener(new ActionListener()
{
////// Title text swap
public void actionPerformed(ActionEvent arg0)
{
selection.setUserObject(tfTitle.getText());
((DefaultTreeModel)jtStoryViewer.getModel()).nodeChanged(selection);
}
});
}
///// checks if the object is a chapter object
if(selection.getUserObject().getClass().getName() == "ISW.common.Chapter")
{
Chapter chapter = (Chapter) selection.getUserObject();
tfTitle.setText(chapter.toString());
////// Action listener for Change Button
btnChange.addActionListener(new ActionListener()
{
////// Title text swap
public void actionPerformed(ActionEvent arg0)
{
chapter.setTitle(tfTitle.getText());
((DefaultTreeModel)jtStoryViewer.getModel()).nodeChanged(selection);
}
});
}
}
});
}
I am using JTree to display and modify some objects. I added a TreeSelectionListener to get the object data on selection. For now I want to be able to change the title of an object, it works fine on first selection on the tree , I change the value in the text box and the "Change" button works just fine, but when I move on to next objects, the change button also modifies the value of all previously selected objects.
I guess it is caused due to my improper usage of the ActionListeners but I can't tell for sure and at this point I'm stuck.
Will be grateful for any hints.
Don't keep adding an ActionListener to the btnChange JButton within the TreeSelectionListener#valueChanged method.
This will cause the button to call EVERY ActionListener you have previously
Instead, give the btnChange a single ActionListener, when clicked, can act on the currently selected node (by checking the JTree it self). You could have the TreeSelectionListener#valueChanged method enable or disable the btnChange based on the validity of the selection
Also, if(selection.getUserObject().getClass().getName() == "ISW.common.Chapter") isn't how String comparison is done in Java, instead you should use something more like if("ISW.common.Chapter".equals(selection.getUserObject().getClass().getName()))

JTabbedPane track previous tab selection

I have a class that extends BasicTabbedPaneUI and does some paint component overriding.
I want to be able to add a addMouseListener to the class I use it in to check when the user selects a tab the current tab index and the previous tab index.
NOTE: The user is able to navigate to tabs via the keyboard and not just clicking on a tab and I want to be able to make sure the previous index tracks this. So in the example below preIndex would equal the previous index regardless to whether the user navigated to it via the keyboard or mouse.
Any ideas please?
tabbedPane.addMouseListener(new MouseAdapter() {
#Override
public void mousePressed(MouseEvent e) {
JTabbedPane tabP = (JTabbedPane) e.getSource();
int currIndex = tabP.indexAtLocation(e.getX(), e.getY());
int prevIndex = ?????
}
});
Many thanks!!!!
I would use the change listener instead of mouse listener (it's called in both cases: for mouse and key event triggered tab change). If you cannot determine previously selected tab you can use following approach: save currently selected tab index as client property of the tabbed pane.
private static final String OLD_TAB_INDEX_PROPERTY = "oldTabIdx";
tabbedPane.addChangeListener(new ChangeListener() {
public void stateChanged(ChangeEvent e) {
JTabbedPane tabP = (JTabbedPane) e.getSource();
int currIndex = tabP.getSelectedIndex();
int oldIdx = 0;
Object old = tabP.getClientProperty(OLD_TAB_INDEX_PROPERTY);
if (old instanceof Integer) {
oldIdx = (Integer) old;
}
tabP.putClientProperty(OLD_TAB_INDEX_PROPERTY, currIndex);
// now we can use old and current index
}
});

How to handle the finishing of changing the value in the grid cell in GXT2

I have an app with GXT. In this app there is a grid in which I need to edit values in cells. I create cells in this way:
column = new ColumnConfig();
fieldEditor = new CellEditor(new TextField<String>());
column.setEditor(fieldEditor);
column.setId("value");
column.setHeader("Value");
column.setWidth(650);
configs.add(column);
As you see I use TextField as an editor. so, after I change the value I can press Enter or click in any other place of the window. how to handle this. I tried to handle ButtonPress event and MouseClick event, but this drops an exception or don't do anything.
UPD: NOw I'm trying to implement this in the following way:
valueEditor = new TextField<String>();
KeyListener keyListener = new KeyListener(){
#Override
public void componentKeyUp(ComponentEvent event){
if (event.getKeyCode() == KeyCodes.KEY_ENTER){
if (valueEditor.getValue() != null && !("").equals(valueEditor.getValue())){
Window.alert("Enter Button Pressed");
PropertyItem item;
item = new PropertyItem(grid.getStore().getAt(rowNumber).getName(),
grid.getStore().getAt(rowNumber).getType(), valueEditor.getValue(),
grid.getStore().getAt(rowNumber).isAccepted());
store.update(item);
store.commitChanges();
saveProperties(store, customerId, toRemove);
}
}
}
};
valueEditor.addKeyListener(keyListener);
and what is the problem: it doesn't handle the Enter press in the end of editing, but only handle it after the editing deactivated.
If you want to handle enter event on TextField than you can used following code
TextField<String> field = new TextField<String>();
KeyListener keyListener = new KeyListener(){
#Override
public void componentKeyUp(ComponentEvent event) {
if(event.getKeyCode() == KeyCodes.KEY_ENTER){
//do stuff
}
}
};
field.addKeyListener(keyListener);
column = new ColumnConfig();
fieldEditor = new CellEditor(field);
column.setEditor(fieldEditor);
column.setId("value");
column.setHeader("Value");
column.setWidth(650);
configs.add(column);
if it's not help to you than you can used focus lost event.
hope it will help you.

Java swt treeview popup menu

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).

Categories