Drag and drop with an image - java

I need to create a drag and drop system in swing where an image of the thing being dragged is attached to the cursor during the drag. In theory this is achieveable with
public Icon TransferHandler.getVisualRepresentation(Transferable t)
but there appears to be a long standing bug (here) that means this method is never called. I know I can do it by implementing my own DnD system with DragSource etc., but does anyone know of an easier workround that will get me what I need?

The method TransferHandler.getVisualRepresentation wasn't supported in java 1.4, I'm not sure if or when it was fixed. To test whether it works in a current version you could adapt this example

In the end I used the old style drag-and-drop to implement what I wanted. However I have no reason to think abrightwell's solution wouldn't work just as well - this was just the best way at the time.

You could Try putting the image on a Jlabel (in the draggesture recognizer)and set its bounds, in the droptargetListener dragover method. Alternately, hows about implementing a Mouse listener (I've not tested this latter method).

I have used the "work around" suggested towards the bottom of the bug report you have listed. It worked well enough for me. Granted I was using this with Mac OS X so I have no idea whether Winderz will support it. It would be nice if they would at least fix it to work like they intended and simply document where it will and won't work... oh well. Good Luck.

Related

Java, Swing, Netbeans: how to create an interface similar to the iPhone's SMS screen?

I'm looking to create an interface similar to that of the iPhone's SMS screen. More specifically I'm looking to replicate the "bubbles" coming from each side of the page which contain the messages, as shown here http://www.bidslammer.com/images/iphone_shot1.png .
I do also want to recreate the date and time above the bubbles like you can see in that image. I need to be able to do this by code because its use will be to display the messages that it receives over my socket connection, and show the messages I send over the socket.
I'm really new to Java, and even newer to Swing, so I'm looking for some pointers on how I should go about this.
Can anyone offer my any suggestions about how I would go about doing this? I'm not looking for someone to do the work for me, just a few pointers, perhaps some things I should learn how to use/do and perhaps a helpful tutorial or two.
Google for "swing tutorial" gives a lot of tutorial links, so just pick one. When I was learning Swing I used original Java Swing tutorial.
As for some pointers, I think good idea is to use images to represent the bubbles - that will be the easiest. Inherit some basic component like JLabel or JPanel and override a drawing method - do a custom drawing. First draw the bubble image and then the text over it. This may help with image drawing.
Generally with custom component drawing you use Graphics class, which provides a lot of useful drawing methods.
The sticky notes demo might get you started in the right direction.
Even though that demo is a NetBeans platform module, the sticky note itself is a pure Swing component and should be usable without the platform.

What is the best way to make clickable text in java?

This is for an application so I don't want a hyperlink. I first tried using a Jbutton without all of border/background stuff and then hooking up an actionListener to it but I couldn't get it to the point where I thought it looked nice. I also tried using a JLabel and hooking up a mouse listener to that but I also couldn't get it to look right.
Basically I would like a way using swing to make a button exactly like a url link in an application. What is the standard way of doing this?
but I couldn't get it to the point where I thought it looked nice
You might want to go into greater detail on just what "looked nice" means. I can see you solving this by either a JButton or a JLabel, but the key is perhaps not to look for another solution but to play with the settings of the button or the label til they look nice. If you can't find a nice solution, then post your code (an SSCCE would work best of all) and perhaps we can help you.
that isn't answer to your question but are you tried to add ButtonModel to your JButton example here
It is a rather heavy hammer to use, but SwingX has a JXHyperLink control that is probably exactly what you want. The source is at http://java.net/projects/swingx/sources/svn/content/trunk/swingx-core/src/main/java/org/jdesktop/swingx/JXHyperlink.java?rev=4027 and you can see an article about it at http://www.javalobby.org/java/forums/t18617.html.
It is old, but SwingX continues to do good things.
It's you're trying to make a desktop application which looks like HTML inside a browser, you might try using some of the richer Swing text components in a read-only mode. You could use a mouse-listener to map X/Y clicks to a particular character of text, and then cause an action to occur on that basis.

+Android Webview - how to autoscroll a page?

I'd like to open a html-page in a webview and make it auto-scroll downwards automatically in accordance with an interval set through a timer.
Although below example is for desktop, this link should give you an idea what I want to achieve (enable autoscrolling in the upper right part of the page):
example
First I thought about opening a webpage and then use some kind of code which would simulate/trigger DPAD-down (or Arrow-down). In Windows Mobile, I believe there was something called SendKeys but I couldn't find something similar in the Android-SDK (except for a test-SDK which I doubt I could use for publishing my app in AndroidMarket) so I guess the way mentioned above is not possible.
Another solution could perhaps be a java-script but the webpages are not created by me so I can't insert any anchor.
However, I noted that Webview has some methods called PageDown (and PageUp) which could also be a solution but I am afraid a PageDown would be too much for scrolling the way I want. I want the scrolling to be slow and smooth, more like a line at a time.
Do you have any ideas how I could implement this? I'd really appreciate your help.
You could try using,
View.scrollBy(int x, int y) to scroll the WebView.Use:
computeVerticalScrollOffset(),
computeVerticalScrollExtent() computeVerticalScrollRange() to calculate a maximum y value.

Programmatically Scroll an SWT Table horizontally

Similar question, but not exactly the same.
table.showColumn() is helpful, but the scrolling only has the granularity of the column width. But I want a more precise control of the scroll location.
Consider the following use case. I have two tables that I know are of the same width and have the same column widths. And I want to implement some kind of a scroll synchronizer so that when the user scrolls one table (horizontally), the other table scrolls to the same location.
EDIT:
On the Eclipse forum there seems to be the same question and some working ideas, but no resolution.
EDIT:
I discovered this behavior on Windows
The method setOrigin(x,y) in ScrolledComposite should help:
Scrolls the content so that the specified point in the content is in the top left corner. If no content has been set, nothing will occur. Negative values will be ignored. Values greater than the maximum scroll distance will result in scrolling to the end of the scrollbar.
I'm afraid I can't get a satisfying result in Windows Vista/7 either, but felt I was getting close.
I believe you will have to grab the Table's horizontal scroll bar by using table.getHorizontalBar(). Then, you can specify scrollbar.setSelection() to make it move to a specified position.
This is where I get stuck. Somehow, you have to notify either the Table or the ScrollBar that the value has changed. I've tried everything from update() to notifyListener(), but no dice. It seems that it is merely a matter of laying out, but layout() doesn't have any effect, either.
Incidentally, bear in mind that the ScrollBar's parent (type Scrollable) is not a ScrolledComposite like I had expected, but is actually the Table itself.
I hope this gives you some ideas and helps you find a solution. If so, please let me know, since it's bugging me now, too!
Having poked around in the ScrollBar source a bit, it looks like there are numerous (windows) bugs that have been overcome at one point or another. This may be another, though you didn't mention your OS, nor did Paul, so it's hard to tell.
All the "change the scroll bar position" functions end up calling SetScrollInfo which is package private. I suspect that the intention is for this to actually update things the way you want.
None of that solves your problem. Thankfully the same source also hints at a solution:
Slider.
You'd have to reimplement all the scrollbar behavior within slider, but once done, that should give you the control (har) you want. Hopefully. OTOH, you may run into exactly the same OS bug (if bug it be), and be back at square one.
At THAT point, you definitely register it as an Eclipse UI bug.
PS: Have you searched the Eclipse Bugzilla for anything similar? Poking around a bit turned up this bug related to the scroll bar being out of sync with a tree view in the Navigator (on PC Linux-Motif, but working fine in Windows). I suspect you'll find similar issues if you dig a bit more.

Creating a left-hand margin for a JEditorPane text box

Edit - in short, what I'm looking for is a reliable way to add a left margin to a JEditorPane (read on for the issues I'm having if you'd like).
I am trying to style some text in a JEditorPane, and have been fairly successful. The only problem I'm facing is that it seems impossible to create a margin.
Basically, I've extended PlainView and overridden the drawUnselectedText method. For now I just have it coloring the text red and changing the font. I've also overridden the drawSelectedText method to color the text.
This works consistently, whether text is selected or not, regardless of cursor position etc. - as long as I don't have a margin set.
However if I set a margin for the JEditorPane, the JEditorPane only works most of the time. The only case where it doesn't work is when a selection is made starting at the left-most character of any line. In that case, the margin is simply ignored and the selected text appears to the far left of the JEditorPane.
I know this is a pretty specific issue that most other people probably haven't experienced, and I have found nothing on the net about this, but I'm hoping someone here will have a solution.
Any ideas? Even anything that would allow me to set a margin using some other method than what I'm doing would be extremely helpful.
I gave up on finding an elegant solution to this problem. Basically I created a hack which tests whether or not the conditions exist where the bug will occur (selection from left, etc), and if they do it adjusts the rendering of the text.
I really don't like hacks as they are often bad for portability and maintainability, but this seems to work, so I suppose I'll abstract it away and hope it doesn't break anything.
In any case, it's working which is the key point. I didn't receive any answers, but I'm sure there are some people who at least tried to think of something, so thanks :)
Btw - if someone can come up with an elegant solution I will still accept that instead of my own answer (I can't anyways for two days).
Edit: This is one reason I like Java. Hacks are less likely to cause portability issues - even though this is related to rendering (it works through interaction with a Graphics object), it should still work on any OS.

Categories