Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / GUI / August 2006

Tip: Looking for answers? Try searching our database.

Help Menu - opens frame which does not go to front

Thread view: 
johnmmcparland - 29 Aug 2006 11:16 GMT
Hi all,

I'm using a JFrame to display help content in a JEditorPane. The help
frame is started up when the user clicks on the "Help" menu.
Unfortunately, the help frame does not have focus nor is it at the
front.

Good examples of the effect I'm looking for are in Firefox and Internet
Explorer. In Firefox, go to Help then Help Contents. In IE go to Help
then Contents and Index.

A small example of my current code is below.

package helpMenu;

import javax.swing.JEditorPane;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.event.MenuEvent;
import javax.swing.event.MenuListener;

public class MyWindow extends JFrame
{
   public MyWindow()
   {
       // JFrame settings.
       setTitle("Help Menu Demo");
       setSize(800,600);
       setLocationRelativeTo(null);
       setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

       // Menu bar / menus.
       JMenuBar menuBar= new JMenuBar();
       JMenu help= new JMenu("Help");
       help.addMenuListener(new MListener("Help"));
       menuBar.add(help);

       setJMenuBar(menuBar);
       setVisible(true);
   }

   // Inner class to handle JMenu actions.
   private class MListener implements MenuListener
   {
       String name;

       public MListener(String nm)
       {
           name= nm;
       }

       // Invoked when the menu is selected.
       public void menuSelected(MenuEvent me)
       {
          if (name.equals("Help"))
          {
             JFrame helpFrame= new JFrame("Help");
             helpFrame.setSize(200,500);

             JEditorPane helpContent= new JEditorPane();

             helpFrame.add(helpContent);

             helpFrame.setVisible(true);
          }
       }

       // Invoked when the menu is cancelled.
       public void menuCanceled(MenuEvent me)
       {

       }

       // Invoked when the menu is deselected.
       public void menuDeselected(MenuEvent me)
       {

       }
   }

    public static void main(String[] args)
   {
        new MyWindow();

    }

}


thanks,

john
johnmmcparland - 29 Aug 2006 14:17 GMT
Update on this,

I've got the help to the front by making it a JDialog and setting
toFront and the location.  But I cannot set it to be the currently
selected window.  Any ideas?

[code]

package helpMenu;

import javax.swing.JDialog;
import javax.swing.JEditorPane;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.event.MenuEvent;
import javax.swing.event.MenuListener;

public class MyWindow extends JFrame
{
    private JDialog helpDialog;
    private JEditorPane helpContent;

   public MyWindow()
   {
       // JFrame settings.
       setTitle("Help Menu Demo");
       setSize(800,600);
       setLocationRelativeTo(null);
       setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

       // Menu bar / menus.
       JMenuBar menuBar= new JMenuBar();
       JMenu help= new JMenu("Help");
       help.addMenuListener(new MListener("Help"));
       menuBar.add(help);

       setJMenuBar(menuBar);

       // The help dialog.
       setupHelpDialog();

       setVisible(true);
   }

   private void setupHelpDialog()
   {
       helpDialog= new JDialog(this, "MyWindow Help", false);
       helpDialog.setSize(250,700);

    helpDialog.setLocation(this.getSize().width,this.getSize().height-350);
       helpContent= new JEditorPane();
       helpDialog.add(helpContent);
       helpDialog.setVisible(false);
   }

   // Inner class to handle JMenu actions.
   private class MListener implements MenuListener
   {
       String name;

       public MListener(String nm)
       {
           name= nm;
       }

       // Invoked when the menu is selected.
       public void menuSelected(MenuEvent me)
       {
          if (name.equals("Help"))
          {
             helpDialog.toFront();
             helpDialog.setVisible(true);
          }
       }

       // Invoked when the menu is cancelled.
       public void menuCanceled(MenuEvent me)
       {
          helpDialog.setVisible(false);
       }

       // Invoked when the menu is deselected.
       public void menuDeselected(MenuEvent me)
       {

       }
   }

    public static void main(String[] args)
   {
        new MyWindow();

    }

}

[/code]
> Hi all,
>
[quoted text clipped - 89 lines]
>
> john
Andrew Thompson - 29 Aug 2006 14:44 GMT
> ..Any ideas?

Modal dialog?  (It is not quite the same as the way
the IE help works, but it might suit the requirement.)

Andrew T.
johnmmcparland - 29 Aug 2006 14:50 GMT
Hi Andrew,

yea a modal dialog will work but it's not the right way to do it.  If I
use that then the use cannot make changes to the main window (even if
it had content ;) ) but thanks anyway.

> > ..Any ideas?
>
> Modal dialog?  (It is not quite the same as the way
> the IE help works, but it might suit the requirement.)
>
> Andrew T.
Babu Kalakrishnan - 29 Aug 2006 15:57 GMT
> Hi Andrew,
>
> yea a modal dialog will work but it's not the right way to do it.  If I
> use that then the use cannot make changes to the main window (even if
> it had content ;) ) but thanks anyway.

Untested :
Try a non-modal (with a parent specified as your main frame), and try to
call toFront() on it.

BK

P.S. While replying in newsgroups, try to add your answer below relevant
lines of the message you're responding to. Top posting makes the thread
difficult to follow.
Andrew Thompson - 29 Aug 2006 18:29 GMT
...
> > yea a modal dialog will work but it's not the right way to do it.  If I
> > use that then the use cannot make changes to the main window (even if
[quoted text clipped - 3 lines]
> Try a non-modal (with a parent specified as your main frame), and try to
> call toFront() on it.

The second example actually does that, but now that I look
at it more closely, I note that setVisible(true) is called after
the call to toFront().

The OP might try calling toFront() and requestFocus() *after*
the dialog is visible..

Andrew T.
Ian Wilson - 31 Aug 2006 11:36 GMT
> ...
>
[quoted text clipped - 12 lines]
> The OP might try calling toFront() and requestFocus() *after*
> the dialog is visible..

I wonder if SwingUtilities.invokeLater may be of some relevance?

http://forum.java.sun.com/thread.jspa?threadID=666890&messageID=3905383

I use this idiom for a slightly related issue:
  SwingUtilities.invokeLater(new Runnable(){
    public void run(){
      [component].requestFocusInWindow();
    }
 });
Babu Kalakrishnan - 31 Aug 2006 12:41 GMT
>> The OP might try calling toFront() and requestFocus() *after*
>> the dialog is visible..
[quoted text clipped - 9 lines]
>     }
>  });

Even though the requestFocusInWindow() method is not really relevant
here (it serves the purpose of moving focus to a particular component
within the window - not the window itself), this message reminded me of
some discussions we had in this newsgroup several years ago - maybe
2000/2001 or so (Don't know if Google still has those archives - and as
to how relevant it would still be, because the focus subsystem in swing
has undergone drastic changes since then).

Basically the issue used to be that a component would not accept /
honour calls to requestFocus() till the Window was realized (or some
time that was slightly later than this) - so the workaround was to wrap
the requestFocus() call in a SwingUtilities.invokeLater wrapper so that
the actual call would take place only during the next cycle of the EDT.
And if I remember right, another solution that emerged during those
discussions was that one could actually put in the requestFocus() call
in a WindowOpened() eent handler of the window, and it would work fine
(whereas it used to fail even if the call was immediately after the
setVisible(true) call).

So may be the OP could try these workarounds :

1) Register a windowListener on this dialog and call toFront() from the
windowOpened() event handler.

2) If that also doesn't work, try to delay the call by one more EDT
cycle by wrapping it in an invokeLater wrapper.

BK


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.