• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 668
  • Last Modified:

JDialog not showing every time

I have a JMenuItem

        menuItem = new JMenuItem(geAboutAction());

and its belonging Action:

        public void actionPerformed(ActionEvent e) {
            AboutBox.handleDialog(RegExpFrame.this);
        }

The problem is that this my AboutBox JDialog sometimes doesn't show when I click on menu item.
What can be the reason / problem?

Rgds, Ales

--- AboutBox ---

/**
 * The AboutBox shows a background picture and some main
 * data about the program such as version. The dialog's size is set
 * to the size of the image which is used as the background.
*/
public class AboutBox extends JDialog {

    /**
     * The image used as background
     */
    private Image m_background;
    private JFrame m_parentFrame;

    /**
     * Creates a new AboutBox with JFrame owner
     *
     * @param owner The owner of the dialog
     */
    public AboutBox(JFrame owner) {
        super(owner);
        m_parentFrame = owner;
        setTitle(UITools.getResourceString(getTitleKey()));
        setModal(true);
        buildUI();
        setResizable(false);
        ImageObserver observer = new ImageHandler(this);

        if (m_background == null) {
            m_background = java.awt.Toolkit.getDefaultToolkit().createImage(getImageSrc());
        }

        int width = m_background.getWidth(observer);
        int height = m_background.getHeight(observer);
    }

    protected String getTitleKey() {
        return "title.about";
    }

    protected URL getImageSrc() {
        return UIFactory.getResource("res/images/background.gif");
    }

    /**
     * Builds the user interface.
     */
    public void buildUI() {
        Container container = getContentPane();
        container.add(createPane());
    }

    /**
     * Creates the complete about panel. The background is a PicturePanel.
     *
     * @return JPanel the complete panel
     */
    protected JPanel createPane() {
        JPanel background = createBackground();

        JLabel label = null;
        /*
        JPanel titlePanel = new JPanel(new FlowLayout(FlowLayout.LEFT, 7, 10));
        titlePanel.setOpaque(false);
        label = new JLabel(UITools.getResourceString("label.automaton"));
        label.setFont(new Font("courier", Font.BOLD, 20));
        label.setOpaque(false);
        titlePanel.add(label);
        */

        JPanel centerPanel = new JPanel();
        centerPanel.setOpaque(false);
        label = new JLabel(UITools.getResourceString("label.app"));
        label.setFont(new Font("TimesNewRoman", Font.BOLD, 24));
        centerPanel.add(label);

        JPanel footer = new JPanel(new GridLayout(0, 1));
        footer.setBorder(BorderFactory.createEmptyBorder(0, 3, 0, 3));
        footer.setOpaque(false);
        label = new JLabel(UITools.getResourceString("label.version"));
        label.setForeground(Color.black);
        Font smallPrint = new Font("TimesNewRoman", Font.PLAIN, 10);
        label.setFont(smallPrint);
        footer.add(label);

        label = new JLabel(UITools.getResourceString("label.created_by"));
        label.setForeground(Color.black);
        smallPrint = new Font("TimesNewRoman", Font.PLAIN, 10);
        label.setFont(smallPrint);
        footer.add(label);

        //background.add(titlePanel, BorderLayout.NORTH);
        background.add(centerPanel, BorderLayout.CENTER);
        background.add(footer, BorderLayout.SOUTH);

        return background;
    }

    protected JPanel createBackground() {
        JPanel background = new PicturePanel();
        background.setLayout(new BorderLayout());
        return background;
    }

    /**
     * This is a convinience method. It is used to display the AboutBox.
     *
     * @param owner The owner of the dialog.
     */
    public static void handleDialog(JFrame owner) {
        try {
            AboutBox box = new AboutBox(owner);
        } catch (Throwable e) {
            UITools.handleThrowable(e);
        }
    }


    /**
     * This class is used to create a background panel.
     */
    private class PicturePanel extends JPanel {

        /**
         * The method is overriden in order to draw the background Image.
         */
        public void paintComponent(Graphics g) {
            super.paintComponent(g);
            g.drawImage(m_background, 0, 0, getWidth(), getHeight(), null);
        }
    }


    /**
     * An object of this class is notified via imageUpdate method about the
     * image loading. It is used to set the size of the dialog when the
     * image is loaded.
     */
    private class ImageHandler implements ImageObserver {

        private AboutBox m_aboutBox;

        /**
         * Constructor takes the dialog as parameter.
         *
         * @param aboutBox
         */
        public ImageHandler(AboutBox aboutBox) {
            m_aboutBox = aboutBox;
        }

        /**
         * This method is implementation of the ImageObserver method. It
         * is notified of the image loading progress and changes the
         * size of the dialog appropriately.
         */
        public boolean imageUpdate(Image img, int infoflags,
                                   int x, int y, int width, int height) {
            if ((infoflags & ImageObserver.ALLBITS) == ImageObserver.ALLBITS) {
                m_aboutBox.setSize(width, height);
                m_aboutBox.setLocationRelativeTo(m_parentFrame);
                m_aboutBox.setVisible(true);
                return false;
            }
            return true;
        }
    }

}
0
drjustin
Asked:
drjustin
  • 4
  • 3
1 Solution
 
aozarovCommented:
Maybe by the time you call:
int width = m_background.getWidth(observer);
int height = m_background.getHeight(observer);

The hight and width are already known, so no observer notifcation will happen (and this is where you put the visible(true))
Try adding after those two lines:

if (width > -1)
setVisible(true);
0
 
TimYatesCommented:
       public void actionPerformed(ActionEvent e) {
            System.out.println( "Pow!" ) ;
            AboutBox.handleDialog(RegExpFrame.this);
        }

does it print "Pow" every time?

If not, you aren't 'quite' clicking on the menu item...
0
 
drjustinAuthor Commented:
Aozarov : yep, this is it :-)

I show every time - but it blinks (since it is 'created' in the top left corner) now when placed in the middle of my JFrame.
How can I make it not blink?
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
aozarovCommented:
>> I show every time - but it blinks
What blinks?
0
 
drjustinAuthor Commented:
This AboutBox gets at first created in top left bottom, but only for a split.
Then it gets moved to the middle of my JFrame.
So it looks like it blinks.
0
 
aozarovCommented:
Did you add those two lines also (before the setVisible(true)) ?
m_aboutBox.setSize(width, height);
m_aboutBox.setLocationRelativeTo(m_parentFrame);

Is that happen always?
0
 
drjustinAuthor Commented:
Aozarov - :-)
0
 
aozarovCommented:
:-)
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 4
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now