participate


Swing - How to immobilize a JInternalFrame?
<<   Back to Forum  |   Give us Feedback
This topic has 9 replies on 1 page.
JosAH
Posts:13,022
Registered: 4/6/04
How to immobilize a JInternalFrame?   
Sep 15, 2006 4:42 AM

 
Greetings,

I want to immobilize a JInternalFrame (who'd tought it? ;-) At the moment
I do it like this:
// in a class extending JInternalFrame itself:
this.addComponentListener(new ComponentAdapter() {
			
   public void componentMoved(ComponentEvent ce) {
			
      if (locked) {
         if (point == null) point= getLocation();
         setLocation(point);
      }
   }
});

'point' and 'locked' are just private member variables. This works but it
looks funny, i.e. it's as if the JInternalFrame is fighting back: it moves a
bit but jumps back to its original location when you don't pay attention for
a split second.

It makes me giggle but my customers find it sloppy. They are used to
these neat little pins you can use to keep a frame pinned to where it is
at that moment. Is there anything like that possible using Swing? Of
course I scrutinized the API docs but I could't find anything I could use.
I don't care about a visible pin, if only that JInternalFrame would stand
still completely.

Ideas anyone? Thanks in advance and

kind regards,

Jos
 
camickr
Posts:33,025
Registered: 27/02/98
Re: How to immobilize a JInternalFrame?   
Sep 15, 2006 9:10 AM (reply 1 of 9)  (In reply to original post )

 
This posting shows one way:

http://forum.java.sun.com/thread.jspa?forumID=57&threadID=652179
 
JosAH
Posts:13,022
Registered: 4/6/04
Re: How to immobilize a JInternalFrame?   
Sep 15, 2006 9:38 AM (reply 2 of 9)  (In reply to #1 )

 
This posting shows one way:

http://forum.java.sun.com/thread.jspa?forumID=57&threadID=652179

Thank you very much for that. That's what I call 'component surgery' ;-)
I've implemented a little 'toggle' method that removes or reinstalls those
listeners according to a simple boolean flag. As far as I can see now
everything works fine. Thank you very much again and

kind regards,

Jos
 
kirillg
Posts:627
Registered: 8/1/06
Re: How to immobilize a JInternalFrame?   
Sep 18, 2006 9:54 AM (reply 3 of 9)  (In reply to #2 )

 
The latest 3.1dev drop of Substance look-and-feel allows you to set the SubstanceLookAndFeel.PERMANENTLY_PINNED client property on the JInternalFrame. If you set it to Boolean.TRUE, the relevant frame would be "pinned" - you can't move it or resize it.

In general, it's not a good idea to remove the listeners not installed by you, since you don't know what functionality you're removing - this can cause unexpected behaviour at least or exceptions / frame freezing in worse cases.
 
camickr
Posts:33,025
Registered: 27/02/98
Re: How to immobilize a JInternalFrame?   
Sep 18, 2006 11:21 AM (reply 4 of 9)  (In reply to #3 )

 
In general, it's not a good idea to remove the listeners not installed by you...

Yes, but after minutes worth of testing I didn't see any problems :-)

While looking at the source code of the installed MouseMotionListener to see exactly what it did, I came up with a "better" (?) approach:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.plaf.basic.*;
 
public class InternalFrameUnMovable2 extends JFrame
{
	JDesktopPane desktop;
 
	public InternalFrameUnMovable2()
	{
		desktop = new JDesktopPane();
		desktop.setDesktopManager( new NoDragDesktopManager() );
		getContentPane().add( desktop );
 
		desktop.add( createInternalFrame(1, Color.RED) );
		desktop.add( createInternalFrame(2, Color.GREEN) );
		desktop.add( createInternalFrame(3, Color.BLUE) );
	}
 
	private JInternalFrame createInternalFrame(int number, Color background)
	{
		JInternalFrame internal =
			new JInternalFrame( "Frame" + number, true, true, true, true );
		internal.setBackground( background );
		internal.setVisible( true );
		int location = 50 * number;
		internal.setBounds(location, location, 300, 300);
		return internal;
	}
 
	public static void main(String args[])
	{
		InternalFrameUnMovable2 frame = new InternalFrameUnMovable2();
		frame.setDefaultCloseOperation( EXIT_ON_CLOSE );
		frame.setSize(600, 600);
		frame.setLocationRelativeTo( null );
		frame.setVisible(true);
 
		//  Activate first internal frame
 
		try
		{
			JInternalFrame[] frames = frame.desktop.getAllFrames();
			frames[0].setSelected(true);
		}
		catch (java.beans.PropertyVetoException e) {}
 
		//  Make first internal frame unmovable
 
		JInternalFrame[] frames = frame.desktop.getAllFrames();
		JInternalFrame f = frames[0];
		f.putClientProperty("dragMode", "fixed");
	}
 
	class NoDragDesktopManager extends DefaultDesktopManager
	{
		public void beginDraggingFrame(JComponent f)
		{
			if (!"fixed".equals(f.getClientProperty("dragMode")))
				super.beginDraggingFrame(f);
		}
 
		public void dragFrame(JComponent f, int newX, int newY)
		{
			if (!"fixed".equals(f.getClientProperty("dragMode")))
				super.dragFrame(f, newX, newY);
		}
 
		public void endDraggingFrame(JComponent f)
		{
			if (!"fixed".equals(f.getClientProperty("dragMode")))
				super.endDraggingFrame(f);
		}
	}
}
 
kirillg
Posts:627
Registered: 8/1/06
Re: How to immobilize a JInternalFrame?   
Sep 18, 2006 11:29 AM (reply 5 of 9)  (In reply to #4 )

 
And now you single-handedly obliterate the installed desktop managers under Synth, Motif and Windows LAFs...
 
camickr
Posts:33,025
Registered: 27/02/98
Re: How to immobilize a JInternalFrame?   
Sep 18, 2006 11:35 AM (reply 6 of 9)  (In reply to #5 )

 
And now you single-handedly obliterate the installed desktop managers under Synth, Motif and Windows LAFs...

As opposed to forcing everyone to use the Substance LAF?

Its an option. The user can decide if it meets the requirement or not.
 
KelVarnson
Posts:1,331
Registered: 17/03/04
Re: How to immobilize a JInternalFrame?   
Sep 18, 2006 11:50 AM (reply 7 of 9)  (In reply to #4 )

 
While looking at the source code of the installed
MouseMotionListener to see exactly what it did, I came up with a "better" (?) approach:

Very nice solution.

However Jos, keep in mind that whatever solution is used, the frame would also have to be resizable = false, or the user could work around it by just dragging the edges until the frame was back to it's original shape but in a different location.
 
kirillg
Posts:627
Registered: 8/1/06
Re: How to immobilize a JInternalFrame?   
Sep 18, 2006 2:05 PM (reply 8 of 9)  (In reply to #6 )

 
As opposed to forcing everyone to use the Substance
LAF?

"Forcing" is a strong word here. I'm just following the often-voiced requests and adding them to Substance. Just pointing out that the solution with installing a custom desktop manager is not cross-LAF and may interfere with native fidelity, as well as the solution with removing mouse listeners that may result in unpredictable behavior. Not to mention a possible ClassCastException if the specific InternalFrameUI implementation doesn't extend BasicInternalFrameUI (since it doesn't have to).

By the way - the solution provided by Substance takes care of both moving and resizing.
 
JosAH
Posts:13,022
Registered: 4/6/04
Re: How to immobilize a JInternalFrame?   
Sep 19, 2006 1:19 AM (reply 9 of 9)  (In reply to #7 )

 
While looking at the source code of the installed
MouseMotionListener to see exactly what it did, I
came up with a "better" (?) approach:

Very nice solution.

I agree; I ended up with simple MouseMotionListeners that wrap the
original ones registered at the NORTH pane. Those wrappers can be
enabled and disabled at will. The downside is that I have to re-wrap
those listeners when the LAF changes ...

However Jos, keep in mind that whatever solution is used, the
frame would also have to be resizable = false, or the user could work
around it by just dragging the edges until the frame was back
to it's original shape but in a different location.

Yep, I completely 'freeze' the frame, i.e. disable the MouseMotionListeners
and make the JInternalFrame non-resizable, non-maximizable and non-
iconifiable. I do have to check out the 'Substance' UI (I never heard of it
before, but then again, I'm no GUI wizard ;-)

Nice informative thread, thanks to all repliers for that.

kind regards,

Jos
 
This topic has 9 replies on 1 page.
Back to Forum
 
Read the Developer Forums Code of Conduct

Click to email this message Email this Topic

Edit this Topic
  
 
 
Forums Statistics
    Users Online : 28
  • Guests : 133

About Sun forums
  • Oracle Forums is a large collection of user generated discussions. It is here to help you ask questions, find answers, and participate in discussions.

    Check out our guide on Getting started with Oracle Forums for a full walkthrough of how to best leverage the benefits of this community.

Powered by Jive Forums