tags:

views:

400

answers:

4

I can't seem to figure out how to make a resize drag handle in Java Swing. If you're not sure what I mean, open up the "Ask Question" page on this website and look at the bottom of the text area. It has a bar which you can drag in order to change the size of the area. I'm looking for a way to do this in Java Swing. I don't even know where to look at this point.

Any help would be appreciated.

A: 

I haven't done it either, but since you're just looking for a start, I'll give it a shot.

I would put a JComponent of some sort (possibly a custom subclass) at the spot where you want the handle to appear (I'm assuming you can manipulate layout managers well enough to do that part). Then, I would add a MouseListener and a MouseMotionListener -- probably the same class, a subclass of MouseAdapter -- to the component. When a click and drag is detected, I would resize the component that the handle is attached to.

Does that help any?

Michael Myers
+6  A: 

In my opinion, the drag handle that's used on the Stack Overflow posting form most closely resembles that of a vertical JSplitPane, both in appearance and functionality.

htw
Exactly what I was looking for. Thanks.
Evan Fosmark
A: 

This is just an idea where you may start looking.

In swing, the gui components do use a layout manager ( LayoutManagers ) whose are responsible to place each graphic element in its place.

They are bit tricky, because the logic to layout the elements vary from LayoutManager to another, most of the times you have to combine them.

LayoutManagers usually query the component they are laying out to know what is the minimum size, the maximum size and the preferred size. Some lym completely ignore this information, some of them use it partially.

Well the suggestion would be to use a layout manager that honors the preferred size ( or the maximum ) and then add a mouse listener over other element. When then the element is dragged you may programaticlly increase/decrease the max/min/preff size of your component ( most likely a JTextArea ) and let the LYM resize the visual apparence of the component.

So it would look like this pseudo:

 public App { 

      private JComponent container;
      private JComponent myTarget;
      private JComponent dragComponent;


      public void someWhere() { 
              container.setLayoutManager( someLayoutManager() );
              container.add( myTarget );
              container.add( dragComponent );

              // warn: not actual method names, I don't remember them
               dragComponent.addMouseListener( new MouseAdapter() {
                    public void mouseDragged( MouseEvent me ) { 
                          Point point me.getPoint();
                          int h = myTarget.getHeight();
                          int w = myTarget.getWidth();
                          // Do some math to know if you have to add or subtract. 
                          int movement = // -20, +20, whatever.
                          myTarget.setMaximumSize( h + ( movement ) , w ); 
                    }
               } );
       }
 }

That's just a pseudo code, so don't shoot me.

Again the idea is to find the right layoutmanager and add a mouse listener to know where the component is being dragged. Each time the component is dragged you change the size of the target component and let the LayoutManager reshape the component.

I hope this helps.

p.s. Swing also have drag/drop support, see if it helps.

OscarRyz
Oooorrrr... you can use a JSplitPane => http://bit.ly/l8doc hehehe
OscarRyz
A: 

As another person answered already, what you are trying to do simply requires a JSplitPane set to horizontal alignment. You can adjust the split bar width to the pixel size you wish.