Solved

.requestFocus() doesn't seem to be working

Posted on 2008-06-10
5
1,206 Views
Last Modified: 2013-12-29
GuiDialogMessageMulti.scroll (not to be confused by a variable with the same name in the inner class) doesn't seem to be getting/maintaining focus correctly.

Any ideas as to why?
//=======================================================================================================================================================================
 
package com.floorsoft.floorwizard3_1.gui;
 
import javax.swing.JScrollPane;
import javax.swing.JEditorPane;
import javax.swing.BorderFactory;
import javax.swing.JDialog;
import javax.swing.SwingConstants;
import java.util.ArrayList;
import java.awt.event.ComponentEvent;
import java.awt.event.ComponentListener;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.Color;
import java.awt.Rectangle;
import java.awt.Cursor;
import java.awt.Point;
 
public class GuiDialogMessageMulti extends JDialog implements ActionListener, ComponentListener
{
//----------Class Variables----------------------------------------------------------------------------------------------------------------------------------------------
 
  ArrayList<Entry> lstEntry = new ArrayList<Entry>();
  final GuiCardPanel cards = new GuiCardPanel();
  GuiPanel pnlNavigation = new GuiPanel();
  GuiScrollPane scroll = null;
  GuiPanel pnlSubNav = null;
  GuiButton btnClose = null;
 
  int minHeightOfPnlSubNav = 0;
 
  int i = 1;
 
//----------Constructors-------------------------------------------------------------------------------------------------------------------------------------------------
 
  public GuiDialogMessageMulti()
  {
    setFocusable(false);
    cards.setFocusable(false);
    add(cards);
    scroll = pnlNavigation.addScrollPanePanel(10,10,50,50,(pnlSubNav = new GuiPanel(false)),JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,JScrollPane.HORIZONTAL_SCROLLBAR_NEVER,50,50);
    scroll.setBorder(BorderFactory.createLineBorder(Color.BLACK,1));
    btnClose = pnlNavigation.addButton(10,10,120,20,"Close");
    pnlSubNav.setBackground(new Color(255,255,255));
    rebuild();
    addComponentListener(this);
    btnClose.addActionListener(this);
    btnClose.setFocusable(false);
    pnlNavigation.setFocusable(false);
    pnlSubNav.setFocusable(false);
  }
 
//----------Implemented Methods------------------------------------------------------------------------------------------------------------------------------------------
 
  public void actionPerformed(ActionEvent event)
  {
    Object source = event.getSource();
    if (source.equals(btnClose)) super.setVisible(false);
    for (Entry entry : lstEntry) if (source.equals(entry.btnClose))    super.setVisible(false);
    for (Entry entry : lstEntry)
      if (source.equals(entry.btnBack)) {
        cards.showComponent(pnlNavigation);
        scroll.requestFocus();
      }
    for (Entry entry : lstEntry)
      if (source.equals(entry.btnNavigate)) {
        cards.showComponent(entry.pnlBody);
        entry.scroll.getViewport().setViewPosition(new Point(0,0));
        entry.scroll.requestFocus();
      }
  }
 
//-----------------------------------------------------------------------------------------------------------------------------------------------------------------------
 
  public void componentHidden(ComponentEvent event) { }
 
//-----------------------------------------------------------------------------------------------------------------------------------------------------------------------
 
  public void componentMoved(ComponentEvent event) { }
 
//-----------------------------------------------------------------------------------------------------------------------------------------------------------------------
 
  public void componentResized(ComponentEvent event)
  {
    if (event.getSource().equals(this)) setSize();
  }
 
//-----------------------------------------------------------------------------------------------------------------------------------------------------------------------
 
  public void componentShown(ComponentEvent event)
  {
    Object source = event.getSource();
    if (source.equals(this)) {
      scroll.getViewport().setViewPosition(new Point(0,0));
      cards.showComponent(pnlNavigation);
      setSize();
      scroll.requestFocus();
    }
  }
 
//----------Class Methods------------------------------------------------------------------------------------------------------------------------------------------------
 
  public void addEntry(String title, String body)
  {
    lstEntry.add(new Entry(this,Integer.toString(i++),title,body));
    rebuild();
  }
 
//-----------------------------------------------------------------------------------------------------------------------------------------------------------------------
 
