Here's my modal JInternalFrame code
843804Nov 1 2002 — edited Nov 18 2008Hi, I think I've found a trick to do a modal internalFrame dealing with
the sistem eventQueue class and startModal method: (I tested and I think
it works fine, if there�s a bug please let me know about it !!!)
public class MyInternalFrame extends JInternalFrame {
// indica si aquest es modal o no.
boolean modal=false;
public void show() {
super.show();
if (this.modal) startModal();
}
public void setVisible(boolean value) {
super.setVisible(value);
if (modal) {
if (value) {
startModal();
} else {
stopModal();
}
}
}
private synchronized void startModal() {
try {
if (SwingUtilities.isEventDispatchThread()) {
EventQueue theQueue =
getToolkit().getSystemEventQueue();
while (isVisible()) {
AWTEvent event = theQueue.getNextEvent();
Object source = event.getSource();
boolean dispatch=true;
if (event instanceof MouseEvent) {
MouseEvent e = (MouseEvent)event;
MouseEvent m =
SwingUtilities.convertMouseEvent ((Component) e.getSource(),e,this);
if (!this.contains(m.getPoint())
&& e.getID()!=MouseEvent.MOUSE_DRAGGED) dispatch=false;
}
if (dispatch)
if (event instanceof ActiveEvent) {
((ActiveEvent)event).dispatch();
} else if (source instanceof Component) {
((Component)source).dispatchEvent(
event);
} else if (source instanceof MenuComponent) {
((MenuComponent)source).dispatchEvent(
event);
} else {
System.err.println(
"Unable to dispatch: " + event);
}
}
} else {
while (isVisible()) {
wait();
}
}
} catch (InterruptedException ignored) {
}
}
private synchronized void stopModal() {
notifyAll();
}
public void setModal(boolean modal) {
this.modal=modal;
}
public boolean isModal() {
return this.modal;
}
}
Please, con you tell me how to format the code to do it more readable ?