Discussion:
JInternalFrame, Swing, disable move functionality - how ?
(too old to reply)
kelahcim
2004-03-01 13:07:08 UTC
Permalink
Hi,

I have a problem with JInternalFrame, recently.
I would like to create such a JInternalFrame, that wouldn't be able to move.

At the end there is a small program which demonstrates what am I thinking
about.

You can see that there are three "panels"

1. JDesktopPane
2. JPanel
3. JTabbedPane

You can also see, that only panel able to stop JInternalFrame from moving is
JTabbedPane.

Can anybody tell me if this is a some kind of a parameter (which determines
whether Frame is able to move or not) or maybe I have to work harder to
obtain such an efect?

Best regards,

Michal

/**
* code
*/

import java.awt.Dimension;
import javax.swing.DefaultDesktopManager;
import javax.swing.JComponent;
import javax.swing.JInternalFrame;
import javax.swing.plaf.InternalFrameUI;
import javax.swing.plaf.basic.BasicInternalFrameUI;

public class Example extends javax.swing.JFrame {
public Example() {
initComponents();

JInternalFrame jif1 = new JInternalFrame("hello 1");
jif1.setVisible(true);
jTabbedPane1.add(jif1);

JInternalFrame jif2 = new JInternalFrame("hello 2");
jif2.setVisible(true);
jif2.setBounds(0,0, 200,100);
jDesktopPane1.add(jif2);

}
private void initComponents() {
java.awt.GridBagConstraints gridBagConstraints;

jSplitPane1 = new javax.swing.JSplitPane();
jTabbedPane1 = new javax.swing.JTabbedPane();
jPanel1 = new javax.swing.JPanel();
jSplitPane2 = new javax.swing.JSplitPane();
jDesktopPane1 = new javax.swing.JDesktopPane();
jPanel2 = new javax.swing.JPanel();
jInternalFrame1 = new javax.swing.JInternalFrame();

getContentPane().setLayout(new java.awt.GridLayout(1, 1));

addWindowListener(new java.awt.event.WindowAdapter() {
public void windowClosing(java.awt.event.WindowEvent evt) {
exitForm(evt);
}
});

jTabbedPane1.setTabPlacement(javax.swing.JTabbedPane.BOTTOM);
jSplitPane1.setLeftComponent(jTabbedPane1);

jPanel1.setLayout(new java.awt.GridLayout(1, 1));

jSplitPane2.setLeftComponent(jDesktopPane1);

jPanel2.setLayout(new java.awt.GridBagLayout());

jInternalFrame1.setTitle("hello 3");
jInternalFrame1.setVisible(true);
gridBagConstraints = new java.awt.GridBagConstraints();
gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
gridBagConstraints.weightx = 1.0;
gridBagConstraints.weighty = 1.0;
jPanel2.add(jInternalFrame1, gridBagConstraints);

jSplitPane2.setRightComponent(jPanel2);

jPanel1.add(jSplitPane2);

jSplitPane1.setRightComponent(jPanel1);

getContentPane().add(jSplitPane1);

pack();
}
private void exitForm(java.awt.event.WindowEvent evt) {
System.exit(0);
}
public static void main(String args[]) {
new Example().show();
}
private javax.swing.JDesktopPane jDesktopPane1;
private javax.swing.JInternalFrame jInternalFrame1;
private javax.swing.JPanel jPanel1;
private javax.swing.JPanel jPanel2;
private javax.swing.JSplitPane jSplitPane1;
private javax.swing.JSplitPane jSplitPane2;
private javax.swing.JTabbedPane jTabbedPane1;
}
ak
2004-03-02 09:41:54 UTC
Permalink
Post by kelahcim
I have a problem with JInternalFrame, recently.
I would like to create such a JInternalFrame, that wouldn't be able to move.
you need to install your own UI for this:


/***************************************************************/

package com;

import javax.swing.*;
import javax.swing.event.MouseInputAdapter;
import javax.swing.plaf.ComponentUI;
import javax.swing.plaf.basic.BasicInternalFrameUI;
import java.awt.event.MouseEvent;

