Java Utililty Methods JFrame

List of utility methods to do JFrame

Description

The list of methods to do JFrame are organized into topic(s).

Method

voidrefreshShape(final JFrame frame)
refresh Shape
if (frame != null) {
    if ("Mac OS X".equals(System.getProperty("os.name"))) {
        Graphics2D g2 = (Graphics2D) frame.getGraphics();
        Composite oldComposite = g2.getComposite();
        g2.setComposite(AlphaComposite.Clear);
        g2.fillRect(0, 0, frame.getWidth(), frame.getHeight());
        g2.setComposite(oldComposite);
        frame.paint(g2);
...
voidregisterCloseAction(final JFrame dialog, KeyStroke keyStroke)
Registers a keystroke to close the given dialog.
ActionListener escListener = new ActionListener() {
    public void actionPerformed(ActionEvent e) {
        dialog.dispose();
};
dialog.getRootPane().registerKeyboardAction(escListener, keyStroke, JComponent.WHEN_IN_FOCUSED_WINDOW);
voidregisterMenuShortcut(String uniqueActionName, Action action, int keyCode, JFrame frame)
Adds a menu shortcut (or accelerator) to the input map.
JRootPane rootPane = frame.getRootPane();
InputMap inputMap = rootPane.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW);
ActionMap actionMap = rootPane.getActionMap();
int ctrlKey = java.awt.Toolkit.getDefaultToolkit().getMenuShortcutKeyMask();
inputMap.put(KeyStroke.getKeyStroke(keyCode, ctrlKey), uniqueActionName);
actionMap.put(uniqueActionName, action);
voidrenderDialog(JFrame theframe, String szMessage, int noffsetx, int noffsety)
Calls renderDialog with Help in the title window for a JFrame
renderDialog(theframe, szMessage, noffsetx, noffsety, "Help");
voidreplaceGlassPane(JFrame frame, Component newGlassPane)
replace Glass Pane
Component oldGlassPane = frame.getGlassPane();
if (oldGlassPane != null)
    oldGlassPane.setVisible(false);
if (newGlassPane != null) {
    frame.setGlassPane(newGlassPane);
    newGlassPane.setVisible(true);
voidrestoreFrame(Class pClass, final JFrame pFrame, String pFrameId)
restore Frame
Preferences prefs = Preferences.userNodeForPackage(pClass);
final String locPrefId = pFrameId + ".location";
final String szPrefId = pFrameId + ".size";
String defLoc = "0:0";
String defSz = "500:500";
String[] locStr = prefs.get(locPrefId, defLoc).split(":");
String[] szStr = prefs.get(szPrefId, defSz).split(":");
final int szWidth = Integer.parseInt(szStr[0]);
...
voidrestoreFrame(JFrame frame)
restore Frame
try {
    frame.setExtendedState(JFrame.NORMAL);
} catch (IllegalComponentStateException e) {
voidrightShiftDialog(JDialog dialog, JFrame parent)
right Shift Dialog
Point p = parent.getLocationOnScreen();
p.x += parent.getWidth();
p.y += ((parent.getHeight() - dialog.getHeight()) / 2);
if (p.y <= 0)
    p.y = 20;
if (p.x <= 0)
    p.x = 20;
dialog.setLocation(p);
...
voidrun(JFrame frame, int width, int height)
run
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(width, height);
frame.setVisible(true);
voidsaveFrame(Class pClass, JFrame pFrame, String pFrameId)
save Frame
Preferences prefs = Preferences.userNodeForPackage(pClass);
final String locPrefId = pFrameId + ".location";
final String szPrefId = pFrameId + ".size";
String location = pFrame.getLocation().x + ":" + pFrame.getLocation().y;
String size = pFrame.getSize().width + ":" + pFrame.getSize().height;
prefs.put(locPrefId, location);
prefs.put(szPrefId, size);