Solved

.requestFocus() doesn't seem to be working

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

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
hash value 2 38
countHi challenge 25 84
topping2 challenge 13 55
starter POM and spring-boot-starter,  spring-boot-web 2 10
For customizing the look of your lightweight component and making it look opaque like it was made of plastic.  This tip assumes your component to be of rectangular shape and completely opaque.   (CODE)
For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
This video teaches viewers about errors in exception handling.

763 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

13 Experts available now in Live!

Get 1:1 Help Now