JDialog Non Modal....Default button remains

I've created a non modal JDialog
By doing the following
1. Creating an object of JOptionPane(message, JOptionPane.ERROR_MESSAGE)
2. Adding the JOptionPane object to the ContentPane
3. Creating a button
4. Adding button("End Application") to ContentPane
5. Adding listeners to window and button
The result is a dialog
Message
ICON "OK" Button...............................NOT REQUIRED
"End Application" Button
I don't require the "OK" Button. Is there a way of removing this button. I need the dialog box to be modal so can't use showMessageDialog, rename "Ok" button to "End Application" etc.
Should I be taking a different approach ie Not adding JOptionPane to JDialog??
If the above isn't possible. Do you know where I can find the default error gif on error dialog boxes?
Kind regards,
Jean

Get the error icon with UIMananger.getIcon("OptionPane.errorIcon"). It might throw exceptions while painting because it's expecting to be painting in a JOptionPane. Just catch and ignore the exceptions and it will paint alright.

Similar Messages

  • Change a modal JDialog to non-modal JDialog

    I created a modal JDialog initially, for some result i need change it to a non-modal JDialog, is there any set modal command can do it??

    Nope.
    The reason is because of program flow:
    System.out.println("first line");
    JDialog dialog = new JDialog(modal);
    dialog.setVisible(true); // blocks this flow if and only if modal is true
    System.out.println("second line");
    Try putting a button in the dialog which prints out "button pressed".
    If you press the button each time before closing the dialog you will get:
    when modal is true:
    first line
    button presed
    second line
    when modal is false:
    first line
    second line
    button prese

  • Enabling non-modal JDialog

    I have created a JDialog which is non-modal. This dialog has lot of controls viz. buttons, combo box etc. This dialog is launched on clicking a button placed on panel. Surprisingly, when the dialog is launched, all components are disabled. Is there any way I can make these enabled? There is no line of code which has been done to change the behaviour of these components. They work fine if I make the dialog modal. Please advice.

    Thanks.
    The constructor for creating the dialog is pasted as below:
    public AbstractDialog(Frame owner, String title, boolean isModal) {
              super(owner, title, isModal);
              JRootPane rootPane = getRootPane();
              Action escapeKeyAction = new AbstractAction() {
         public void actionPerformed(ActionEvent e) {
         AbstractDialog.this.dispose();
              rootPane.registerKeyboardAction( escapeKeyAction,
    KeyStroke.getKeyStroke( KeyEvent.VK_ESCAPE, 0 ),
    JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT );
              UFSAFocusTraversalPolicy obj = new UFSAFocusTraversalPolicy ();
              //obj.setImplicitDownCycleTraversal(false);
              this.setFocusTraversalPolicy(obj);
              this.setDefaultCloseOperation(JDialog.DO_NOTHING_ON_CLOSE);
         this.setFocusCycleRoot(true);
         this.addWindowListener(windowAdapter);
    The isModal passed is false.

  • Non modal JDialog

    Hi All,
    I want a non modal JDialog which is used to show the message to the user that it is searching for the records, when a search is performed and records are retrieved from the database. When the search is going on user might hit cancel on the JDialog to cancel the search. My problem is, the cancel button on the Search dialog is not catching the event and user is not able to select the cancel option on the dialog.
    Here is my code :
    class SearchWindow extends JDialog {
    private JPanel btnPanel;
    private JLabel lblSearch;
    private JButton btnCancel;
    * Constructor
    public SearchWindow() {
    super((Frame)null, false);
    setTitle("Searching Shipment Legs");
    cancelled = false;
    addWindowListener(new WindowAdapter() {
    public void windowClosing(WindowEvent evt) {
    close();
    Container contentPane = getContentPane();
    contentPane.setLayout(null);
    addButtons();
    pack();
    Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
    setSize(new Dimension(300, 100));
    setSize(300,100);
    setLocation((screenSize.width-700)/2,(screenSize.height-450)/2);
    private void addButtons() {
    btnPanel = new JPanel();
    btnPanel.setLayout(new BorderLayout());
    lblSearch = new JLabel("Searching..........");
    btnCancel = new JButton("Cancel");
    btnPanel.setBackground(Color.lightGray);
    lblSearch.setBackground(Color.lightGray);
    btnCancel.setBackground(Color.lightGray);
    btnPanel.add(lblSearch, BorderLayout.CENTER);
    btnPanel.add(btnCancel, BorderLayout.SOUTH);
    btnCancel.addMouseListener(new MouseAdapter() {
    public void mousePressed(MouseEvent evt) {
    System.out.println("cancel");
    cancelled = true;
    close();
    btnPanel.setBounds(0,0,200,50);
    this.getContentPane().add(btnPanel);
    lblSearch.setVisible(true);
    btnCancel.setVisible(true);
    this.getContentPane().validate();
    public void close() {
    this.setVisible(false);
    this.dispose();
    public void show() {
    super.show();
    paintComponents(getGraphics());
    this.setModal(false);
    Any help is greatly appreciated.
    Thanks,
    Bhaskar

    Hi Haroldsmith
    I am calling this search window in one of my programs where on button click it will fetch the records from the database. When this process is on, search dialog is shown up. Ths dialog is shown correctly and its getting closed as soon as the search is completed. But the probelm is its not allowing me to click on cancel button.
    Bhaskar

  • Non-modal JDialog is not painted and blocks the GUI

    I have developed a GUI that's basically a JFrame with a JDesktopPane.
    The user can, via a menu item, pop up a JDialog that contains some JLists and then select some value from it. Once he/she has done the selection and clicks on OK, the dialog disappears (data processing is then done in the main GUI) and comes back once a specific event has happened. The user then selects other data and so on, until he/she clicks on Cancel, which definitely disposes of the JDialog.
    The graphics of the JDialog are build in the class constructor, which does a pack() but does not make the dialog visible yet. The dialog appears only when doSelection() is called.
         /** Called the first time when user selects the menu item, and then
         when a specific event has happened. */
         public Data[] doSelection() {
              dialog.setVisible(true);
              // ... Code that reacts to user's input. Basically, the ActionListener
              // added to the buttons retrieves the user's selection and calls
              // dialog.setVisible(false) if OK is clicked, or calls dialog.dispose()
              // if Cancel is clicked.
         }Now, everything works fine if the JDialog is modal, but if I make it non-modal only the window decorations of the JDialog are painted, and the control doesn't return to the main GUI. Calling doLayout() or repaint() on the doSelection() has no effect. How can this be fixed?
    I hope I have been able to explain the problem satisfactorily, I could not create a suitable SSCCEE to show you. Thanks in advance for any hint.

    Ok, I've taken some time to think about this problem and I've modified the code a bit.
    Now the dialog shows itself and is responsive (i.e. its JLists can be operated), but the Ok button does not close the dialog as I'd want. I believe that I'm messing up things about threading, and the operations in actionPerformed() should be carried out in another thread, if possible.
    Thanks in advance for any hint / suggestion / comment / insult.
         private Data[] selection;
         /** Constructor */
         public MyDialog() {
              // ... Here is the code that builds the dialog...
              dialog.setModal(false);
              dialog.pack();
              // Note that the dialog is not visible yet
         public Data[] doSelection() {
              operatorAnswer = NONE_YET;
              dialog.setVisible(true);          
              while (operatorAnswer == NONE_YET) {
                   try {
                        wait();
                   } catch (InterruptedException e) { }
              return (operatorAnswer == OK ? selection : null);
         public void actionPerformed(ActionEvent evt) {
              if (okButton.equals(evt.getSource())) {
                   operatorAnswer = OK;
                   retrieveSelection();
                   dialog.setVisible(false);
              else if (cancelButton.equals(evt.getSource())) {               
                   operatorAnswer = CANCEL;
                   dialog.dispose();
         private void retrieveSelection() {
              // ... Here is the code that retrieves selected data from the dialog's JLists
              // and stores it in the "selection" private array...
              notifyAll();
         }

  • How do I create a Modal JDialog (non-bypassable when visible)?

    After customising a JDialog, I found that it is basically ignored after it has been created, by the main program. I've tried many ways to stop this from happening, searched the Java tutorial and googled it, but cannot find out how to do so. I have used the constructor for a modal Dialog:
    JDialog(Dialog owner, String title, boolean modal)However, my methods are still treated like a non-modal Dialog, as they are ignored. Is there a certain method I have to call to make this Dialog modal (not able to be bypassed when made visible). If so then I would appreciate it if anybody could help, or perhaps point me along the right way.

    JDialog(Dialog owner, String title, boolean modal)How do you expect us to solve your problem based on a single line of code? Create a simple demo program that shows the problem
    Here is a simple demo that works.
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    public class DialogModal extends JFrame implements ActionListener
         public DialogModal()
              JButton button = new JButton("Show Dialog");
              button.addActionListener( this );
              getContentPane().add( button );
         public void actionPerformed(ActionEvent e)
              JDialog dialog = new JDialog(this, "Modal Test", true);
              dialog.setSize(300, 300);
              dialog.show();
         public static void main(String[] args)
              JFrame frame = new DialogModal();
              frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
              frame.pack();
              frame.setLocationRelativeTo( null );
              frame.setVisible(true);
    }

  • Is there a way to display a  non modal JDialog on JApplet

    Whenever I try to add a non modal JDialog over a JApplet, the JDialog freezes and components on it never gets painted. After a disappointing search over web, I've kinda begin to hate swing. I am shocked that a very basic thing like this is so hard to achieve in Java. Any solution folks?
    My code is as follows:
    import java.awt.Frame;
    import javax.swing.JApplet;
    import javax.swing.JDialog;
    import javax.swing.JLabel;
    import javax.swing.JButton;
    public class DialogApplet extends JApplet {
         private javax.swing.JPanel jContentPane = null;
         private JButton jButton = null;
          * This method initializes jButton     
          * @return javax.swing.JButton     
         private JButton getJButton() {
              if (jButton == null) {
                   try {
                        jButton = new JButton();
                        jButton.setText("Click Me"); 
                        jButton.setBounds(75, 80, 147, 34); 
                        jButton.addActionListener(new java.awt.event.ActionListener() {
                             public void actionPerformed(java.awt.event.ActionEvent e) {   
                                  Frame f = javax.swing.JOptionPane.getFrameForComponent(jContentPane);
                                  JDialog pi = new JDialog(f, "MainFrame Dialog", false);
                                pi.getContentPane().add(new JLabel("I got to be working Bossie!!!"));
                                pi.pack();
                                pi.setLocation(75, 80);
                                pi.setVisible(true);
                                try {
                                Thread.sleep(10000);
                            } catch (InterruptedException e1) {
                                e1.printStackTrace();
                                pi.setVisible(false);
                   catch (java.lang.Throwable e) {
                        e.printStackTrace();
              return jButton;
         public static void main(String[] args) {
          * This is the default constructor
         public DialogApplet() {
              super();
              init();
          * This method initializes this
          * @return void
         public void init() {
              this.setSize(300,200);
              this.setContentPane(getJContentPane());
          * This method initializes jContentPane
          * @return javax.swing.JPanel
         private javax.swing.JPanel getJContentPane() {
              if(jContentPane == null) {
                   jContentPane = new javax.swing.JPanel();
                   jContentPane.setLayout(null);
                   jContentPane.add(getJButton(), null); 
              return jContentPane;
    }

    try this, it is really simple, just to look at
    example from the swing tutorials.
    Thanks my friend, If you carefully observe my code, I also needed some piece of code to run in the background ((The thread.sleep() part)) while displaying the dialog. The problem was the JDialog used to freeze and components on it never used to get painted. Finally I managed to find a way out. If we try to display a JDialog with a new thread, The event dispatching thread takes precedence and the painting of components on the JDialog happens only after even dispatching thread is done. All I did was display JDialog in the event dispatch thread and run the background process in new thread.
    My code.
    import java.awt.BorderLayout;
    import java.awt.Container;
    import java.awt.Frame;
    import javax.swing.JApplet;
    import javax.swing.JButton;
    import javax.swing.JDialog;
    import javax.swing.JLabel;
    import javax.swing.JOptionPane;
    import javax.swing.JPanel;
    public class DialogApplet extends JApplet {
        private javax.swing.JPanel jContentPane = null;
        private JButton jButton = null;
        private JDialog dialog = null;
         * This is the default constructor
        public DialogApplet() {
            super();
            init();
         * This method initializes this
        public void init() {
            this.setSize(300, 200);
            Container container = this.getContentPane();
            container.add(getJContentPane());
         * This method initializes jContentPane
         * @return javax.swing.JPanel
        private javax.swing.JPanel getJContentPane() {
            if (jContentPane == null) {
                jContentPane = new javax.swing.JPanel();
                jContentPane.setLayout(null);
                jContentPane.add(getJButton(), null);
            return jContentPane;
         * This method initializes jButton
         * @return javax.swing.JButton
        private JButton getJButton() {
            if (jButton == null) {
                try {
                    jButton = new JButton();
                    jButton.setText("Click Me");
                    jButton.setBounds(75, 80, 147, 34);
                    jButton.addActionListener(new java.awt.event.ActionListener() {
                            public void actionPerformed(java.awt.event.ActionEvent e) {
                                showDialog();
                } catch (java.lang.Throwable e) {
                    e.printStackTrace();
            return jButton;
         * This method displays Dialog
        public void showDialog() {
            final Frame frame = JOptionPane.getFrameForComponent(this);
            dialog = new JDialog(frame, "DialogApplet", false);
            dialog.setModal(true);
            Container contentPane = dialog.getContentPane();
            JPanel panel = new JPanel(new BorderLayout());
            panel.add(new JLabel("I got to be working Bossie!!!"), BorderLayout.CENTER);
            contentPane.add(panel);
            dialog.pack();
            Thread t = new Thread() {
                    public void run() {
                        for (int i = 0; i < 100000; ++i) {
                            System.out.println(i);
                        dialog.hide();
            t.start();
            dialog.show();
    }

  • A non-modal, but "always on top" JDialog

    Hi everyone,
    In an applet I am creating, I need to create a non-modal JDialog which stays on top whenever its open. The reason this JDialog is required to be non-modal is that I need the main applet window to be able to react to events and reflect changes in the non-modal JDialog.
    I was wondering how this can be achieved.
    To summarize the problem:
    How can you have a JDialog whose modal property is "false", but stays on top of the applet window.
    This problem has been bugging me for quite a while now, so any pointers will be appreciated.
    Thanks,
    Alan

    Here is how you do it in an applet (as opposed to JApplet):
    * Browser sniffer *
          boolean isNN4=false,isNN6=false,isIE=false;
          Frame F;
          JSObject window=JSObject.getWindow(this);
          JSObject document=(JSObject)window.getMember("document");
          String tmp=document.getMember("all").toString();
          if (tmp.equals("undefined")) {
             tmp=document.getMember("getElementById").toString();
             if (tmp.equals("undefined")) isNN4=true;       // Netscape Navigator 4
             else isNN6=true;                               // Netscape Navigator 6
          } else isIE=true;                                 // MS Internet Explorer
          if (isIE) {
             F=new Frame();
          } else F=(Frame)getParent();
    now you have a frame for use with the Dialog (not JDialog) method
    ...Just add the code shown above in the init method of your applet.
    Good Luck!
    ;o)
    V.V.

  • JButton on XP non-modal Jdialog doen't work.

    I recently upgraded from 1.3 to 1.4 while upgrading my OS from NT 4.0 to XP.
    In a stand-alone application I have non-modal JDialog's that are launched as threads so that I can have multiple instances of them running simultaneously. There are JButton's on the JDialog's which no longer respond to any mouse events even though I can execute them via the keyboard using the tab and enter keys. They work just fine under 1.3 or if I make them modal.
    Any thoughts?

    Hello,
    Issue is-
    There are JButton's on the JDialog's which no longer respond to any mouse events even though I can execute them via the keyboard using the tab and enter keys. They work just fine if I make them modal.
    Plz suggest

  • 1. Non-modal popup or 2.minimize button on titlebar of popup

    Hi All,
    Firstly, is it now possible to open non-modal pop-ups ( as many pop-ups after executing an action method :( ) , or has anyone come across this found a work-around for this ?
    currently, I am opening a modal dialog itself. But when I click the minimize ( since minimize, maximize and close buttons are activated in the title bar ), the popup minimizes and maximises immediately. This is now raised as a bug and I need to find a solution for this.
    Would much appreciate help,comments or views on this ?
    -- Urvi

    Gor_Mahia wrote:
    ok i got what you mean but its an option for ver1.0 not version 2.0 of the same plugin..thanks.Well, this is an option in the Apex framework for dynamic actions. In Apex 4.2, the options for "Event Scope" are Static, Dynamic and Once.
    In your case, you can set it to live.

  • Non-modal JDialog hides its JFrame "owner"

    I'm writing my first significant swing application. It has a JFrame that launches a JDialog. When constructing the JDialog, I have the JFrame as the owner and set it to be non-modal. The dialog is indeed non-modal, since it allows me to click on the JFrame and run it. However, if the two windows overlap, the JDialog is always on top, covering the JFrame, even if the JFrame has focus. This can make it difficult to effectively work with the JFrame while the JDialog is on the screen.
    There must be a simple solution to this, but I haven't discovered it yet. Any ideas?

    Thank you so much for your reply. I just confirmed that passing null to the JDialog does indeed allow the JFrame to overlay it. However, now if I minimize the JFrame, the JDialog stays up. I'd prefer that it go disappear with the JFrame. Is there a way to "have it both ways"? That is, Is there a way to make the JDialog go away when the JFrame is minimized, but still make it possible for the JFrame to overlay the JDialog?
    Thanks again for the original reply.
    TIA on this one, too!
    Tim

  • Focus loss? bad modality? wrong default buttons?

    Hi Fort&eacute; Users,
    Can I ask if anyone has experienced any of the following problems?
    We are using Fort&eacute; 3.0.G2 on NT4.0 SP3 using Compaq Deskpro PCs.
    1. Occasionally, the modality is lost when a modal child window is
    displayed for the first time.
    2. Sometimes the closing of a child window doesn't correctly return the
    focus to the parent window. This may be related to using the keyboard to
    "click" the default button rather than actually clicking with the mouse, but
    I'm not sure of that.
    3. Also the default button is set correctly when a child window closes,
    but the window indicates that a different button is the default button. The
    highlighting is on the wrong button.
    All these problems happen in both distributed and standalone runs.
    I ask because I don't want to waste time with Fort&eacute; Support if they aren't a
    generic problem.
    Thanks,
    Nick.
    To unsubscribe, email '[email protected]' with
    'unsubscribe forte-users' as the body of the message.
    Searchable thread archive <URL:http://pinehurst.sageit.com/listarchive/>

    Troubleshooting Update:
    If I first import my project using the RH7 WYSIWYG as default
    editor, the Russian ToC is produced. I can then change the default
    editor, (say, to Word2003 or FrontPage) and the Russian ToC is then
    preserved. Very strange!
    That still leaves a css problem. On import of a Word file,
    the styles belong to some unknown (default?) stylesheet, and
    editing and correcting is an intolerable job. Why is the Word
    template not being imported to become the RH7 topic template?
    Your assistance much appreciated.

  • Shortcut for selecting non-default button in dialog boxes

    I seem to remember that there was a keyboard shortcut many OSs ago. I can't seem to find it or replicate it. Particularly, I would like to choose the non-default button in a dialog box in Acrobat Pro for productivity's sake.
    Thanks, Jon

    Hi Jon, and a warm welcome to the forums!
    No way to tell if Adobe follows the standard, but TAB generally moves through the Dialog buttons & Space Bar selects that choice where Enter/Return chooses the default blue button. Sometimes the CMD key + the first letter of the button selects it iirc. Sometimes the esc key passes for Cancel.
    If it's not, in Sys Prefs change if the “Full keyboard access” under keyboard shortcuts is set to use text boxes and lists only, switch it to “All controls”.
    http://forums.macnn.com/90/mac-os-x/401945/keyboard-shortcuts-for-dialog-pop-ups /

  • Non modal optionpane

    hello,
    i wanted to use an option pane with a string array to display a list
    i thought using the api would be easier because all of the formating and things is allready done but i want it to be non modal,
    i found this thread [http://forum.java.sun.com/thread.jspa?threadID=744269&messageID=4262681] but i dont really understand whats goin on, i dont need to know anything about the users interaction with this dialog box
    thanks in advance
    alex

    The api gives an example. something like this has worked for me:
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import javax.swing.JButton;
    import javax.swing.JDialog;
    import javax.swing.JFrame;
    import javax.swing.JOptionPane;
    import javax.swing.SwingUtilities;
    public class NonModalJOptionPane extends JFrame
        public NonModalJOptionPane()
            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            JButton button = new JButton("Press me");
            button.addActionListener(new ActionListener()
                public void actionPerformed(ActionEvent arg0)
                    JOptionPane myOptionPane = new JOptionPane("Hey, hey, my, my",
                        JOptionPane.INFORMATION_MESSAGE, JOptionPane.OK_OPTION);
                    JDialog dialog = myOptionPane.createDialog(
                        NonModalJOptionPane.this, "Rock and Roll Will Never Die");
                    dialog.setModal(false);
                    dialog.setVisible(true);
            add(button);
            pack();
            setLocationRelativeTo(null);
        public static void main(String[] args)
            SwingUtilities.invokeLater(new Runnable()
                public void run()
                    new NonModalJOptionPane().setVisible(true);
    }

  • Default Button: switched to last focussed, but want Windows behavior, how?

    In a JDialog, if I add components and set one to be the default button, plus
    use the Windows L&F, I expect the Windows L&F behavior, that is, as long
    as I tab along buttons, an Enter will activate a button if it has focus, but if I
    tab to another component, the desired default button should be visually marked (darker edge)
    and an Enter should activate it.
    In Plugin's JRE 1.3.1, this is not the case. The default button is reset to the last button that had
    the keyboard focus.
    In a JVM 1.2.2, the same code produced the expected result!
    The bug db listed a few solutions, one of them was to use a Focus Manager to
    track focus and then "manually" reset the default button each time the focus is not on a button.
    Anyone out there with a more details on this solution?
    Or perhaps, there is a better solution than tracking focus events manually?
    Sylvia

    Found a solution myself, which at least works for windows l&f:
    make all buttons non-default capable (setDefaultCapable(false)), except for the one button, that is to be the default button. This has the unfortunate effect, that a non-default button, which has input focus, does not react to to ENTER, but the ENTER is sent to the default button instead.
    So must add a keyboard action to handle the ENTER:
    KeyStroke keyStroke = KeyStroke.getKeyStroke ("ENTER");
    ActionListener action = new ActionListener(){
    public void actionPerformed() {getMyButton().doClick();}};
    getMyButton().registerKeyboardAction (action, keyStroke, WHEN_FOCUSED);
    That did it for my case.
    Sylvia

Maybe you are looking for

  • How to Use a Certificate for Two Way SSL and another certificate for WS Security Header at Client Console Application(C# Dotnet)

    Hi, I want to consume a Java Web service from Dotnet based client Application. The service require one Certificate("abc.PFX") for Two Way SSL purpose and another certificate("xyz.pfx") for WS security purpose to be passed from client Application(Dotn

  • BackUp Question External Drive

         So I dont like Lion and by the looks of things Mountain Lion is the same. I'm going to be downgrading to SnowLeopard and I got a question. I bought a MyPassport External Drive for Mac and I was wondering something. If I am on Lion now and I back

  • I've made a mess of my VI and I'm lost

    Anybody that could help would be helping my immensely. I have incorporated a new subVI into this simple genetic algorithm VI. The problem is that originally, initially an array was formed. Now a cluster is formed and it has messed up the rest of my V

  • Appearing Duplicate column in  ABAP QUERY  output list.

    Hi ,          I have created a report using SAP QUERY having issue on output display  showing duplicate columns  name Weight Unit (LIKP-GEWEI ) and rest          of  field area  coming fine .               Kindly help to solve the issue .          Th

  • What's the point on using KF in addition when loading datastore objects?

    Hi all, i'd like to know why sometimes KFs are uploaded in addition even when loading a dso. I mean, when KF are all in overwrite it's easier to do a full repair request if something goes wrong or you are loading a new field in BW, so why some KFs ar