Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Showing JDialog as sheet not working

I am currently using this code to create a JDialog;

package com.kamuara.reposync.window;

import java.awt.Dialog;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.UIManager;

public class SheetDialog {

    private JFrame _windowFrame;

    public static void main(String[] args) {
        System.setProperty("apple.awt.documentModalSheet", "true");
        System.setProperty("apple.awt.brushMetalLook", "true");

        try {
            UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
            new SheetDialog();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public SheetDialog() {
        _windowFrame = new JFrame();
        _windowFrame.setResizable(false);
        _windowFrame.setBounds(100, 100, 451, 320);
        _windowFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        _windowFrame.getContentPane().setLayout(null);
        _windowFrame.setVisible(true);

        JButton showDialogButton = new JButton("Show Dialog");
        showDialogButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                showSheetDialog(_windowFrame, "Test", "This should be a sheet dialog", "Oke");
            }
        });
        showDialogButton.setBounds(328, 263, 117, 29);
        _windowFrame.getContentPane().add(showDialogButton);
    }

    public void showSheetDialog(JFrame owner, String title, String message, String button) {
        final JDialog messageDialog = new JDialog(owner, title, Dialog.ModalityType.DOCUMENT_MODAL);
        messageDialog.setBounds(30, 0, owner.getWidth() - 60, 130);

        // TODO: only when os is osx
        messageDialog.getRootPane().putClientProperty("apple.awt.documentModalSheet", "true");
        messageDialog.setLayout(null);

        int offsetX = 25;

        JLabel titleLabel = new JLabel(title);
        titleLabel.setFont(new Font("Lucida Grande", Font.BOLD, 13));
        titleLabel.setBounds(offsetX, 10, 100, 25);
        messageDialog.getContentPane().add(titleLabel);

        JLabel messageLabel = new JLabel(message);
        messageLabel.setVerticalTextPosition(JLabel.TOP);
        messageLabel.setHorizontalTextPosition(JLabel.LEFT);
        messageLabel.setFont(new Font("Lucida Grande", Font.PLAIN, 11));
        messageLabel.setBounds(offsetX, 10, messageDialog.getWidth() - 10, messageDialog.getHeight() - 60);
        messageDialog.getContentPane().add(messageLabel);

        JButton okButton = new JButton(button);
        okButton.setBounds(messageDialog.getWidth() - 105, messageDialog.getHeight() - 35, 100, 25);
        okButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent arg0) {
                messageDialog.dispose();
            }
        });
        messageDialog.getContentPane().add(okButton);

        messageDialog.setVisible(true);
    }
}

I was previously using Java 6 to compile the application and setting the clientProperty apple.awt.documentModalSheet was working perfectly to display the dialog as a "Sheet" on OSX but now I started using Java 7 (update 25) and the dialog is no longer displayed as a Sheet. I can't seem to find any update documentation on this. Have they changed anything about this? How can I solve this? The current interface design looks tons better with a Sheet than a dialog.

Update

I found the following Bug report which seems to be the same issue as I am experiencing;

http://bugs.sun.com/view_bug.do?bug_id=8010197

Does anyone know how to resolve this? I have looked into libraries like QuaQua but I would prefer not using any library because I just want the Sheet functionality.

Update 2

I tried QuaQua, but the library currently has the exact same problem when compiling with Java 7. Any workarounds?

Update 3

