[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 753
  • Last Modified:

Problem with input focus and JTabbedPane

Hi,
I have a JFrame to which I add a JTabbedPane. I supervise the stateChanged event posted be the JTabbedPane. When a specifc tab becomes active - one that holds a collection of hierarchical arranged JPanels - I would like a specific JPanel, which is a descendant of this tab, to get the input focus. Unfortunately nothing I've tried so far will make that happen. Only after I click with the mouse into the panel it'll receive the input focus. Even calling the requestFocus() for the JPanel won't do any good! Any idea how to achieve my desired behaviour? Any comments welcome.
Thanks in advance
Miknosoft
0
Miknosoft
Asked:
Miknosoft
  • 6
  • 6
  • 3
1 Solution
 
hoomanvCommented:
jTabbedPane.setSelectedComponent(jPanel); // would select the panel
0
 
MiknosoftAuthor Commented:
hoomanv,
thank you for your suggestion. As a mater of fact, the component homing the panel I woud like to receive the input focus is already the selected component when the stateChanged event is posted.
Regards
Miknosoft
0
 
hoomanvCommented:
Is your problem solved now ?
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
MiknosoftAuthor Commented:
No.
The problem is that I cannot give the input focus to a specific panel which is part of the selected component.
Regard
Miknosoft
0
 
hoomanvCommented:
It could be easier to help, if you post the code
0
 
MiknosoftAuthor Commented:
Hi,
this won't do you any good. It's a quite big program and the described problem is only part of it. Would be difficult to extract the portion of code that's affected.
Thanks anyway and regards
Miknosoft
0
 
MiknosoftAuthor Commented:
Hi,
I've put together a little test program which basically shows what I want to do. I simply add a JTabbedPane to my frame, create 2 JPanels each homing one JTextField and try to give the input focus to the JTextField in case the homing JPanel is selected:

////////////////////////////////////////////////////////////
import javax.swing.*;
import javax.swing.event.*;
import java.awt.*;

public class
TabbedPaneTest
extends JFrame
implements ChangeListener
{
  private JTabbedPane m_tabbedPane;
  private JPanel      m_panel1;
  private JTextField  m_inputField1;
  private JPanel      m_panel2;
  private JTextField  m_inputField2;

  public static void main(String[] args)
  {
    new TabbedPaneTest();
  } // main

  public TabbedPaneTest()
  {
    super("TabbedPaneTest");
    setPreferredSize(new Dimension(320, 180));
    setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    // create and add tabbed pane
    m_tabbedPane = new JTabbedPane();
    getContentPane().add(m_tabbedPane);
    m_tabbedPane.addChangeListener(this);
    // create and add panel no 1
    m_panel1 = new JPanel();
    m_inputField1 = new JTextField("JTextField1");
    m_panel1.add(m_inputField1);
    m_tabbedPane.add(m_panel1, "Panel 1");
    m_inputField1.requestFocus();
    // create and add panel no 2
    m_panel2 = new JPanel();
    m_inputField2 = new JTextField("JTextField2");
    m_panel2.add(m_inputField2);
    m_tabbedPane.add(m_panel2, "Panel 2");
    // finally show up
    pack();
    show();
  }

  public final void stateChanged(ChangeEvent e)
  {
    if (m_panel1.equals(m_tabbedPane.getSelectedComponent()))
    {
      System.out.println("Giving focus to JTextField no 1");
      m_inputField1.requestFocus();
    }
    else
    {
      System.out.println("Giving focus to JTextField no 2");
      m_inputField2.requestFocus();
    }
  }
}
////////////////////////////////////////////////////////////

I'm using JRE 1.5.0.06

Regards
Miknosoft
0
 
girionisCommented:
Try to replace this line:

> m_inputField1.requestFocus();

with this:

SwingUtilities.invokeLater(new Runnable()
{
      public void run()
      {
          m_inputField1.requestFocus();
      }
});
0
 
hoomanvCommented:
but I think he'll be forced to declare m_inputField1 as final
0
 
MiknosoftAuthor Commented:
Hi,
I fixed to manage this. Anyway there's no difference in the behaviour.
Regards
Miknosoft
0
 
girionisCommented:
Maybe not... the new Runnable is not actually an inner class.
0
 
hoomanvCommented:
I succeed ;-)

package untitled1;

import java.awt.*;
import java.awt.event.*;

import javax.swing.*;
import javax.swing.event.*;

public class Frame1 extends JFrame {
    BorderLayout borderLayout1 = new BorderLayout();
    JTabbedPane jTabbedPane1 = new JTabbedPane();
    JPanel jPanel1 = new JPanel();
    JPanel jPanel2 = new JPanel();
    JTextField jTextField1 = new JTextField();
    JTextField jTextField2 = new JTextField();
    public Frame1() {
        try {
            jbInit();
        } catch (Exception exception) {
            exception.printStackTrace();
        }
    }

    private void jbInit() throws Exception {
        getContentPane().setLayout(borderLayout1);
        jPanel2.addFocusListener(new FocusAdapter() {
            public void focusGained(FocusEvent e) {
                jPanel2_focusGained(e);
            }
        });
        jTabbedPane1.addChangeListener(new ChangeListener() {
            public void stateChanged(ChangeEvent e) {
                jTabbedPane1_stateChanged(e);
            }
        });
        this.getContentPane().add(jTabbedPane1, java.awt.BorderLayout.CENTER);
        jTextField1.setText("jTextField1");
        jTextField2.setText("jTextField2");
        jPanel1.addFocusListener(new FocusAdapter() {
            public void focusGained(FocusEvent e) {
                jPanel1_focusGained(e);
            }
        });
        jTabbedPane1.add(jPanel1, "jPanel1");
        jPanel1.add(jTextField1);
        jTabbedPane1.add(jPanel2, "jPanel2");
        jPanel2.add(jTextField2);
    }

    public static void main(String[] args) {
        Frame1 frame1 = new Frame1();
        frame1.setSize(new Dimension(300,200));
        frame1.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        frame1.setVisible(true);
    }

    public void jPanel1_focusGained(FocusEvent e) {
        System.out.println("panel1 focused");
        jTextField1.dispatchEvent(new FocusEvent(jTextField1, FocusEvent.FOCUS_GAINED));
    }

    public void jPanel2_focusGained(FocusEvent e) {
        System.out.println("panel2 focused");
        jTextField2.dispatchEvent(new FocusEvent(jTextField2, FocusEvent.FOCUS_GAINED));
    }

    public void jTabbedPane1_stateChanged(ChangeEvent e) {
        System.out.println("state changed");
        Component com = jTabbedPane1.getSelectedComponent();
        com.dispatchEvent(new FocusEvent(com, FocusEvent.FOCUS_GAINED));
    }
}
0
 
girionisCommented:
> I fixed to manage this. Anyway there's no difference in the behaviour.

So it didn't work? Try to use component.requestFocusInWindows and see fi that works.
0
 
MiknosoftAuthor Commented:
Hi hoomanv,
thank you very much for your interest in my problem. After adapting the technique to dispatch the focus event for the desired focus component, the behaviour is exactly as desired!
Have a nice time and kind regards,
Miknosoft
0
 
hoomanvCommented:
Glad I Could Help :)
0

Featured Post

Prep for the ITIL® Foundation Certification Exam

December’s Course of the Month is now available! Enroll to learn ITIL® Foundation best practices for delivering IT services effectively and efficiently.

  • 6
  • 6
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now