Solved

.requestFocus() doesn't seem to be working

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

919 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now