public class BasicMyInternalFrameUI extends BasicInternalFrameUI {

public static ComponentUI createUI(JComponent b) {
return new BasicMyInternalFrameUI((JInternalFrame)b);
}

public BasicMyInternalFrameUI(JInternalFrame b) {
super(b);
}

protected MouseInputAdapter createBorderListener(JInternalFrame w) {
return new MyBorderListener();
}

protected class MyBorderListener extends BorderListener {

public MyBorderListener() {
}

public void mouseDragged(MouseEvent e) {
if (e.getSource() == getNorthPane()) {
return;
}
super.mouseDragged(e);
}
}
}

/***************************************************************/

package com;

import javax.swing.*;
import javax.swing.plaf.ComponentUI;
import javax.swing.plaf.InternalFrameUI;

public class MyInternalFrame extends JInternalFrame {

private static final String uiClassID = "com.MyInternalFrame";

static {
UIManager.put(uiClassID, "com.BasicMyInternalFrameUI");
}

public MyInternalFrame() {
}

public MyInternalFrame(String title) {
super(title);
}

public void updateUI() {
ComponentUI ui = UIManager.getUI(this);
setUI((BasicMyInternalFrameUI) ui);
invalidate();
}

public void setUI(BasicMyInternalFrameUI ui) {
super.setUI(ui);
}

public InternalFrameUI getUI() {
return (BasicMyInternalFrameUI) ui;
}

public String getUIClassID() {
return uiClassID;
}
}

/***************************************************************/
package com;


import javax.swing.*;

public class Example extends javax.swing.JFrame {

public Example() {
initComponents();

JInternalFrame jif1 = new JInternalFrame("jif1");
jif1.setVisible(true);
jTabbedPane1.add(jif1);

JInternalFrame jif2 = new MyInternalFrame("jif2");
jif2.setVisible(true);
jif2.setBounds(0, 0, 200, 100);
jDesktopPane1.add(jif2);

}

private void initComponents() {
java.awt.GridBagConstraints gridBagConstraints;

jSplitPane1 = new javax.swing.JSplitPane();
jTabbedPane1 = new javax.swing.JTabbedPane();
jPanel1 = new javax.swing.JPanel();
jSplitPane2 = new javax.swing.JSplitPane();
jDesktopPane1 = new javax.swing.JDesktopPane();
jPanel2 = new javax.swing.JPanel();
jInternalFrame1 = new MyInternalFrame("jInternalFrame1");

getContentPane().setLayout(new java.awt.GridLayout(1, 1));

addWindowListener(new java.awt.event.WindowAdapter() {
public void windowClosing(java.awt.event.WindowEvent evt) {
exitForm(evt);
}
});

jTabbedPane1.setTabPlacement(javax.swing.JTabbedPane.BOTTOM);
jSplitPane1.setLeftComponent(jTabbedPane1);

jPanel1.setLayout(new java.awt.GridLayout(1, 1));

jSplitPane2.setLeftComponent(jDesktopPane1);

jPanel2.setLayout(new java.awt.GridBagLayout());

jInternalFrame1.setVisible(true);
gridBagConstraints = new java.awt.GridBagConstraints();
gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
gridBagConstraints.weightx = 1.0;
gridBagConstraints.weighty = 1.0;
jPanel2.add(jInternalFrame1, gridBagConstraints);

jSplitPane2.setRightComponent(jPanel2);

jPanel1.add(jSplitPane2);

jSplitPane1.setRightComponent(jPanel1);

getContentPane().add(jSplitPane1);

pack();
}

private void exitForm(java.awt.event.WindowEvent evt) {
System.exit(0);
}

public static void main(String args[]) {
new Example().show();
}

private javax.swing.JDesktopPane jDesktopPane1;
private javax.swing.JInternalFrame jInternalFrame1;
private javax.swing.JPanel jPanel1;
private javax.swing.JPanel jPanel2;
private javax.swing.JSplitPane jSplitPane1;
private javax.swing.JSplitPane jSplitPane2;
private javax.swing.JTabbedPane jTabbedPane1;
}

/***************************************************************/


--

____________

http://reader.imagero.com the best java image reader.
kelahcim
2004-03-02 10:21:42 UTC
Permalink
Post by kelahcim
I have a problem with JInternalFrame, recently.
I would like to create such a JInternalFrame, that wouldn't be able to
move.
That code does a thing.
Thank you.