Replaced code with working sample (http://pastebin.com/PJ8VGdPb)

Update 4

Found out SWT has a style for their Shell class named SWT.SHEET which still works in Java7, I don't prefer using a library like SWT, but it seems to be the only solution.

like image 996
Thizzer Avatar asked Jul 24 '13 20:07

Thizzer


People also ask

How do I open JDialog in center of screen?

If you really want to center a JDialog on screen, you can use code like this: // center a jdialog on screen JDialog d = new JDialog(); d. setSize(400, 300); d. setLocationRelativeTo(null); d.

How do I add Scrollpane to my panel?

getContentPane(). add(scrollPanel); This code will work in general to add JScrollPane to JPanel. Adjust bounds of frame, panel and scrollpane according to your requirements but ensure that the bounds of JScrollPane are within the bounds of the frame otherwise the scrollpane will not be visible.

Where is JDialog used in an application?

JDialog is one of the important features of JAVA Swing contributing to interactive desktop-based applications. This is used as a top-level container on which multiple lightweight JAVA swing components can be placed to form a window based application.


2 Answers

As far as I know, Apple didn't officially released their version of JDK 7. The latest version of the JDK Apple has optimized for their OS X is still JDK 6. That is also why updates for Java come thru the AppStore update tab. These updates do not come straight from Oracle.

If you downloaded JDK 7 directly from Oracle, this is a more generic, non-tweaked version.

So, I think you will just have to wait for Apple to release their OS X optimized JDK 7.

I experienced a lot of OS X features not to be working when I download from Oracle:

  • Trackpad gestures
  • Native Aqua Look'n'Feel doesn't work, even when trying to set it manually through UIManager.
  • Application icon not working when using JOptionPane.
  • JMenu's will stick into the JFrame itself instead of moving to the top of the screen.
like image 86
Martijn Courteaux Avatar answered Oct 18 '22 04:10

Martijn Courteaux


It seems before JDK fix the bug, you have to implement the Sheet yourself.

The key points are:

  1. Use the glass pane of the window frame to hold the Sheet dialog
  2. Use the GridBagLayout (with NORTH anchor) to put the dialog at top|center of the pane
  3. Animate the Sheet when show/disappeare by painting the dialog repeatedly, every time paint more/less part of the dialog

The following is the example code

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GraphicsConfiguration;
import java.awt.GraphicsEnvironment;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;

import javax.swing.Box;
import javax.swing.JComponent;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.Timer;
import javax.swing.border.LineBorder;

public class SheetableJFrame extends JFrame implements ActionListener {

    public static final int INCOMING = 1;
    public static final int OUTGOING = -1;
    public static final float ANIMATION_DURATION = 1000f;
    public static final int ANIMATION_SLEEP = 50;

    JComponent sheet;
    JPanel glass;
    Sheet animatingSheet;
    boolean animating;
    int animationDirection;
    Timer animationTimer;
    long animationStart;
    BufferedImage offscreenImage;

    public SheetableJFrame() {
        super();
        glass = (JPanel) getGlassPane();
        glass.setLayout(new GridBagLayout());
        animatingSheet = new Sheet();
        animatingSheet.setBorder(new LineBorder(Color.black, 1));
    }

    public JComponent showJDialogAsSheet(JDialog dialog) {
        sheet = (JComponent) dialog.getContentPane();
        sheet.setBorder(new LineBorder(Color.black, 1));
        glass.removeAll();
        animationDirection = INCOMING;
        startAnimation();
        return sheet;
    }

    public void hideSheet() {
        animationDirection = OUTGOING;
        startAnimation();
    }

    private void startAnimation() {
        glass.repaint();
        // clear glasspane and set up animatingSheet
        animatingSheet.setSource(sheet);
        glass.removeAll();
        GridBagConstraints gbc = new GridBagConstraints();
        gbc.anchor = GridBagConstraints.NORTH;
        glass.add(animatingSheet, gbc);
        gbc.gridy = 1;
        gbc.weighty = Integer.MAX_VALUE;
        glass.add(Box.createGlue(), gbc);
        glass.setVisible(true);

        // start animation timer
        animationStart = System.currentTimeMillis();
        if (animationTimer == null) animationTimer = new Timer(ANIMATION_SLEEP, this);
        animating = true;
        animationTimer.start();
    }

    private void stopAnimation() {
        animationTimer.stop();
        animating = false;
    }

    // used by the Timer
    public void actionPerformed(ActionEvent e) {
        if (animating) {
            // calculate height to show
            float animationPercent = (System.currentTimeMillis() - animationStart) / ANIMATION_DURATION;
            animationPercent = Math.min(1.0f, animationPercent);
            int animatingHeight = 0;

            if (animationDirection == INCOMING) {
                animatingHeight = (int) (animationPercent * sheet.getHeight());
            } else {
                animatingHeight = (int) ((1.0f - animationPercent) * sheet.getHeight());
            }
            // clip off that much from sheet and put it into animatingSheet
            animatingSheet.setAnimatingHeight(animatingHeight);
            animatingSheet.repaint();

            if (animationPercent >= 1.0f) {
                stopAnimation();
                if (animationDirection == INCOMING) {
                    finishShowingSheet();
                } else {
                    glass.removeAll();
                    glass.setVisible(false);
                    glass.setLayout(new GridBagLayout());
                    animatingSheet = new Sheet();
                }
            }
        }
    }

    private void finishShowingSheet() {
        glass.removeAll();
        GridBagConstraints gbc = new GridBagConstraints();
        gbc.anchor = GridBagConstraints.NORTH;
        glass.add(sheet, gbc);
        gbc.gridy = 1;
        gbc.weighty = Integer.MAX_VALUE;
        glass.add(Box.createGlue(), gbc);
        glass.revalidate();
        glass.repaint();
    }

    class Sheet extends JPanel {
        Dimension animatingSize = new Dimension(0, 1);
        JComponent source;
        BufferedImage offscreenImage;

        public Sheet() {
            super();
            setOpaque(true);
        }

        public void setSource(JComponent source) {
            this.source = source;
            animatingSize.width = source.getWidth();
            makeOffscreenImage(source);
        }

        public void setAnimatingHeight(int height) {
            animatingSize.height = height;
            setSize(animatingSize);
        }

        private void makeOffscreenImage(JComponent source) {
            GraphicsConfiguration gfxConfig = GraphicsEnvironment.getLocalGraphicsEnvironment()
                    .getDefaultScreenDevice().getDefaultConfiguration();
            offscreenImage = gfxConfig.createCompatibleImage(source.getWidth(), source.getHeight());
            Graphics2D offscreenGraphics = (Graphics2D) offscreenImage.getGraphics();
            source.paint(offscreenGraphics);
        }

        public Dimension getPreferredSize() {
            return animatingSize;
        }

        public Dimension getMinimumSize() {
            return animatingSize;
        }

        public Dimension getMaximumSize() {
            return animatingSize;
        }

        public void paint(Graphics g) {
            // get the bottom-most n pixels of source and paint them into g, where n is height
            BufferedImage fragment = offscreenImage.getSubimage(0, offscreenImage.getHeight() - animatingSize.height,
                    source.getWidth(), animatingSize.height);
            g.drawImage(fragment, 0, 0, this);
        }
    }
}

The test code

import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;

import javax.swing.JDialog;
import javax.swing.JOptionPane;

public class SheetTest extends Object implements PropertyChangeListener {

    JOptionPane optionPane;
    SheetableJFrame frame;

    public static void main(String[] args) {
        new SheetTest();
    }

    public SheetTest() {
        frame = new SheetableJFrame();
        // build JOptionPane dialog and hold onto it
        optionPane = new JOptionPane("Do you want to close?", JOptionPane.QUESTION_MESSAGE, JOptionPane.CANCEL_OPTION);
        frame.setSize(640, 480);
        frame.setVisible(true);
        optionPane.addPropertyChangeListener(this);

        JDialog dialog = optionPane.createDialog(frame, "irrelevant");
        frame.showJDialogAsSheet(dialog);
    }

    public void propertyChange(PropertyChangeEvent pce) {
        if (pce.getPropertyName().equals(JOptionPane.VALUE_PROPERTY)) {
            System.out.println("Selected option " + pce.getNewValue());
            frame.hideSheet();
        }
    }
}

reference
http://oreilly.com/pub/h/4852
http://book.javanb.com/swing-hacks/swinghacks-chp-6-sect-6.html

like image 21
Dan Avatar answered Oct 18 '22 04:10

Dan