Solved

How to add a maximize button to JDialog

Posted on 2004-10-21
4,772 Views
Last Modified: 2008-02-26
How can I add a Maximize button to a JDialog or JOptionPane?
0
Question by:CharlesSHill
    10 Comments
     
    LVL 12

    Expert Comment

    by:Giant2
    using a Frame.
    0
     
    LVL 24

    Expert Comment

    by:sciuriware
    You can't maximise a JDialog or JOptionPane.
    You can add your own button, but you can only .setSize() to the size from:
              Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
    That's all.
    ;JOOP!
    0
     
    LVL 37

    Accepted Solution

    by:
    Here's some demo code:

    /*
     * MaxDialogDemo.java
     *
     */

    import java.awt.*;
    import javax.swing.*;
    /**
     *
     * @author  zzynx
     */
    public class MaxDialogDemo extends javax.swing.JPanel {

        JDialog parentDialog = null;
       
        public MaxDialogDemo(JDialog parent) {
            parentDialog = parent;
            initComponents();
        }
       
        private void initComponents() {
            centerPanel = new javax.swing.JPanel();
            southPanel = new javax.swing.JPanel();
            buttonPanel = new javax.swing.JPanel();
            maxButton = new javax.swing.JButton();
            standardButton = new javax.swing.JButton();

            setLayout(new java.awt.BorderLayout());

            add(centerPanel, java.awt.BorderLayout.CENTER);

            maxButton.setText("Maximize");
            maxButton.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    maxButtonActionPerformed(evt);
                }
            });

            buttonPanel.add(maxButton);

            standardButton.setText("Back to normal");
            standardButton.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    standardButtonActionPerformed(evt);
                }
            });

            buttonPanel.add(standardButton);

            southPanel.add(buttonPanel);

            add(southPanel, java.awt.BorderLayout.SOUTH);

        }

        private void standardButtonActionPerformed(java.awt.event.ActionEvent evt) {
            parentDialog.hide();
            parentDialog.setSize(600,400);
            parentDialog.setLocationRelativeTo(null);
            parentDialog.show();
        }

        private void maxButtonActionPerformed(java.awt.event.ActionEvent evt) {
            parentDialog.hide();
            parentDialog.setLocation(0,0);
            Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
            parentDialog.setSize(screenSize.width, screenSize.height);
            parentDialog.show();
        }
       
       
        private javax.swing.JPanel buttonPanel;
        private javax.swing.JPanel centerPanel;
        private javax.swing.JButton maxButton;
        private javax.swing.JPanel southPanel;
        private javax.swing.JButton standardButton;
       
        static public void main(String[] args) {
          JFrame x = new JFrame("Maximized Framed");
          x.setLocationRelativeTo(null);
          x.setExtendedState(Frame.MAXIMIZED_BOTH);
          x.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);  
          x.setTitle("Maximize Dialog Demo");
          x.setSize(300, 300);
          x.setVisible(true);
         
          JDialog dlg = new JDialog(x, "Demo dialog", true);
          dlg.getContentPane().setLayout(new BorderLayout());
          dlg.getContentPane().add(new MaxDialogDemo(dlg), BorderLayout.CENTER);
          dlg.pack();
         
          dlg.setSize(600,400);
          dlg.setLocationRelativeTo(null); // center
          dlg.show();
        }
    }
    0
     
    LVL 37

    Expert Comment

    by:zzynx
    The demo code I posted does exactly what is aked for, so...
    0
     
    LVL 24

    Expert Comment

    by:sciuriware
    Although a work-around, zzynx gave a perfect solution.
    <*>
    0
     
    LVL 37

    Expert Comment

    by:zzynx
    Thanks sciuriware :)
    0
     
    LVL 24

    Expert Comment

    by:sciuriware
    No thanks, I was impressed.
    <*>
    0
     
    LVL 37

    Expert Comment

    by:zzynx
    Thank you for accepting Charles.
    But, why a B-grade?
    0
     

    Author Comment

    by:CharlesSHill
    Sorry, the selections said xxx, Good, Excellent, I did not know they were translating to A,B,C. I thought the solution was quite fine so I assigned Good.
    0
     
    LVL 37

    Expert Comment

    by:zzynx
    >> I thought the solution was quite fine
    You bet ;°)
    Just wondered what you expected as an answer that deserves an A grade?

    Never mind.
    0

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Highfive + Dolby Voice = No More Audio Complaints!

    Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

    Suggested Solutions

    Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
    Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
    Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
    This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

    884 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    20 Experts available now in Live!

    Get 1:1 Help Now