Michal
ak
2004-03-02 11:59:59 UTC
Permalink
Post by kelahcim
That code does a thing.
Thank you.
note, that in real life you should provide more UIs - not only Basic - at
least Metal, Windows and Motif.

--

____________

http://reader.imagero.com the best java image reader.
Jon A. Cruz
2004-03-02 17:25:12 UTC
Permalink
Post by kelahcim
I have a problem with JInternalFrame, recently.
I would like to create such a JInternalFrame, that wouldn't be able to
move.
Or...

So that you don't fall into the trap of having to support all
LookAndFeels on different versions on different platforms...

You could instead use a simple layout manager that would allow
positioning unless you 'freeze' one of it's children.
ak
2004-03-02 18:50:43 UTC
Permalink
Post by Jon A. Cruz
So that you don't fall into the trap of having to support all
LookAndFeels on different versions on different platforms...
You could instead use a simple layout manager that would allow
positioning unless you 'freeze' one of it's children.
LayoutManager can't prevent you from dragging JInternalFrame
--

____________

http://reader.imagero.com the best java image reader.
Jon A. Cruz
2004-03-03 05:39:53 UTC
Permalink
Post by ak
LayoutManager can't prevent you from dragging JInternalFrame
Sure it can.

One way is to use it's addLayoutComponet methods to hook in a
ComponentListener on any targeted JInternalFrames.


Or... one could hook the proper events from inside of subclass of
JInternalFrame itself, instead of using a custom UI.
ak
2004-03-03 07:58:28 UTC
Permalink
Post by Jon A. Cruz
Post by ak
LayoutManager can't prevent you from dragging JInternalFrame
Sure it can.
One way is to use it's addLayoutComponet methods to hook in a
ComponentListener on any targeted JInternalFrames.
it looks ugli.
Post by Jon A. Cruz
Or... one could hook the proper events from inside of subclass of
JInternalFrame itself, instead of using a custom UI.
it looks also ugli.

this is SUN mistake, they could have one boolean variable more.

the only one better way is to use GlassPane to filter out mouseDragged
events.

____________

http://reader.imagero.com the best java image reader.
kelahcim
2004-03-03 08:47:01 UTC
Permalink
Post by ak
this is SUN mistake, they could have one boolean variable more.
the only one better way is to use GlassPane to filter out mouseDragged
events.
But wouldn't it block the dragging entirely in the component ?

michal
ak
2004-03-03 09:29:35 UTC
Permalink
Post by kelahcim
Post by ak
the only one better way is to use GlassPane to filter out mouseDragged
events.
But wouldn't it block the dragging entirely in the component ?
no, because you can first check which component is currently under the mouse
and than decide to filter out or not.

____________

http://reader.imagero.com the best java image reader.
kelahcim
2004-03-03 11:46:18 UTC
Permalink
Post by ak
no, because you can first check which component is currently under the mouse
and than decide to filter out or not.
But i have found a totally different solution for my problem.

If I am not wrong it is enought to call setMaximum(true).
Then I get maximized frame which fullfils totaly the DesktopPane and is
unable to move.

Well, it is not exactly what I ment to do, but in fact it does a thing.

I can dock and undock the frame from the JDesktopPanel.

michal
ak
2004-03-03 16:16:56 UTC
Permalink
Post by kelahcim
Post by ak
no, because you can first check which component is currently under the
mouse
Post by ak
and than decide to filter out or not.
But i have found a totally different solution for my problem.
If I am not wrong it is enought to call setMaximum(true).
Then I get maximized frame which fullfils totaly the DesktopPane and is
unable to move.
Well, it is not exactly what I ment to do, but in fact it does a thing.
I can dock and undock the frame from the JDesktopPanel.
michal
sorry, but in your example middle frame got not all possible place.
I thought you need many not moveable frames in one container,

____________

http://reader.imagero.com the best java image reader.
kelahcim
2004-03-04 07:17:49 UTC
Permalink
Post by ak
sorry, but in your example middle frame got not all possible place.
I thought you need many not moveable frames in one container,
Yes, I know.

But I didn't know, that I can maximize jframe by hand, that's why I haven't
done it.
And I suppose I didn't expressed myself enough clearly :)

Sorry.

Michal

Loading...