user interface - How to implement draggable tab using Java Swing? -


how implement draggable tab using java swing? instead of static jtabbedpane drag-and-drop tab different position rearrange tabs.

edit: the java tutorials - drag , drop , data transfer.

curses! beaten punch google search. unfortunately it's true there no easy way create draggable tab panes (or other components) in swing. whilst example above complete 1 i've written bit simpler. demonstrate more advanced techniques involved bit clearer. steps are:

  1. detect drag has occurred
  2. draw dragged tab offscreen buffer
  3. track mouse position whilst dragging occurs
  4. draw tab in buffer on top of component.

the above example give want if want understand techniques applied here might better exercise round off edges of example , add features demonstrated above it.

or maybe i'm disappointed because spent time writing solution when 1 existed :p

import java.awt.component; import java.awt.graphics; import java.awt.image; import java.awt.point; import java.awt.rectangle; import java.awt.event.mouseadapter; import java.awt.event.mouseevent; import java.awt.event.mousemotionadapter; import java.awt.image.bufferedimage;  import javax.swing.jbutton; import javax.swing.jframe; import javax.swing.jtabbedpane;   public class draggabletabbedpane extends jtabbedpane {    private boolean dragging = false;   private image tabimage = null;   private point currentmouselocation = null;   private int draggedtabindex = 0;    public draggabletabbedpane() {     super();     addmousemotionlistener(new mousemotionadapter() {       public void mousedragged(mouseevent e) {          if(!dragging) {           // gets tab index based on mouse position           int tabnumber = getui().tabforcoordinate(draggabletabbedpane.this, e.getx(), e.gety());            if(tabnumber >= 0) {             draggedtabindex = tabnumber;             rectangle bounds = getui().gettabbounds(draggabletabbedpane.this, tabnumber);               // paint tabbed pane buffer             image totalimage = new bufferedimage(getwidth(), getheight(), bufferedimage.type_int_argb);             graphics totalgraphics = totalimage.getgraphics();             totalgraphics.setclip(bounds);             // don't double buffered when painting static image.             setdoublebuffered(false);             paintcomponent(totalgraphics);              // paint dragged tab buffer             tabimage = new bufferedimage(bounds.width, bounds.height, bufferedimage.type_int_argb);             graphics graphics = tabimage.getgraphics();             graphics.drawimage(totalimage, 0, 0, bounds.width, bounds.height, bounds.x, bounds.y, bounds.x + bounds.width, bounds.y+bounds.height, draggabletabbedpane.this);              dragging = true;             repaint();           }         } else {           currentmouselocation = e.getpoint();            // need repaint           repaint();         }          super.mousedragged(e);       }     });      addmouselistener(new mouseadapter() {       public void mousereleased(mouseevent e) {          if(dragging) {           int tabnumber = getui().tabforcoordinate(draggabletabbedpane.this, e.getx(), 10);            if(tabnumber >= 0) {             component comp = getcomponentat(draggedtabindex);             string title = gettitleat(draggedtabindex);             removetabat(draggedtabindex);             inserttab(title, null, comp, null, tabnumber);           }         }          dragging = false;         tabimage = null;       }     });   }    protected void paintcomponent(graphics g) {     super.paintcomponent(g);      // dragging?     if(dragging && currentmouselocation != null && tabimage != null) {       // draw dragged tab       g.drawimage(tabimage, currentmouselocation.x, currentmouselocation.y, this);     }   }    public static void main(string[] args) {     jframe test = new jframe("tab test");     test.setdefaultcloseoperation(jframe.exit_on_close);     test.setsize(400, 400);      draggabletabbedpane tabs = new draggabletabbedpane();     tabs.addtab("one", new jbutton("one"));     tabs.addtab("two", new jbutton("two"));     tabs.addtab("three", new jbutton("three"));     tabs.addtab("four", new jbutton("four"));      test.add(tabs);     test.setvisible(true);   } } 

Comments

Popular posts from this blog

c++ - How do I get a multi line tooltip in MFC -

asp.net - In javascript how to find the height and width -

c# - DataTable to EnumerableRowCollection -