Solved

Increasing the button size of showConfirmDialog and showMessageDialog

Posted on 2004-09-16
11
878 Views
Last Modified: 2008-01-16
hi,
 How to increase the size of YES_NO Button and OK Button of showConfirmDialog and showMessageDialog respectively.  Now it seems to be very small width and height when we view through 1024 resolution screen.

Regards

Anitha S
0
Comment
Question by:anithas
  • 7
  • 3
11 Comments
 
LVL 37

Expert Comment

by:zzynx
Comment Utility
Set "____Yes____" and "____No____" on that buttons  (with _ being a blank)
0
 
LVL 37

Expert Comment

by:zzynx
Comment Utility
That's for the width of course
0
 
LVL 37

Expert Comment

by:zzynx
Comment Utility
I mean

          Object[] options = { "       Yes        ", "       No       " };
          JOptionPane.showOptionDialog(null, "Are you sure to .... ?", "Title",
                 JOptionPane.DEFAULT_OPTION, JOptionPane.WARNING_MESSAGE,
                 null, options, options[0]);
0
 
LVL 37

Expert Comment

by:zzynx
Comment Utility
>> Now it seems to be very small width and height when we view through 1024 resolution screen.
I think it will follow the system's look and feel. So, not sure if it's a good idea to change that.
0
 
LVL 37

Accepted Solution

by:
zzynx earned 50 total points
Comment Utility
Here's what you want:

/*
 * JOptionPaneDemo.java
 *
 */

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
/**
 *
 * @author  zzynx
 */
public class JOptionPaneDemo extends JFrame {
   
    public JOptionPaneDemo() {
        init();
        setSize(500, 400);
        setLocationRelativeTo(null);
    }

    private void init() {
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosing(java.awt.event.WindowEvent evt) {
                System.exit(0);
            }
        });
       
        JButton button = new JButton("Go");
        button.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                buttonActionPerformed(evt);
            }
        });
       
        getContentPane().setLayout(new FlowLayout());
        getContentPane().add(button);
       
        pack();
    }
   
    private void buttonActionPerformed(java.awt.event.ActionEvent evt) {
        JOptionPane p = new JOptionPane("Are you sure you want to log out?",
            JOptionPane.QUESTION_MESSAGE, JOptionPane.YES_NO_OPTION);
        // Get the first button:
        JButton bYes = (JButton)((JPanel)p.getComponent(1)).getComponent(0);
        // Set it's preferred size
        // Remark: by setting the width of one, the other will follow.
        // To see what I mean comment out the line marked with /*[x]*/
        bYes.setPreferredSize( new Dimension(150, 40) );
        JButton bNo = (JButton)((JPanel)p.getComponent(1)).getComponent(1);
        bNo.setPreferredSize( new Dimension(150, 40) );  /*[x]*/
        final JDialog d = p.createDialog(this, "Report");
        FocusListener fl= new FocusAdapter() {
            public void focusGained(FocusEvent e) {
                JButton focusButton= (JButton)(e.getSource());
                d.getRootPane().setDefaultButton(focusButton);      
            }    
        };
        bYes.addFocusListener(fl);
        bNo.addFocusListener(fl);
        d.setLocationRelativeTo(this);
       
        bNo.requestFocus(); // <<< Set the default button here
        d.setVisible(true);
        Object value = p.getValue();
        if (value == null || !(value instanceof Integer)) {
            JOptionPane.showMessageDialog(null, "You closed without answering");
        }
        else    {
            int i = ((Integer)value).intValue();
            if (i == JOptionPane.DEFAULT_OPTION) {
                JOptionPane.showMessageDialog(null, "You pressed ESC");
            }
            else if (i == JOptionPane.NO_OPTION) {
                JOptionPane.showMessageDialog(null, "You choose \"No\"");
            } else if (i == JOptionPane.YES_OPTION) {
                JOptionPane.showMessageDialog(null, "You choose \"Yes\"");
            }
        }
    }

    public static void main(String args[]) {
        JOptionPaneDemo f = new JOptionPaneDemo();
        f.show();
    }
   
}
0
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 
LVL 24

Expert Comment

by:sciuriware
Comment Utility
You can also get what you want by:

   Font buttonFont = new Font("Ms Sans Serif", Font.BOLD, 12);

               UIManager.put("Button.font", buttonFont);

Then ALL buttons get the new font; you can choose another or bigger font if you like.

;JOOP!
0
 
LVL 24

Expert Comment

by:sciuriware
Comment Utility
Please split.
;JOOP!
0
 
LVL 37

Expert Comment

by:zzynx
Comment Utility
Sorry sciuriware, but I really think my last comment is the solution.
(S)he asked for an increase of the button sizes. (S)he didn't told (s)he wants the font to be bigger.
0
 
LVL 24

Expert Comment

by:sciuriware
Comment Utility
mmmmmmmmmmmmmmm
0
 
LVL 37

Expert Comment

by:zzynx
Comment Utility
Thank you
0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
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…
This video teaches viewers about errors in exception handling.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

772 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

15 Experts available now in Live!

Get 1:1 Help Now