/* (@)FullscreenWithOptionPaneTestGui.java */
/* Copyright 2010 Sebastian Haufe
* Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
[url]http://www.apache.org/licenses/LICENSE-2.0[/url]
* Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License. */
package com.ebenius;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
public class FullscreenWithOptionPaneTestGui {
/** Creates the GUI. Call on EDT, only! */
static void createAndShowGui() {
final JPanel contentPane = new JPanel(new FlowLayout());
/* a color changing content pane */
new Timer(340, new ActionListener() {
public void actionPerformed(ActionEvent e) {
final Color background = contentPane.getBackground();
final Color newBackground;
if (Color.BLUE.equals(background)) {
newBackground = Color.RED;
} else if (Color.RED.equals(background)) {
newBackground = Color.YELLOW;
} else if (Color.YELLOW.equals(background)) {
newBackground = Color.GREEN;
} else if (Color.GREEN.equals(background)) {
newBackground = Color.ORANGE;
} else if (Color.ORANGE.equals(background)) {
newBackground = Color.GRAY;
} else {
newBackground = Color.BLUE;
}
contentPane.setBackground(newBackground);
contentPane.repaint();
}
}).start();
/* the full screen window */
final JWindow window = new JWindow();
/* button to show the option pane */
contentPane.add(new JButton(new AbstractAction("Show Option Pane") {
private static final long serialVersionUID = 1L;
public void actionPerformed(ActionEvent e) {
/* create a desktop pane for the internal dialog */
final JDesktopPane desktopPane = new JDesktopPane();
desktopPane.setOpaque(false);
/* center dialogs on the desktop pane */
desktopPane.setLayout(new LayoutManager() {
public void addLayoutComponent(String name, Component comp) {}
public void removeLayoutComponent(Component comp) {}
public Dimension preferredLayoutSize(Container parent) {
return new Dimension(0, 0);
}
public Dimension minimumLayoutSize(Container parent) {
return new Dimension(0, 0);
}
public void layoutContainer(Container parent) {
final Dimension pSize = parent.getSize();
final Insets pInsets = parent.getInsets();
final int x = (pSize.width + pInsets.left - pInsets.right) / 2;
final int y = (pSize.height + pInsets.top - pInsets.bottom) / 2;
for (int i = 0; i < parent.getComponentCount(); i++) {
final Component component = parent.getComponent(i);
final Dimension size = component.getPreferredSize();
component.setSize(size);
component.setLocation(x - size.width / 2, y - size.height / 2);
}
}
});
/* set the desktop pane as glass pane, show dialog, reset glass */
final Component oldGlassPane = window.getGlassPane();
window.setGlassPane(desktopPane);
JOptionPane.showInternalMessageDialog(desktopPane, "Huhu");
window.setGlassPane(oldGlassPane);
}
}));
/* exit */
contentPane.add(new JToggleButton(new AbstractAction("Exit") {
private static final long serialVersionUID = 1L;
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
}));
/* pack and show frame */
window.setContentPane(contentPane);
window.pack();
window.setLocationRelativeTo(null);
window.getGraphicsConfiguration().getDevice().setFullScreenWindow(window);
}
/** @param args ignored */
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGui();
}
});
}
}