  private void rebuild()
  {
    cards.removeAll();
    cards.add("nav", pnlNavigation);
    for (Entry e : lstEntry) cards.add(e.name,e.pnlBody);
    cards.showComponent(pnlNavigation);
 
    minHeightOfPnlSubNav = 0;
    for (Entry e : lstEntry) {
      Rectangle r = e.btnNavigate.getBounds();
      e.btnNavigate.setBounds(r.x,minHeightOfPnlSubNav += 20, r.width, r.height);
    }
    minHeightOfPnlSubNav += 40;
 
    setSize();
  }
 
//-----------------------------------------------------------------------------------------------------------------------------------------------------------------------
 
  private void setSize()
  {
    Rectangle rctNav = pnlNavigation.getVisibleRect();
    scroll.setBounds(10,10,(int)(rctNav.getWidth() - 20),(int)(rctNav.getHeight() - 50));
    for (Entry e : lstEntry) {
      Rectangle rctBtn = e.btnNavigate.getBounds();
      e.btnNavigate.setBounds((int)(rctBtn.getX()),(int)(rctBtn.getY()),(int)(rctNav.getWidth() - 60),(int)(rctBtn.getHeight()));
    }
    scroll.setChildSize((int)(rctNav.getWidth() - 20),Math.max(minHeightOfPnlSubNav,(int)(scroll.getBounds().getHeight() - 2)));
    btnClose.setBounds((int)((rctNav.getWidth() - 120) / 2),(int)(rctNav.getHeight() - 30),120,20);
  }
 
//-----------------------------------------------------------------------------------------------------------------------------------------------------------------------
 
  public void center(Rectangle rectangle)
  {
    this.setLocation((int)(rectangle.getX() + (rectangle.getWidth() - getSize().getWidth()) / 2),(int)(rectangle.getY() + (rectangle.getHeight() - getSize().getHeight()) / 2));
  }
 
//-----------------------------------------------------------------------------------------------------------------------------------------------------------------------
 
//=======================================================================================================================================================================
 
  private class Entry implements ComponentListener, SwingConstants
  {
    GuiDialogMessageMulti dialog = null;
 
    String name = null;
 
    GuiPanel pnlBody = new GuiPanel();
    JScrollPane scroll = null;
    JEditorPane editor = null;
 
    final GuiButton btnBack;
    final GuiButton btnClose;
    final GuiButton btnNavigate;
 
  //----------Constructors-----------------------------------------------------------------------------------------------------------------------------------------------
 
    private Entry(GuiDialogMessageMulti dialog, String name, String title, String body)
    {
      this.name   = name;
      this.dialog = dialog;
      // Create & Setup
      scroll      = new JScrollPane(editor = new JEditorPane());
      editor.setContentType("text/html");
      editor.setText(body);
      editor.setEditable(false);
      scroll.setBorder(BorderFactory.createLineBorder(Color.BLACK,1));
      pnlBody.add(scroll);
      btnBack     = pnlBody.addButton(10,10,120,20,"Back");
      btnClose    = pnlBody.addButton(140,10,120,20,"Close");
      btnNavigate = dialog.pnlSubNav.addButton(20,10,120,20,"<html><Font color=\"#0000FF\"><p><u>" + name + ".&nbsp;&nbsp;" + title + "</u></p></font></html>");
      btnNavigate.setCursor(new Cursor(Cursor.HAND_CURSOR));
      btnNavigate.setOpaque(false);
      btnNavigate.setBorder(null);
      btnNavigate.setHorizontalAlignment(LEFT);
      // Listeners
      dialog.addComponentListener(this);
      btnBack.addActionListener(dialog);
      btnClose.addActionListener(dialog);
      btnNavigate.addActionListener(dialog);
 
      btnBack.setFocusable(false);
      btnClose.setFocusable(false);
      btnNavigate.setFocusable(false);
      editor.setFocusable(false);
    }
 
  //----------Implemented Methods----------------------------------------------------------------------------------------------------------------------------------------
 
    public void componentHidden(ComponentEvent event) { }
 
  //---------------------------------------------------------------------------------------------------------------------------------------------------------------------
 
    public void componentMoved(ComponentEvent event) { }
 
