java - Show the line numbers when the JScrollBar is moved -


in linux text editor, kate, there nice functionality when click , drag scroll bar shows current line numbers in view in text component. enter image description here question how can add function in java scroll pane containing jtextarea. component can use show notification?

apparently can jpopupmenu:

enter image description here

i tried class because knew had method show(component, x, y). might possible other classes, or trying implement whatever method yourself.

i put couple of mouse listeners scrollbar , toyed little values x, y in show() call until satisfied position being drawn at.

full code:

import java.awt.dimension; import java.awt.event.mouseadapter; import java.awt.event.mouseevent; import java.awt.event.mousemotionadapter;  import javax.swing.jframe; import javax.swing.jlabel; import javax.swing.jpopupmenu; import javax.swing.jscrollbar; import javax.swing.jscrollpane; import javax.swing.jtextarea; import javax.swing.swingutilities;  public class scrollbartest {     private jscrollpane scrollpane;     private jscrollbar scrollbar;     private jpopupmenu popupmenu;     private jlabel popuplabel;      public static void main(string[] args)     {         swingutilities.invokelater(new runnable() {             public void run()             {                 new scrollbartest();             }         });     }      public scrollbartest()     {         jframe frame = new jframe("test");          popupmenu = new jpopupmenu();         popuplabel = new jlabel();         popupmenu.add(popuplabel);          scrollpane = new jscrollpane(buildtesttextarea());         scrollbar = scrollpane.getverticalscrollbar();         scrollbar.addmousemotionlistener(new popupmousemotionlistener());         scrollbar.addmouselistener(new popupmouselistener());          frame.add(scrollpane);          frame.setsize(new dimension(400, 400));         frame.setlocationrelativeto(null);         frame.setdefaultcloseoperation(jframe.exit_on_close);         frame.setvisible(true);     }      public jtextarea buildtesttextarea()     {         jtextarea textarea = new jtextarea();         stringbuilder builder = new stringbuilder();         (int = 0; < 10000; ++i)         {             builder.append("x");         }         textarea.settext(builder.tostring());         textarea.setlinewrap(true);         return textarea;     }      private class popupmousemotionlistener extends mousemotionadapter     {         @override         public void mousedragged(mouseevent e)         {             double value = scrollbar.getvalue();             double max = scrollbar.getmaximum() - scrollbar.getvisibleamount();             double h = scrollbar.getheight();             popuplabel.settext("" + (int) (100*value/max) + "%");             popupmenu.show(scrollpane, scrollbar.getx() - popupmenu.getwidth() - 2, (int) ((h - popupmenu.getheight())*value/max));         }     }      private class popupmouselistener extends mouseadapter     {         @override         public void mousereleased(mouseevent e)         {             popupmenu.setvisible(false);         }     } } 

Comments

Popular posts from this blog

commonjs - How to write a typescript definition file for a node module that exports a function? -

openid - Okta: Failed to get authorization code through API call -

ios - Change Storyboard View using Seague -