Google Tag Manager

2012/01/25

aterai

Sharing tabs between 2 JFrames

Code

//Display "DropLocation" using JLayer 
class DropLocationLayerUI extends LayerUI {
  private static final int LINEWIDTH = 3;
  private final Rectangle lineRect = new Rectangle();
  @Override public void paint(Graphics g, JComponent c) {
    super.paint (g, c);
    JLayer layer = (JLayer)c;
    DnDTabbedPane tabbedPane = (DnDTabbedPane)layer.getView();
    DnDTabbedPane.DropLocation loc = tabbedPane.getDropLocation();
    if(loc != null && loc.isDropable() && loc.getIndex()>=0) {
      int index = loc.getIndex();
      boolean isZero = index==0;
      Rectangle r = tabbedPane.getBoundsAt(isZero?0:index-1);
      if(tabbedPane.getTabPlacement()==JTabbedPane.TOP ||
         tabbedPane.getTabPlacement()==JTabbedPane.BOTTOM) {
        lineRect.setRect(
            r.x-LINEWIDTH/2+r.width*(isZero?0:1), r.y,LINEWIDTH,r.height);
      }else{
        lineRect.setRect(
            r.x,r.y-LINEWIDTH/2+r.height*(isZero?0:1), r.width,LINEWIDTH);
      }
      Graphics2D g2 = (Graphics2D)g.create();
      g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.5f));
      g2.setColor(Color.RED);
      g2.fill(lineRect);
      g2.dispose();
    }
  }
}
private final JLabel label = new JLabel() {
// https://free-the-pixel.blogspot.com/2010/04/ghost-drag-and-drop-over-multiple.html
// Free the pixel: GHOST drag and drop, over multiple windows]
  @Override public boolean contains(int x, int y) {
    return false;
  }
};
private final JWindow dialog = new JWindow();
public TabTransferHandler() {
  dialog.add(label);
  //dialog.setAlwaysOnTop(true); // Web Start
  dialog.setOpacity(0.5f);
  //com.sun.awt.AWTUtilities.setWindowOpacity(dialog, 0.5f); // JDK 1.6.0
  DragSource.getDefaultDragSource().addDragSourceMotionListener(
      new DragSourceMotionListener() {
    @Override public void dragMouseMoved(DragSourceDragEvent dsde) {
      Point pt = dsde.getLocation();
      pt.translate(5, 5); // offset
      dialog.setLocation(pt);
    }
  });
//...

References

5 comments:

  1. hi thanks for your great help, I wanted to ask is there any way that is transparent JPopupMenu?, the question is I have a panel inside the popup and just want to display the popup panel is transparent and to have an effect more setOpaque nice and try and not know any way to achieve this effect would greatly appreciate it thank you

    ReplyDelete
  2. Hi Hannibal,
    If I understand your requirement you might be able to use the following url:
    Tooltips - Translucent and Shaped Swing Windows | Java.net
    Another example:
    TranslucentPopupMenuTest.java

    ReplyDelete
  3. Hello, your drag/drop implementation is extremely powerfull and works very smoothely, thank you for sharing such quality code. Nevertheless I encounter this issue: adding a tooltip to the closeable tab breaks the drag/drop feature (e.g adding label.setToolTipText("test"); or this.setToolTipText("test"); after line 576). I reproduce the issue both on Windows and Linux (Java 7), have you any clue for a workaround?

    ReplyDelete
    Replies
    1. Hi ron190jSQL, thank you for your very useful information.
      Maybe the ButtonTabComponent(JLabel or JPanel, has the tooltip text) steal all mouse events(drag, select...).
      So need to use `JTabbedPane#setToolTipTextAt(int, String)` control instead of `JLabel#setToolTipText(String)`.

      Delete
    2. Thank you!! setToolTipTextAt() displays the tooltip properly, you're a beast ;)

      Delete