Solved

.requestFocus() doesn't seem to be working

Posted on 2008-06-10
5
1,196 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
  • 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

ScreenConnect 6.0 Free Trial

At ScreenConnect, partner feedback doesn't fall on deaf ears. We collected partner suggestions off of their virtual wish list and transformed them into one game-changing release: ScreenConnect 6.0. Explore all of the extras and enhancements for yourself!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
JavaScript/Java - Changing an image background color 4 66
map related example 6 48
difference of if loops 23 49
java stored proc example 9 30
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…

772 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