Click to See Complete Forum and Search --> : Help with JApplet Focus


Adam Leo
September 21st, 2000, 06:41 PM
Hi, all.
I am trying to help some disabled people by building an HTML editor for them in JApplet.
Since they can't use mouse, everything has to be accessible with keyboard. Now I am
having a problem to make the JApplet get the focus automatically as the page is loaded.
The textarea DOES get the input focus, but if I switch to the menu using keyboard,
the editor will mess up: the arrow keys, return, Home, End ... won't work anymore.
But if I click outside of the applet and then click back (before the editor mess up),
everything works just fine. This is the most strange situation I have met.
I understand this is a specific question and a long FAQ, but I really appreciate if
any Jguru can help me as well as those disabled. The following is a sample of how
the request focus doesn't work properly.
<pre>

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.text.*;
import java.util.*;

public class HEditJApplet extends JApplet{

private JScrollPane editScrollPane;
private JTextPane editTextPane;
private JApplet me;
private JMenuBar jMenuBar;
private JMenu jMenuEdit;


public HEditJApplet()
{
getRootPane().putClientProperty("defeatSystemEventQueueCheck", Boolean.TRUE);
me = this;
editTextPane = new JTextPane();
editTextPane.setCaretPosition(0);

Hashtable actions = new Hashtable();
Action[] actionsArray = editTextPane.getActions();
for (int i = 0; i < actionsArray.length; i++) {
Action a = actionsArray[i];
actions.put(a.getValue(Action.NAME), a);
}

jMenuEdit = new JMenu("Edit");
jMenuEdit.setMnemonic('E');

JMenuItem jmiSelAll = new JMenuItem((Action)actions.get(DefaultEditorKit.selectAllAction));

jmiSelAll.setText("Select All");
jmiSelAll.setMnemonic('A');
jmiSelAll.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_A, Event.CTRL_MASK));
jMenuEdit.add(jmiSelAll);

jMenuBar = new JMenuBar();
jMenuBar.add(jMenuEdit);

editScrollPane = new JScrollPane(editTextPane);
editScrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
this.setJMenuBar(jMenuBar);
this.getContentPane().add(editScrollPane, BorderLayout.CENTER);
this.setVisible(true);
}

public void init()
{
me.requestFocus();
editTextPane.grabFocus();
}

public void start(){}
public void destroy(){}
public void stop(){}


};

</pre>
I used JDK1.3 and the HTMLConverter to convert the html file.

Thank you all for your attention, and if you think you can help, please email yaodongliu@yahoo.com.



God only helps those who help others.
Please rate my answers :-)