Solved

Increasing the button size of showConfirmDialog and showMessageDialog

Posted on 2004-09-16
11
951 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
ID: 12073541
Set "____Yes____" and "____No____" on that buttons  (with _ being a blank)
0
 
LVL 37

Expert Comment

by:zzynx
ID: 12073544
That's for the width of course
0
 
LVL 37

Expert Comment

by:zzynx
ID: 12073643
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
Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

 
LVL 37

Expert Comment

by:zzynx
ID: 12073653
>> 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
ID: 12073721
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
 
LVL 24

Expert Comment

by:sciuriware
ID: 12074878
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
ID: 12284257
Please split.
;JOOP!
0
 
LVL 37

Expert Comment

by:zzynx
ID: 12284333
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
ID: 12284474
mmmmmmmmmmmmmmm
0
 
LVL 37

Expert Comment

by:zzynx
ID: 12285076
Thank you
0

Featured Post

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
micro services vs rest web services 16 103
backtracking recursion  code 19 57
javap bin 2 34
login form jsp example 2 24
For customizing the look of your lightweight component and making it look opaque like it was made of plastic.  This tip assumes your component to be of rectangular shape and completely opaque.   (CODE)
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
This video teaches viewers about errors in exception handling.

786 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