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.

Similar Messages

  • 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

  • Make modal dialog box "Always on Top"

    i know there has been discussion about Frame always on top etc. but i have a slightly different problem.
    I have a Modal Dialog box which goes to the background when the parent window loses focus and gains it back.
    i am working on java version1.4. So the new methof in 1.5 version is of no use. Also i am working on Windows XP SP1.
    Do i have to make the dialogbox extend OptionPane instead of JDialo, which i am doing rt now?
    but OptionPane comes with a baggage and unnecessary code which i have to implement etc.
    Is there any easier way to solve this issue.
    Thanks
    PM

    This solution works for me under J2SDK v1.4.2_03. If you were to use it on a large scale, you'd probably want to develop a bit more of a formalized framework for it.
            final JFrame test_frame = new JFrame("Test");
            test_frame.getContentPane().setLayout(new BoxLayout(test_frame.getContentPane(), BoxLayout.Y_AXIS));
            JPanel panel = new JPanel(new FlowLayout());
            test_frame.getContentPane().add(panel);
            final JDialog[] modal_dialog = new JDialog[]{null};
            final JDialog d = new JDialog(test_frame, "Bah", true);
            JButton close = new JButton("Close");
            d.getContentPane().add(close);
            close.addActionListener(new ActionListener()
                public void actionPerformed(ActionEvent e)
                    d.setVisible(false);
                    modal_dialog[0] = null;
            FocusListener fl = new FocusAdapter()
                public void focusGained(FocusEvent e)
                    if (modal_dialog[0]!=null)
                        modal_dialog[0].requestFocus();
            test_frame.addFocusListener(fl);
            JButton open = new JButton("Open");
            panel.add(open);
            open.addActionListener(new ActionListener()
                public void actionPerformed(ActionEvent e)
                    modal_dialog[0] = d;
                    d.pack();
                    d.setLocationRelativeTo(test_frame);
                    d.setVisible(true);
            test_frame.pack();
            test_frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            AWTUtilities.centerWindow(test_frame, null);
            test_frame.setVisible(true);

  • 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();
         }

  • Always on top window (not modal)

    Hi, everyone:
    Is there a way of making a window to be displayed always on top? I don't want it to be a modal window. I want the user to be able to flip to another (background) form, but still being able to fully see my current form on top.
    Thanks a lot.
    Regards,
    Eugene.

    Yes this is possible - if you are using Forms client server on windows you can find the code on Metalink (metalink.oracle.com) Note:68822.1

  • 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.

  • 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

  • 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

  • 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.

  • Firefox always on top (No Addon's installed) I moved away from chrome for this but it still happens in latest version?

    I have installed latest version of FF
    It started off great for a few hours and then it happened, ALWAYS ON TOP
    I have to shut down and start again
    no additional add-ons have been installed, it is out of the box
    Can you please fix this? I have the same issue with chrome which is a great browser however it actually is so problematic for me that I switched to mozilla. However you have the same problem.
    Help please.
    Regards
    -=D

    When this happens, can you force all windows to minimize as a workaround (instead of shutting down), then choose the desired window from the jump list on the Firefox icon on the task bar? Either:
    * Window key + d
    * Window key + m
    * click the unmarked vertical button at the far right end of the Windows task bar
    I can't think of a way for a web page to put itself in front of all other applications, but perhaps it could do so using a plugin.
    Have you noticed any pattern in the sites where this happens?

  • Setting always on top makes it on top of non java applications.

    Hi all..
    If i set always on top property for my frame then my frame is always on top of all the application.
    While i want the frame to be always on top of my Java application not with other windows or any other application.
    Could you suggest me how to proceed for it.

    Typically an application only has a single JFrame, so it is either active or not.
    If you have child windows related to the main frame that you want to also be active when the frame is active then you use a JDialog and specify the main frame as the owner.

  • How to open a form always on top but not at focus?

    I had a form application which will call another form as reference. May I know that is there any way I can to to set the called form always on top, but the focus is still set at the calling form so that I can still operate on the calling form?
    Many thanks
    MInny

    Hi:
    Thank you for reply to my question.
    I have tried your codes and apply them the following the form. However, I always got the following error:
    Error: (WWV-00000)
    No conversion performed for type INTEGER, value . (WWC-49102)
    No conversion performed for type INTEGER, value . (WWC-49102)
    No conversion performed for type INTEGER, value . (WWC-49102)
    No conversion performed for type INTEGER, value . (WWC-49102)
    My codes is:
    declare
    request_no number;
    blk varchar2(30) := 'DEFAULT';
    l_url varchar2(4000);
    begin
    request_no := p_session.get_value_as_NuMBER(
    p_block_name => blk,
    p_attribute_name => 'A_WORK_REQUEST_ID');
    l_url := 'portal30.wwa_app_module.link?p_arg_names=_moduleid&p_arg_values=11880470335&p_arg_names=_sessionid&p_arg_values=&p_arg_names=work_request_id&p_arg_values='||request_no;
    portal30.wwa_app_module.set_target(l_url,'call');
    end;
    then,
    I created another form which has two fields and the dattype of pk is varchar2 and change manually put the pk's p_arg_value into the url.
    e.g.
    l_url:='PORTAL30.wwa_app_module.link?p_arg_names=_moduleid&p_arg_values=9029417810&p_arg_names=WORK_AREA_CD&p_arg_values=APP';
    portal30.wwa_app_module.set_target(l_url,'call');
    However, I still get the error message as below:
    Error: (WWV-00000)
    No conversion performed for type INTEGER, value . (WWC-49102)
    No conversion performed for type INTEGER, value . (WWC-49102)
    No conversion performed for type INTEGER, value . (WWC-49102)
    No conversion performed for type INTEGER, value . (WWC-49102)
    No conversion performed for type INTEGER, value . (WWC-49102)
    Your help would be highly appreciated.
    Wei Ye

  • 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();
    }

  • JOptionPane show Message Dialog always on top found a Solution

    just thought of a solution for the age old java problem with message boxes / dialogs / input .
    It's funny when you google it you'll find like a million people asking for a solution but none or very few that gives an answer that works.
    this is probly not a new solution but i came up with this yesterday and thought i would "share".
    dunno why the java developers hasnt made a always on top function for this since there seems to be a very high demand for it.
    this hax will hopefully work at all times.
    hope google will find this thread, i searched for a solution yesterday and found none? which is surprising and im a pro googler too.
    import javax.swing.*;
    public class main{
         public static void main(String args[]){
              JFrame j=new JFrame();
              j.setAlwaysOnTop(true);
              j.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
              j.setVisible(true);
              j.setVisible(false);
              JOptionPane.showMessageDialog(j,"hi");
              System.exit(0);
    }

    I didn't realize it was an "age old problem." The JOptionPane's showXXX methods just show an ordinary modal dialog. If you want to set the alwaysOnTop property of said dialog then you just need to obtain a reference and call the method in question.
    JOptionPane op = new JOptionPane("hi",JOptionPane.INFORMATION_MESSAGE);
    JDialog dialog = op.createDialog("Message Dialog");
    dialog.setAlwaysOnTop(true); //<-- this line
    dialog.setModal(true);
    dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
    dialog.setVisible(true);No need for an intermediate frame.

Maybe you are looking for

  • Photo gallery page from iWeb '08 doesn't work for Mac OS X 10.3.9 user?

    I have recently published a photo gallery page with comments on using iWeb '08 (latest update). Displays fine for me and apparently a lot of other mixed-platform users. But one user is saying that his eMac with Mac OS X 10.3.9 and the Safari of the d

  • Class not found error on Firefox browser

    Requirement:- We have Apache server on the top of a tomcat container where we have deployed application Webnow. This application will provide SSO to another application Imagenow. The requirement is we have to authenticate and authorize a user based o

  • Can I pass a table using field-symbols to a PERFORM

    Can I pass an internal table using a field-symbol via a PERFORM that is stored in another program. For example, I want to pass lt_data using a field-symbol.  If I can do this, please tell me how to define a field-symbol for a table and how I setup th

  • How to fix this class code

    This program simply takes two string arguments supplied to it from the command line, and swaps them. Then it prints the results. The actual swapping is done by the class I made called SwapWords, and I think here is the problem. I don't quite understa

  • How do you edit a field in the Substitution Table GB01?

    I have a field in GB01 called BEXCLUDE and it is set to X.  I need to change it to space. There isn't a view for the table and the maintenance is set for restricted - which is worthless - SE54 will not all a table maintenance generator used without a