How to add a maximize button to JDialog

How can I add a Maximize button to a JDialog or JOptionPane?
CharlesSHillAsked:
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

x
 
zzynxConnect With a Mentor Software engineerCommented:
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
 
Giant2Commented:
using a Frame.
0
 
sciuriwareCommented:
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
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
zzynxSoftware engineerCommented:
The demo code I posted does exactly what is aked for, so...
0
 
sciuriwareCommented:
Although a work-around, zzynx gave a perfect solution.
<*>
0
 
zzynxSoftware engineerCommented:
Thanks sciuriware :)
0
 
sciuriwareCommented:
No thanks, I was impressed.
<*>
0
 
zzynxSoftware engineerCommented:
Thank you for accepting Charles.
But, why a B-grade?
0
 
CharlesSHillAuthor Commented:
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
 
zzynxSoftware engineerCommented:
>> 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
All Courses

From novice to tech pro — start learning today.