java - Prevent scrollbars disappearing when exiting component inside the JScrollPane -
i made jpanel contains jscrollpane surrounds jtextarea. want hide scrollbars when mouse exits jscrollpane.
if set mouselistener on jscrollpane, nothing happens. if set on jtextarea scrollbars hide/unhide correctly, cannot click them because outside bounds of jtextarea.\
how can prevent scrollbars disappearing when try click them?
code example 1. hiding , unhiding + changing transparency works.
problem: cannot click scrollbars, disappear because exit jtextarea.
public class transparenttextarea extends jtextarea { private static final color light_transparent = new color(0, 0, 0, 150); private static final color heavy_transparent = new color(0, 0, 0, 50); public transparenttextarea(final gamemodel model) { setopaque(false); setforeground(color.white); setbackground(heavy_transparent); seteditable(false); addmouselistener(new mouselistener() { @override public void mousereleased(mouseevent e) { // todo auto-generated method stub } @override public void mousepressed(mouseevent e) { // todo auto-generated method stub } @override public void mouseexited(mouseevent e) { model.setlogactive(false); setbackground(heavy_transparent); } @override public void mouseentered(mouseevent e) { model.setlogactive(true); setbackground(light_transparent); } @override public void mouseclicked(mouseevent e) { // todo auto-generated method stub } }); } } public class logger extends jscrollpane implements panelobserver, changelistener { private gamemodel model; public logger(final gamemodel model) { super(new transparenttextarea(model)); this.model = model; setopaque(false); getviewport().setopaque(false); setborder(borderfactory.createemptyborder()); setviewportborder(borderfactory.createemptyborder()); model.addpanelobserver(this); model.addchangelistener(this); } @override public void panelresized() { float x = model.getpanelwidth() * 0.01f; float y = model.getpanelheight() * 0.70f; float width = model.getpanelwidth() * 0.30f; float height = model.getpanelheight() * 0.28f; setbounds((int) x, (int) y, (int) width, (int) height); } @override public void statechanged(changeevent e) { if (model.islogactive()) { setverticalscrollbarpolicy(jscrollpane.vertical_scrollbar_as_needed); sethorizontalscrollbarpolicy(jscrollpane.horizontal_scrollbar_as_needed); } else { setverticalscrollbarpolicy(jscrollpane.vertical_scrollbar_never); sethorizontalscrollbarpolicy(jscrollpane.horizontal_scrollbar_never); } } }
code example 2.
problem tried fix creating panel around , using mouseexited, not work since childs consume it.
public class logger extends jpanel implements panelobserver, mouselistener { private static final color light_transparent = new color(0, 0, 0, 150); private static final color heavy_transparent = new color(0, 0, 0, 50); private gamemodel model; private jtextarea textarea; private jscrollpane scrollpane; public logger(gamemodel model) { super(null); this.model = model; setopaque(false); textarea = new jtextarea(); textarea.setopaque(false); textarea.setforeground(color.white); textarea.setbackground(heavy_transparent); textarea.seteditable(false); scrollpane = new jscrollpane(textarea); scrollpane.setopaque(false); scrollpane.getviewport().setopaque(false); scrollpane.setborder(borderfactory.createemptyborder()); scrollpane.setviewportborder(borderfactory.createemptyborder()); add(scrollpane); model.addpanelobserver(this); addmouselistener(this); } @override public void panelresized() { float x = model.getpanelwidth() * 0.01f; float y = model.getpanelheight() * 0.70f; float width = model.getpanelwidth() * 0.30f; float height = model.getpanelheight() * 0.28f; setbounds((int) x, (int) y, (int) width, (int) height); scrollpane.setbounds(0, 0, (int) width, (int) height); } @override public void mouseclicked(mouseevent e) {} @override public void mousepressed(mouseevent e) {} @override public void mousereleased(mouseevent e) {} @override public void mouseentered(mouseevent e) { scrollpane.setverticalscrollbarpolicy(jscrollpane.vertical_scrollbar_as_needed); scrollpane.sethorizontalscrollbarpolicy(jscrollpane.horizontal_scrollbar_as_needed); textarea.setbackground(light_transparent); } @override public void mouseexited(mouseevent e) { scrollpane.setverticalscrollbarpolicy(jscrollpane.vertical_scrollbar_never); scrollpane.sethorizontalscrollbarpolicy(jscrollpane.horizontal_scrollbar_never); textarea.setbackground(heavy_transparent); } }
just tried , works fine:
textarea.addmousemotionlistener(new mousemotionadapter() { @override public void mousemoved(mouseevent arg0) { scrollpane.setverticalscrollbarpolicy(jscrollpane.vertical_scrollbar_as_needed); scrollpane.sethorizontalscrollbarpolicy(jscrollpane.horizontal_scrollbar_as_needed); } }); scrollpane.addmouselistener(new mouseadapter() { @override public void mouseexited(mouseevent e) { scrollpane.setverticalscrollbarpolicy(jscrollpane.vertical_scrollbar_never); scrollpane.sethorizontalscrollbarpolicy(jscrollpane.horizontal_scrollbar_never); } });
the idea long mouse moving within textarea, scrollbar visible , works fine. moves outside scrollpane it's not visible anymore.
Comments
Post a Comment