Article View: borland.public.jbuilder.java.swing
Article #723Re: jTabbedPane focus problem
From: John T. Dow
Date: Wed, 05 Jan 2005 04:30
Date: Wed, 05 Jan 2005 04:30
172 lines
6660 bytes
6660 bytes
Solution found!
Arth, your solution worked for me as a demo, but not in my application.
I had googled for a solution but didn't look for a bug. Doing so was
helpful. This bug is relevant:
Bug ID: 5089436 REGRESSION:requestFocusInWindow() fails for comp. on
JTabbedPane aftertab switch
The description indicates that the bug developed in later versions of
java, not the one I have, but the workaround (to use invokeLater) solved
the problem for me.
Here's what I have.
To change the tab programmatically:
jTabbedPane1.setSelectedIndex(indexOfNewTab);
Put this code in the jTabbedPane1_stateChanged event.
int index = jTabbedPane1.getSelectedIndex();
...
if (index == indexOfNewTab) {
Runnable ComponentOnNewTab_RequestFocusLater = new Runnable() {
public void run() {
ComponentOnNewTab.requestFocus();
}
};
SwingUtilities.invokeLater(ComponentOnNewTab_RequestFocusLater);
}
Finally, I had some requestFocusInWindow() calls in the focusLost events
for several components (on the old tab). These were to override the
default focus traversal policy of top to bottom, left to right. Somehow
these requestFocusInWindow calls can be honored even though the tab has
changed. So I found it necessary to explicitly test for that.
void ComponentOnOldTab_focusLost(FocusEvent e) {
if (jTabbedPane1.getSelectedIndex() == indexOfOldTab) {
NextComponentOnOldTab.requestFocusInWindow();
}
}
With these changes, every time I change the tab with the mouse the focus
goes to the proper componet (before it did some of the time). And every
time I change the tab programmatically focus goes to the proper
component (before it never did).
It would be nice to not have to test for the current tab before
requesting focus as I show with "ComponentOnOldTab" above. It would be
nice if the stateChanged event could somehow declare that focus is
leaving the current tab to so no more focus lost events should be
honored on the old tab.
Anyway, solution found.
Arth, thanks for your help.
John
Arthur Ore wrote:
> I'm using JBuilderX Ent too, (JDK 1.4.2_06-b03). If I've understood what you
> are asking, the following works for me. Click any of the buttons or on the
> tabs and the field I specified always seems to get focus. Hope this helps. I
> assume you know you can search for bugs at www.javasoft.com. There do seem
> to be some issues with JTabbedPanes. However, if you can narrow your code
> down to always get a reproducible error and you can't find a bug then you
> could post it here and someone may be able to help.
>
> Arth
>
> import java.awt.*;
> import java.awt.event.*;
> import javax.swing.*;
> import javax.swing.event.*;
>
> public class Frame1
> extends JFrame
> {
> JTabbedPane jTabbedPane1 = new JTabbedPane();
> JPanel jPanel2 = new JPanel();
> JPanel jPanel3 = new JPanel();
> JButton jButton1 = new JButton("jButton1");
> JButton jButton2 = new JButton("jButton1");
> JPanel jPanel1 = new JPanel();
> JTextField jTextField2 = new JTextField("jTextField2");
> JTextField jTextField1 = new JTextField("jTextField1");
> JTextField jTextField3 = new JTextField("jTextField3");
> public Frame1()
> {
> try
> {
> jbInit();
> }
> catch (Exception e)
> {
> e.printStackTrace();
> }
> }
> private void jbInit() throws Exception
> {
> this.setSize(new Dimension(400, 300));
> jTabbedPane1.addChangeListener(new javax.swing.event.ChangeListener()
> {
> public void stateChanged(ChangeEvent e)
> {
> jTabbedPane1_stateChanged(e);
> }
> });
> jButton1.addActionListener(new java.awt.event.ActionListener()
> {
> public void actionPerformed(ActionEvent e)
> {
> jButton1_actionPerformed(e);
> }
> });
> jButton2.addActionListener(new java.awt.event.ActionListener()
> {
> public void actionPerformed(ActionEvent e)
> {
> jButton1_actionPerformed(e);
> }
> });
> jButton2.setText("jButton2");
> this.getContentPane().add(jTabbedPane1, BorderLayout.CENTER);
> jTabbedPane1.add(jPanel2, "jPanel2");
> jPanel2.add(jButton1, null);
> jTabbedPane1.add(jPanel3, "jPanel3");
> jPanel3.add(jButton2, null);
> jTabbedPane1.add(jPanel1, "jPanel1");
> jPanel1.add(jTextField1, null);
> jPanel1.add(jTextField2, null);
> jPanel1.add(jTextField3, null);
> }
>
> public static void main(String[] args)
> {
>
> Frame1 frame = new Frame1();
> Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
> Dimension frameSize = frame.getSize();
> frame.setLocation( (screenSize.width - frameSize.width) / 2,
> (screenSize.height - frameSize.height) / 2);
> frame.setVisible(true);
> }
>
> void jTabbedPane1_stateChanged(ChangeEvent e)
> {
> if (jTabbedPane1.getSelectedIndex() == 2)
> {
> //this.validate(); works without validate here
> jTextField2.requestFocus();
> }
> }
>
> void jButton1_actionPerformed(ActionEvent e)
> {
> jTabbedPane1.setSelectedIndex(2);
> this.validate(); // validate needed otherwise component doesn't retain
> focus
> jTextField2.requestFocus();
> }
>
> }
>
>
Technical Details
Message-ID:
<41DB6D5B.20202@johntdow.com>Path:
arc1-ca.pugleaf.net!g2news1.google.com!news1.google.com!proxad.net!fr.ip.ndsoftware.net!border2.nntp.ams.giganews.com!nntp.giganews.com!pe2.news.blueyonder.co.uk!blueyonder!news-peer-test!btnet!newsfeed.bt.es!caladan.landsraad.net!newsgroups.borland.com!not-for-mailReferences:
<41D99FA3.7060700@johntdow.com> <41d9ade3@newsgroups.borland.com> <41DA069C.3060608@johntdow.com> <41da51e6$1@newsgroups.borland.com> <41DAFE36.2000900@johntdow.com> <41db1740@newsgroups.borland.com>Show Full Headers
Complete Article Headers
X-Google-Thread: 111d45,a0ad10712e673590 X-Google-Attributes: gid111d45,public X-Google-Language: ENGLISH,ASCII-7-bit Message-ID: <41DB6D5B.20202@johntdow.com> Date: Tue, 04 Jan 2005 23:30:19 -0500 From: "John T. Dow" <john@johntdow.com> User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.0; en-US; rv:1.4) Gecko/20030624 Netscape/7.1 (ax) X-Accept-Language: en-us, en MIME-Version: 1.0 Newsgroups: borland.public.jbuilder.java.swing Subject: Re: jTabbedPane focus problem References: <41D99FA3.7060700@johntdow.com> <41d9ade3@newsgroups.borland.com> <41DA069C.3060608@johntdow.com> <41da51e6$1@newsgroups.borland.com> <41DAFE36.2000900@johntdow.com> <41db1740@newsgroups.borland.com> In-Reply-To: <41db1740@newsgroups.borland.com> Content-Type: text/plain; charset=us-ascii; format=flowed Content-Transfer-Encoding: 7bit NNTP-Posting-Host: 151.201.222.38 X-Trace: newsgroups.borland.com 1104899417 151.201.222.38 (4 Jan 2005 20:30:17 -0800) Lines: 173 Path: arc1-ca.pugleaf.net!g2news1.google.com!news1.google.com!proxad.net!fr.ip.ndsoftware.net!border2.nntp.ams.giganews.com!nntp.giganews.com!pe2.news.blueyonder.co.uk!blueyonder!news-peer-test!btnet!newsfeed.bt.es!caladan.landsraad.net!newsgroups.borland.com!not-for-mail