JInternalFrameがマウスドラッグで移動中はそのフレームを半透明に変更する
Total: 1479, Today: 1, Yesterday: 0
Posted by aterai at
Last-modified:
Summary
JInternalFrameがマウスドラッグで移動中の場合はそのフレームを半透明に変更して背景が確認できるよう設定します。
Screenshot

Advertisement
Source Code Examples
JDesktopPane desktop = new JDesktopPane();
desktop.setDesktopManager(new DefaultDesktopManager() {
@Override public void beginDraggingFrame(JComponent f) {
setDraggingFrame(f);
super.beginDraggingFrame(f);
}
@Override public void endDraggingFrame(JComponent f) {
setDraggingFrame(null);
super.endDraggingFrame(f);
f.repaint();
}
@Override public void beginResizingFrame(JComponent f, int direction) {
setDraggingFrame(f);
desktop.setDragMode(JDesktopPane.OUTLINE_DRAG_MODE);
super.beginResizingFrame(f, direction);
}
@Override public void endResizingFrame(JComponent f) {
setDraggingFrame(null);
desktop.setDragMode(JDesktopPane.LIVE_DRAG_MODE);
super.endResizingFrame(f);
}
});
View in GitHub: Java, KotlinDescription
DesktopManager#beginDraggingFrame(...)をオーバーライドしてマウスでドラッグ中のJInternalFrameを記憶DefaultDesktopManagerではJInternalFrame#isDraggingを切り替えているが、このフィールドはパッケージプライベートなのでこのサンプルでは使用できない
JInternalFrame#paintComponent(...)をオーバーライドして自身がドラッグ中の場合は半透明で描画する- 半透明化することで
JDesktop#setDragMode(JDesktopPane.OUTLINE_DRAG_MODE)のアウトラインモードにしなくても移動中の背景が確認しやすくなるJInternalFrame frame = new JInternalFrame(title, true, true, true, true) { @Override protected void paintComponent(Graphics g) { // if (isDragging) { // JInternalFrame#isDragging: package private if (getDraggingFrame() == this) { ((Graphics2D) g).setComposite( AlphaComposite.getInstance(AlphaComposite.SRC_OVER, .2f)); } super.paintComponent(g); } };
- 半透明化することで
DesktopManager#endDraggingFrame(...)をオーバーライドしてマウスでドラッグ中のJInternalFrameの参照をクリアし、自身を再描画JDesktopPane.LIVE_DRAG_MODEではドラッグ移動終了後自動的に再描画は実行されないので、ここでJInternalFrame.repaint()を実行しないとフレームが半透明のままになってしまう
- 同様に
DesktopManager#beginResizingFrame(...)、DesktopManager#endResizingFrame(...)をオーバーライドしてリサイズ中のJInternalFrameを半透明に変更することも可能 - このサンプルではリサイズ中の半透明と合わせて
JDesktop#setDragMode(...)でリサイズ中開始時にJDesktopPane.OUTLINE_DRAG_MODE、リサイズ終了時にJDesktopPane.LIVE_DRAG_MODEに切り替えている