How to add an image while we drag n drop tree item? - java

I have a tree with number of childs and i want to drag and drop these child items on a vertical panel. Right now its working fine.The only thing i want to know is can we add an image in the place of small icon which comes at the time of dragNdrop.

You need to override the newDragProxy() method of the PickupDragController in order to provide your own proxy widget (say, an image) when the drag starts.
Do note that you also need to use setBehaviorDragProxy(true) to allow dragging proxies instead of the original widgets (i.e., the original widgets will stay in place, and you simply drag a proxy of it, that you can style as you wish).

Related

CodeNameOne - React on Tree expansion or collapse

I'm using a subclass of the Tree class in my project. I'm having two problems. First I would like to change the text of the not-leaf nodes when they expand or collapse. Second when expanding some nodes the tree exceeds the screen and even if scrollable is set to true, I can not scroll all the way to the bottom. To be specific the tree always shows as many nodes as in the beginning. If it begins with 10 nodes, then after expanding I will only be able to scroll the ten top nodes and not the whole tree.
While trying to figure out both points I looked for a callback on expansion/collapse, but it seems to be private. Is there any way to add a listener on expansion/collapse or other way to solve my issues?
Tree should be placed in a non-scrollable hierarchy where it can take responsibility over the scrolling e.g. in the center of a border layout on the Form.
You can override the createNode method of Tree and build any type of Component you want. It can change it's text when it's expanded based on the events and do anything you want.

Java jpanel that works like window's drag n drop icon rearranging

So when you move an icon to a new position windows automatically pushes other icons down the list( i would also like to push them up). I would like to do this with jcomponents in general, jtables in specific.
Also for bonus points if I can keep track of what table is where (i'll need to save their order in xml).
any ideas?
There is no out-of-the-box mechanism for rearranging components via drag and drop, but it is easy enough to do yourself. Here's the basic idea:
1) Use a JLayeredPane as base component, and put your tables on the default layer
2) Add a transparent component on top of that, e.g. a higher layer. This will be your glass pane
3) Install a MouseMotionListener on the glass pane. When you detect a drag that should move a
table, go on with 4. Otherwise, redispatch the event to the underlying component.
4) Provide some kind of visual feedback for the drag, e.g. a semitransparent line to indicate the
drop location. Show the user where the table would appear when he releases the mouse.
5) If the drag indicates that the tables should be rearranged, simply redo the layout on your table panel to position them accordingly.
If you have specific questions regarding one of the steps, feel free to comment.

Assinging different icons to different nodes in a JTree

Is it possible to assign different icons to different nodes in a JTree using DefaultTreeCellRenderer.setOpenIcon()? Thanks.
The same cell renderer instance is used to render all the cells of the tree. The open icon is the little + symbol, or triangle symbol at the left of every tree node which allows to expand it (i.e. see its child nodes). I doubt this is the icon you want to change. It would be rather strange not to use the same one for all the nodes.
If you want to display a custom icon for a specific node, create a subclass of DefaultTreeCellRenderer, override the getTreeCellRendererComponent method, decide which icon to display based on the value passed to the method, and call setIcon.
See http://download.oracle.com/javase/tutorial/uiswing/components/tree.html#display for a similar example (which customized the tooltip, and not the icon, but the idea is the same).

J2ME Form Layout

I'm trying to make a menu like shown below. I have some items that extends CustomItem and consists of an Image and some Text. Now I want to position them as shown, but run into problems doing so - it seems only some minimal layout directives can be used in a Form. Is there a way for custom positioning using the Form class or is there another class I could use?
Make a HorizontalLayout CustomItem that aligns everything that is added to it horizontally.
Details:
Such class should have some kind of array to store all the items, and when there is a request to draw it, it should ask the width of each element inside it, do some calculations and call the items' draw method. Of course, such stuff is rather difficult. You would also have to implement the selection of these items with left/right keys...
It may be slightly better to use Canvas.

Swing: Floating panel next to the selected item in a JComboBox

I've created an app with a small window (the size of a combo box). I need to create a floating panel that sits outside the window, next to the selected item in a JComboBox. (See attached image).
I've been reading about the JComboBox.setRenderer(customRenderer) etc. But was just wondering before I go down this path, whether it is at all possible to render something outside the window. I suspect it is, as the combobox itself manages to render it's popup list outside the window.
I'm very new to Swing, so any advice would be appreciated.
It's not possible with the custom renderer since Swing components are light weight. That is, Java is given a native window and all the component drawing takes place in that window. In your case, that is the JFrame containing the combo box.
What you can do though is create a new undecorated window and set it's location accordingly and draw whatever you want inside it.
EDIT: When Java needs to paint outside it's window bounds (like the case of pop up messages or combo boxes drop downs) if the component falls inside the bounds it uses the swing light weight mechanism. But if the component falls out side the bounds it is automatically substituted with a awt heavy weight component that has it's own native drawing surface outside the active window.
I've implemented similar idea using combobox renderers and tooltips on them. Content of every item's tooltip can be customized and rendered using HTML. Location of the tooltip can be set outside of the item itself thus creating design very similar to the one presented in your question.
Here is the starting point for you:
http://www.java2s.com/Code/Java/Swing-Components/ToolTipComboBoxExample.htm

Categories