java - Scrollable area without scrollbars -
i need create area on 1 apply scrollbar, has scroll horizontally (the contents view larger logical area), have use special controls placed left , right control in order scroll.
i have thougth using absolute values (according logical view , subtract offset. thus, controls right offset placed negative x- values , discarded. controls x values above width discarded.
is valid approach?
best regards soeren
you can can create jscrollpane
on component
(your larger logical area) , remove scrollbars.
you can add buttons scroll left , right. when clicked these buttons should move view of scrollpane. done setting absolute position of view. can make relative first getting absolute position of view , incrementing/decrementing , setting again.
here's class shows scrollable window of larger image.
public class viewscroller { public viewscroller() { jframe frame = new jframe("viewscroller"); final imageicon image = new imageicon("path\\to\\my\\image"); jlabel label = new jlabel(image); final jscrollpane scrollpane = new jscrollpane(label); scrollpane.sethorizontalscrollbarpolicy(jscrollpane.horizontal_scrollbar_never); scrollpane.setverticalscrollbarpolicy(jscrollpane.vertical_scrollbar_never); jbutton left = new jbutton("<"); left.addactionlistener(new actionlistener() { @override public void actionperformed(actionevent e) { point p = scrollpane.getviewport().getviewposition(); p.x = p.x < 10 ? 0 : p.x - 10; scrollpane.getviewport().setviewposition(p); } }); jbutton right = new jbutton(">"); right.addactionlistener(new actionlistener() { @override public void actionperformed(actionevent e) { point p = scrollpane.getviewport().getviewposition(); int offset = p.x + scrollpane.getviewport().getwidth(); p.x = offset + 10 > image.geticonwidth() ? p.x : p.x + 10; scrollpane.getviewport().setviewposition(p); } }); frame.add(right, borderlayout.east); frame.add(left, borderlayout.west); frame.add(scrollpane, borderlayout.center); frame.setdefaultcloseoperation(jframe.exit_on_close); frame.setsize(400, 400); frame.setvisible(true); } }
Comments
Post a Comment