  //---------------------------------------------------------------------------------------------------------------------------------------------------------------------
 
    public void componentResized(ComponentEvent event)
    {
      Object source = event.getSource();
      if (source.equals(dialog)) {
        scroll.setBounds(10,10,(int)(pnlBody.getVisibleRect().getWidth() - 20),(int)(pnlBody.getVisibleRect().getHeight() - 50));
        btnBack.setBounds((int)(pnlBody.getVisibleRect().getWidth() / 2 - 125),(int)(pnlBody.getVisibleRect().getHeight() - 30),120,20);
        btnClose.setBounds((int)(pnlBody.getVisibleRect().getWidth() / 2 + 5),(int)(pnlBody.getVisibleRect().getHeight() - 30),120,20);
        scroll.revalidate();
      }
    }
 
  //---------------------------------------------------------------------------------------------------------------------------------------------------------------------
 
    public void componentShown(ComponentEvent event)
    {
      Object source = event.getSource();
      if (source.equals(dialog)) {
        scroll  .setBounds(10,10,(int)(pnlBody.getVisibleRect().getWidth() - 20),(int)(pnlBody.getVisibleRect().getHeight() - 50));
        btnBack .setBounds((int)(pnlBody.getVisibleRect().getWidth() / 2 - 125) ,(int)(pnlBody.getVisibleRect().getHeight() - 30),120,20);
        btnClose.setBounds((int)(pnlBody.getVisibleRect().getWidth() / 2 + 5)   ,(int)(pnlBody.getVisibleRect().getHeight() - 30),120,20);
        scroll.revalidate();
      }
    }
 
  //---------------------------------------------------------------------------------------------------------------------------------------------------------------------
 
  }
  //=====================================================================================================================================================================
 
//-----------------------------------------------------------------------------------------------------------------------------------------------------------------------
 
}
//=======================================================================================================================================================================

Open in new window

0
Comment
Question by:modsiw
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
5 Comments
 
LVL 3

Author Comment

by:modsiw
ID: 21753716
GuiPanel, GuiButton, etc are classes extended off their matching swing component.

Also, this method for instance:
btnClose    = pnlBody.addButton(140,10,120,20,"Close");
Creates a button.
Sets it's bounds.
Sets it's texts.
Adds the button to pnlBody.
and if pnlBody is an appropriate listener, adds pnlBody to the button listener collection.
Returns that button.
0
 
LVL 3

Author Comment

by:modsiw
ID: 21753840
removing these lines:
    setFocusable(false);
    cards.setFocusable(false);

and removing all calls to requestFocus() worked

but, why?
0
 
LVL 13

Accepted Solution

by:
MicheleMarcon earned 250 total points
ID: 21757326
I suppose that is because you set these component as non-focusable, however remember that the focusability is implementation-dependent :(

The focusability of a peer is implementation-dependent. Sun recommends that all implementations for a particular native platform construct peers with the same focusability. The recommendations for Windows and Unix are that Canvases, Labels, Panels, Scrollbars, ScrollPanes, Windows, and lightweight Components have non-focusable peers, and all other Components have focusable peers. These recommendations are used in the Sun AWT implementations. Note that the focusability of a Component's peer is different from, and does not impact, the focusability of the Component itself.

http://java.sun.com/j2se/1.4.2/docs/api/java/awt/DefaultFocusTraversalPolicy.html
0
 
LVL 6

Assisted Solution

by:manuel_m
manuel_m earned 250 total points
ID: 21757562
You also should use requestFocusInWindow() instead of requestFocus().

This component must be displayable, focusable, visible and all of its ancestors (with the exception of the top-level Window) must be visible for the request to be granted. Otherwise the requestFocus method is guaranteed to be failed. (For further information read the API http://java.sun.com/javase/6/docs/api/java/awt/Component.html)

Also see the recommendation for requestFocus to use requestFocusInWindow instead because its platform dependent behavior. http://java.sun.com/javase/6/docs/api/java/awt/Component.html#requestFocus()

manuel
0

Featured Post

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
maven module vs maven project 3 74
difference between sorce folder and folder in eclipise 3 45
Java program running SQL query 5 50
IBM TS2900 (3572) Tape Autoloader Java? 12 61
INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
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 …

726 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