Link to home
Start Free TrialLog in
Avatar of modsiw
modsiw

asked on

.requestFocus() doesn't seem to be working

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

Avatar of modsiw
modsiw

ASKER

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.
Avatar of modsiw

ASKER

removing these lines:
    setFocusable(false);
    cards.setFocusable(false);

and removing all calls to requestFocus() worked

but, why?
ASKER CERTIFIED SOLUTION
Avatar of MicheleMarcon
MicheleMarcon
Flag of Italy image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial