Results 1 to 2 of 2
  1. #1
    bigj is offline Member
    Join Date
    Dec 2009
    Posts
    68
    Rep Power
    0

    Default how can i make this dialog appear in the center of my screen

    Java Code:
    public class PopUpDialog extends JDialog {
        
        private JPanel popUpPanel;
    
        private JLabel errorMessage;
    
        private Image icon;
    
        private ImageIcon background = new ImageIcon("c:/pics/mainbck2.jpg");;
    
        private JButton okButton;
    
        private String errorNotificationFrameTitle;
        private String errorNotificationLabel;
    
        private int popUpWindowWidth;
        private int buttonXPosition;
    
        public PopUpDialog() {
            errorMessage = new JLabel("asdasdasd");
        }
        
        public void setDialogComponents(Container cont) {
    
            
           popUpPanel = new JPanel() {
    
                @Override
                public void paintComponent(Graphics g) {
    
                    // Scale image to size of component
                    Dimension d = getSize();
    
                    g.drawImage(background.getImage(), 0, 0, d.width, d.height, null);
    
                    super.paintComponent(g);
                }
            };
            popUpPanel.setOpaque(false);
    
            popUpPanel.setLayout(null);
    
            // set the properties of this label
            errorMessage.setText(errorNotificationLabel);
            errorMessage.setBounds(20, 5, 500, 30);
            errorMessage.setFont(new Font("SansSerif", Font.BOLD, 11));
    
            popUpPanel.add(errorMessage);
            
            // add the panel to the container
            cont.add(popUpPanel);
        }
        /**
         * Sets the error label message for this pop-up frame.
         *
         * If the message is too long, extend the width of the pop-up frame, and
         * make the button in the center by calculating its x coordinate according
         * to the width of the frame.
         *
         * @param labelMessage error message for this pop-up frame
         */
        public void setErrorNotificationLabel(String labelMessage) {
    
    
            if (labelMessage.length() > 50) {
    
                popUpWindowWidth = popUpWindowWidth + 300;
                buttonXPosition = buttonXPosition + 150;
            }
            else if (labelMessage.length() > 40) {
    
                popUpWindowWidth = popUpWindowWidth + 250;
                buttonXPosition = buttonXPosition + 120;
            }
            else if (labelMessage.length() > 30) {
    
                popUpWindowWidth = (popUpWindowWidth * 2) - 200;
                buttonXPosition = (buttonXPosition * 2) - 60;
            }
    
            errorNotificationLabel = labelMessage;
        }
    
        /**
         * Sets the error title for this pop-up frame.
         *
         * @param frameTitle title for the pop-up frame
         */
        public void setErrorNotificationFrameTitle(String frameTitle) {
    
            errorNotificationFrameTitle = frameTitle;
        }
    
        public void createDialog() {
    
            this.setDialogComponents(this.getContentPane());
            this.setTitle(errorNotificationFrameTitle);
            this.setLocationRelativeTo(null);
            this.setSize(350, 150);
            this.setVisible(true);
        }
        
        public static void main(String[] args) {
    
            SwingUtilities.invokeLater(new Runnable() {
    
                PopUpDialog pU = new PopUpDialog();
    
                public void run() {
                    
                    pU.createDialog();
                }
            });
        }
    }
    how can i make this appear in the middle of my scrren without setting is location manually?

    the method .setLocationRelativeTo(null) make a JFrame appear on the middle of the screen.. but this JDialog doesnt display correctly ..

    i created a JFrame for this dialog but im getting some errors too.. so i preffered to make a dialog by using JDialog... but im having problems in making it appear in the center...

    need help please... tnx in advance!!

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,361
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by bigj View Post
    [CODE]the method .setLocationRelativeTo(null) make a JFrame appear on the middle of the screen.. but this JDialog doesnt display correctly ..
    There must be another error somewhere; both a JFrame and a JDialog (indirectly) extend the Window class and that wanted method is defined in the Window class and inherited by the other classes; it works for me.

    kind regards,

    Jos

Similar Threads

  1. JFrame at Center Screen
    By hiranya in forum AWT / Swing
    Replies: 8
    Last Post: 02-11-2010, 04:29 PM
  2. Replies: 3
    Last Post: 01-16-2010, 07:14 PM
  3. How to make an icon in bottom-right of screen
    By hunterbdb in forum AWT / Swing
    Replies: 1
    Last Post: 08-13-2009, 08:28 AM
  4. Blank Screen while navigating from one screen to another
    By mohana.krishna in forum Java Servlet
    Replies: 0
    Last Post: 03-03-2009, 05:03 PM
  5. Replies: 4
    Last Post: 06-08-2007, 06:15 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •