Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / GUI / March 2008

Tip: Looking for answers? Try searching our database.

Text component not focusable (tabbed pane switching involved)

Thread view: 
Karsten Wutzke - 21 Mar 2008 23:27 GMT
Hello,

I have a simply JFrame with a subclassed JTabbedPane as main
container. Each tab contains a non editable JTextPane which display
some text. Under each text pane there is a one lined JTextField. This
text field acts as input for the JTextPane. It is basically a simple
*chat* GUI...

On pressing a button or some other key combo the text in the text
field is copied to the text pane...

The problem now is:

When having 2 or more chats/tabs, the user can switch chats by
clicking on the tabs. This focuses the tabs however. What I want here
is to focus the *input text field* when the tab selection changes. As
Karsten Lentzsch pointed out, the ChangeListener was basically the way
to go:

public synchronized void stateChanged(ChangeEvent ce)
{
    System.out.println("stateChanged");

    final SingleSelectionModel ssm = getModel();

    int tc = getTabCount();

    //one tab is displayed when chat list empty, so there's always a
selected index
    if ( tc > 0 )
    {
        int index = ssm.getSelectedIndex();

        if ( index == -1 )
        {
            return;
        }

        if ( lsChatPanels.size() <= index )
        {
            return;
        }

        //valid index...

        JChatPanel cp = lsChatPanels.get(index);

        System.out.println("Requesting focus on " + cp.getName());

        JTextComponent tcUserMessage = cp.getUserMessageTextComponent();

        //request focus on user message text component
        tcUserMessage.requestFocus();
    }
}

Well the code basically works, but it simply DOESN'T receive keyboard
focus. It's a very annoying thing in a chat app when you switch a chat
panel and can't just start to type! (usability = zero)...

Whatever the code shows above, the point is, how do I achieve
focussing that text component? I suspect it's not easy in conjunction
with a JTabbedPane because of the explicit mouse click tab selection
which focusses the tabs...

How do I achieve automatic text field focussing when switching tabs? I
fiddled for some hours now, but the results are very depressing...

Karsten
RedGrittyBrick - 25 Mar 2008 10:12 GMT
> Hello,
>
[quoted text clipped - 63 lines]
> How do I achieve automatic text field focussing when switching tabs? I
> fiddled for some hours now, but the results are very depressing...

Even though your code is already on the EDT, maybe you have to wrap the
focus request in an invokeLater in order that things happen in the right
order?

Signature

RGB



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.