Java Swing Scrolling By Dragging the Mouse -
i trying create hand scroller scroll drag mouse across jpanel. far cannot view change. here code:
import java.awt.*; import java.awt.event.*; import javax.swing.*; public class handscroller extends jframe { public static void main(string[] args) { new handscroller(); } public handscroller() { setdefaultcloseoperation(exit_on_close); final jpanel background = new jpanel(); background.add(new jlabel("hand")); background.add(new jlabel("scroller")); background.add(new jlabel("test")); background.add(new jlabel("click")); background.add(new jlabel("to")); background.add(new jlabel("scroll")); final jscrollpane scrollpane = new jscrollpane(background); mouseadapter mouseadapter = new mouseadapter() { @override public void mousepressed(mouseevent e) { jviewport viewport = scrollpane.getviewport(); point vpp = viewport.getviewposition(); vpp.translate(10, 10); background.scrollrecttovisible(new rectangle(vpp, viewport.getsize())); } }; scrollpane.getviewport().addmouselistener(mouseadapter); scrollpane.getviewport().addmousemotionlistener(mouseadapter); setcontentpane(scrollpane); pack(); setlocationrelativeto(null); setvisible(true); } }
i think move view 10 in x , y directions, not doing @ all. there more should doing?
thanks.
your code work. simply, there nothing scroll, window large enough (actually, pack()
has caused jframe resize to fit preferred size , layouts of subcomponents)
remove pack();
, replace line with, say, setsize(60,100);
see effect.
Comments
Post a Comment