swing - Java JScrollBar Design -
i want customize jscrollbar design. use mac develop app eclipse. tried scrollpane.getverticalscrollbar().setbackground(color.black);
nothing happen.
my code:
scrollpane = new jscrollpane(scriptview); scrollpane.setborder(borderfactory.createemptyborder()); scrollpane.getverticalscrollbar().setunitincrement(6); window.getcontentpane().add(scrollpane);
the object scriptview
class jeditorpane
.
how should look:
thanks every help.
i guess looking transparent scrollbar.
this presented idea(not tested code):
import java.awt.*; import javax.swing.*; import javax.swing.border.*; import javax.swing.plaf.basic.*; public class translucentscrollbartest { public jcomponent makeui() { jtextarea cmp = new jtextarea(); string str = "1234567890abcdefghijklmnopqrstuvwxyz"; for(int i=0; i<20; i++) { cmp.append(str+str+"\n"); } cmp.setforeground(color.white); cmp.setbackground(color.black); cmp.setopaque(true); jscrollpane scrollpane = new jscrollpane( cmp, scrollpaneconstants.vertical_scrollbar_always, scrollpaneconstants.horizontal_scrollbar_never); scrollpane.setcomponentzorder(scrollpane.getverticalscrollbar(), 0); scrollpane.setcomponentzorder(scrollpane.getviewport(), 1); scrollpane.getverticalscrollbar().setopaque(false); scrollpane.setlayout(new scrollpanelayout() { @override public void layoutcontainer(container parent) { jscrollpane scrollpane = (jscrollpane)parent; rectangle availr = scrollpane.getbounds(); availr.x = availr.y = 0; insets insets = parent.getinsets(); availr.x = insets.left; availr.y = insets.top; availr.width -= insets.left + insets.right; availr.height -= insets.top + insets.bottom; rectangle vsbr = new rectangle(); vsbr.width = 12; vsbr.height = availr.height; vsbr.x = availr.x + availr.width - vsbr.width; vsbr.y = availr.y; if(viewport != null) { viewport.setbounds(availr); } if(vsb != null) { vsb.setvisible(true); vsb.setbounds(vsbr); } } }); scrollpane.getverticalscrollbar().setui(new basicscrollbarui() { private final dimension d = new dimension(); @override protected jbutton createdecreasebutton(int orientation) { return new jbutton() { @override public dimension getpreferredsize() { return d; } }; } @override protected jbutton createincreasebutton(int orientation) { return new jbutton() { @override public dimension getpreferredsize() { return d; } }; } @override protected void painttrack(graphics g, jcomponent c, rectangle r) {} @override protected void paintthumb(graphics g, jcomponent c, rectangle r) { graphics2d g2 = (graphics2d)g.create(); g2.setrenderinghint(renderinghints.key_antialiasing, renderinghints.value_antialias_on); color color = null; jscrollbar sb = (jscrollbar)c; if(!sb.isenabled() || r.width>r.height) { return; }else if(isdragging) { color = new color(200,200,100,200); }else if(isthumbrollover()) { color = new color(255,255,100,200); }else { color = new color(220,220,200,200); } g2.setpaint(color); g2.fillroundrect(r.x,r.y,r.width,r.height,10,10); g2.setpaint(color.white); g2.drawroundrect(r.x,r.y,r.width,r.height,10,10); g2.dispose(); } @override protected void setthumbbounds(int x, int y, int width, int height) { super.setthumbbounds(x, y, width, height); scrollbar.repaint(); } }); return scrollpane; } public static void main(string[] args) { eventqueue.invokelater(new runnable() { @override public void run() { createandshowgui(); } }); } public static void createandshowgui() { jframe f = new jframe(); f.setdefaultcloseoperation(windowconstants.exit_on_close); f.getcontentpane().add(new translucentscrollbartest().makeui()); f.setsize(320, 240); f.setlocationrelativeto(null); f.setvisible(true); } }
Comments
Post a Comment