TITLE:JComponentの移動、リサイズ

JComponentの移動、リサイズ

Posted by terai at 2008-05-19
  • category: swing folder: ResizableComponents title: JComponentをマウスで移動、リサイズ tags: [JLayeredPane, MouseListener, MouseMotionListener, JComponent] author: aterai pubdate: 2008-05-19T13:41:14+09:00 description: JLayeredPaneに、マウスで移動、リサイズ可能なコンポーネントを追加します。 image: https://lh4.googleusercontent.com/_9Z4BYR88imo/TQTRw-M85QI/AAAAAAAAAhs/BFyVP2IYoak/s800/ResizableComponents.png

概要

JLayeredPaneに、マウスで移動、リサイズ可能なコンポーネントを追加します。

概要

JLayeredPaneに、移動、リサイズ可なコンポーネントを追加します。
  • &jnlp;
  • &jar;
  • &zip;

#screenshot

サンプルコード

#spanend
#spandel
public void mouseDragged(MouseEvent e) {
#spanend
  if(startPos==null || startingBounds==null) return;
  Point p = SwingUtilities.convertPoint((Component)e.getSource(), e.getX(), e.getY(), null);
#spanadd
* サンプルコード [#sourcecode]
#spanend
#spanadd
#code(link){{
#spanend
#spanadd
@Override public void mouseDragged(MouseEvent e) {
#spanend
  if (startPos == null || startingBounds == null) return;
  Point p = SwingUtilities.convertPoint(
    e.getComponent(), e.getX(), e.getY(), null);
  int deltaX = startPos.x - p.x;
  int deltaY = startPos.y - p.y;
  int newX = getX();
  int newY = getY();
  int newW = getWidth();
  int newH = getHeight();

  JComponent parent = (JComponent)getParent();
  JComponent parent = (JComponent) getParent();
  Rectangle parentBounds = parent.getBounds();
  Dimension min = new Dimension(50,50);
  Dimension max = new Dimension(500,500);
  Dimension min = new Dimension(50, 50);
  Dimension max = new Dimension(500, 500);

  switch(cursor) {
  switch (cursor) {
    case Cursor.N_RESIZE_CURSOR: {
      if(startingBounds.height + deltaY < min.height) {
      if (startingBounds.height + deltaY < min.height) {
        deltaY = -(startingBounds.height - min.height);
      }else if(startingBounds.height + deltaY > max.height) {
      } else if (startingBounds.height + deltaY > max.height) {
        deltaY = max.height - startingBounds.height;
      }
      if(startingBounds.y - deltaY < 0) { deltaY = startingBounds.y; }
      if (startingBounds.y - deltaY < 0) {
        deltaY = startingBounds.y;
      }
      newX = startingBounds.x;
      newY = startingBounds.y - deltaY;
      newW = startingBounds.width;
      newH = startingBounds.height + deltaY;
      break;
    }
    case Cursor.NE_RESIZE_CURSOR: {
      if(startingBounds.height + deltaY < min.height) {
      if (startingBounds.height + deltaY < min.height) {
        deltaY = -(startingBounds.height - min.height);
#spandel
//......
#spanend
#spanadd
// ...
#spanend

解説

上記のサンプルでは、ツールバーやポップアップメニューから、移動、リサイズ可能なJTableやJTreeを追加することができます。

解説

上記のサンプルでは、ツールバーやポップアップメニューから、移動、リサイズ可能なJTableJTreeJLayeredPane(JLayeredPaneのデフォルトレイアウトマネージャはnull)に追加できます。 リサイズボーダーの描画などは、Resizable Components - Santhosh Kumar's Weblogから、マウスのドラッグによる移動、リサイズの最大値、最小値の制限などは、%JAVA_HOME%/src/javax/swing/plaf/basic/BasicInternalFrameUI.java からの引用です。
JDK1.5では、JLayeredPane#setComponentPopupMenuを使う場合、以下のようにダミーのマウスリスナーなどを追加しておく必要があります。
  • JDK1.5JLayeredPane#setComponentPopupMenuを使う場合、以下のようなマウスリスナー(何も実行しない空のマウスリスナーでよい)を追加しておく必要がある?
    layer.setComponentPopupMenu(new MyPopupMenu());
    layer.addMouseListener(new MouseAdapter() {});
    

参考リンク

コメント

ResizableComponents1.png

参考リンク

コメント