Link to home
Start Free TrialLog in
Avatar of NomanAhmed
NomanAhmed

asked on

Focus transfer of Component in Card Layout in Java

I am using Card layout as a layout manager for my applicatin , the problem which I am facing right now is that I created two panel, panel 1 has text field and a button and panel 2 has a text field and button panel 1 load when the application run and focus on text field of  panel 1 then I write a funtionality to call second panel on top of first panel using card layout next method , major problem is that when second panel displayed , the focus does not transfer to panel 2 text field , what i want that when second panel displayed focus should be transfer to text field of panel 2
here is the running code:

/////////////////           Main Calss      ///////////////////////

/*
 * CardLayoutDemo.java
 *
 * Created on February 23, 2007, 2:32 PM
 */

package com.bi.test;

import com.bi.test.FirstJPanel;
import java.awt.CardLayout;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;

/**
 *
 * @author  Administrator
 */
public class CardLayoutDemo extends javax.swing.JFrame {
   
    JPanel mainContainerPanel;
    final static String fs = "FS";
    final static String ss = "SS";
    FirstJPanel FirstPanel;
    SecondPanel SecondPanel ;
    private JButton FS,SS;
   
    /** Creates new form CardLayoutDemo */
    public CardLayoutDemo() {
        initComponents();
        mainContainerPanel = new JPanel();
        mainContainerPanel.setLayout(new CardLayout());
       
        FirstPanel =new FirstJPanel(mainContainerPanel);
        SecondPanel =new SecondPanel(mainContainerPanel);
   
        mainContainerPanel.add(FirstPanel, fs);
        mainContainerPanel.add(SecondPanel, ss);
        getContentPane().add(mainContainerPanel, java.awt.BorderLayout.CENTER);
           
    }
   
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc=" Generated Code ">                         
    private void initComponents() {

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
        setBounds((screenSize.width-600)/2, (screenSize.height-550)/2, 600, 550);
    }// </editor-fold>                        
   
    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new CardLayoutDemo().setVisible(true);
            }
        });
    }
   
    // Variables declaration - do not modify                    
    // End of variables declaration                  
   

   
}


/////////////////       First Panel Clsss     ///////////////////////

/*
 * FirstJPanel.java
 *
 * Created on February 23, 2007, 3:15 PM
 */

package com.bi.test;

import java.awt.CardLayout;
import javax.swing.JPanel;

/**
 *
 * @author  Administrator
 */
public class FirstJPanel extends javax.swing.JPanel {
   
    /** Creates new form FirstJPanel */
    JPanel mainContainerPanel=null;
    public FirstJPanel(JPanel mainContainerPanel) {
        this.mainContainerPanel=mainContainerPanel;
         initComponents();
       
    }
   
    public FirstJPanel() {
        initComponents();
    }
   
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc=" Generated Code ">
    private void initComponents() {
        FS = new javax.swing.JButton();
        jTextField1 = new javax.swing.JTextField();

        setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        setBackground(new java.awt.Color(204, 255, 255));
        FS.setText("First Screen");
        FS.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                FSActionPerformed(evt);
            }
        });

        add(FS, new org.netbeans.lib.awtextra.AbsoluteConstraints(130, 130, -1, -1));

        add(jTextField1, new org.netbeans.lib.awtextra.AbsoluteConstraints(120, 90, 110, -1));

    }// </editor-fold>

    private void FSActionPerformed(java.awt.event.ActionEvent evt) {
        CardLayout cl = (CardLayout)(mainContainerPanel.getLayout());
        cl.next(mainContainerPanel);
        cl.show(mainContainerPanel, evt.getActionCommand());
    }
   
   
    // Variables declaration - do not modify
    private javax.swing.JButton FS;
    private javax.swing.JTextField jTextField1;
    // End of variables declaration
   
}


////////////////////////   Second panel class ////////////////////////

/*
 * SecondPanel.java
 *
 * Created on February 23, 2007, 3:17 PM
 */

package com.bi.test;

import java.awt.CardLayout;
import javax.swing.JPanel;

/**
 *
 * @author  Administrator
 */
public class SecondPanel extends javax.swing.JPanel {
   
    /** Creates new form SecondPanel */
      JPanel mainContainerPanel=null;
     
        public SecondPanel(JPanel mainContainerPanel) {
        this.mainContainerPanel=mainContainerPanel;
         initComponents();
         
       
    }
     
    public SecondPanel() {
        initComponents();
    }
   
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc=" Generated Code ">
    private void initComponents() {
        SS = new javax.swing.JButton();
        jTextField1 = new javax.swing.JTextField();

        setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        setBackground(new java.awt.Color(255, 255, 153));
        SS.setText("Second Screen");
        SS.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                SSActionPerformed(evt);
            }
        });

        add(SS, new org.netbeans.lib.awtextra.AbsoluteConstraints(140, 140, -1, -1));

        add(jTextField1, new org.netbeans.lib.awtextra.AbsoluteConstraints(130, 100, 140, -1));

    }// </editor-fold>

    private void SSActionPerformed(java.awt.event.ActionEvent evt) {
        CardLayout cl = (CardLayout)(mainContainerPanel.getLayout());
        cl.previous(mainContainerPanel);
        cl.show(mainContainerPanel, evt.getActionCommand());
    }
   
   
    // Variables declaration - do not modify
    private javax.swing.JButton SS;
    private javax.swing.JTextField jTextField1;
    // End of variables declaration
   
}


Avatar of NomanAhmed
NomanAhmed

ASKER

this is very urgent please help me in this regard
ASKER CERTIFIED SOLUTION
Avatar of Mayank S
Mayank S
Flag of India 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
"

Add a FocusListener to your second JPanel - in the focusGained () method, call your textbox.requestFocus () ;

"

I did above code to my application but it is not working at all
Pls post the code you used.
I only made changes in SecondPanel class in above all classes  which is

///////////////////////   Second panel class ////////////////////////

/*
 * SecondPanel.java
 *
 * Created on February 23, 2007, 3:17 PM
 */

package com.bi.test;

import java.awt.CardLayout;
import javax.swing.JPanel;

/**
 *
 * @author  Administrator
 */
public class SecondPanel extends javax.swing.JPanel  implements FocusListener{  
    //new line added in prevevious code FocusListener
   
    /** Creates new form SecondPanel */
      JPanel mainContainerPanel=null;
     
        public SecondPanel(JPanel mainContainerPanel) {
        this.mainContainerPanel=mainContainerPanel;
         initComponents();
         jTextField1.addFocusListener(this);       //new line added in prevevious code
         
       
    }
     
    public SecondPanel() {
        initComponents();
    }
   
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc=" Generated Code ">
    private void initComponents() {
        SS = new javax.swing.JButton();
        jTextField1 = new javax.swing.JTextField();

        setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        setBackground(new java.awt.Color(255, 255, 153));
        SS.setText("Second Screen");
        SS.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                SSActionPerformed(evt);
            }
        });

        add(SS, new org.netbeans.lib.awtextra.AbsoluteConstraints(140, 140, -1, -1));

        add(jTextField1, new org.netbeans.lib.awtextra.AbsoluteConstraints(130, 100, 140, -1));

    }// </editor-fold>

    private void SSActionPerformed(java.awt.event.ActionEvent evt) {
        CardLayout cl = (CardLayout)(mainContainerPanel.getLayout());
        cl.previous(mainContainerPanel);
        cl.show(mainContainerPanel, evt.getActionCommand());
    }
        //new line added in prevevious code
public void focusGained(FocusEvent e) {
jTextField.requestFocus();
    }
     //new line added in prevevious code
    public void focusLost(FocusEvent e) {
    }
   
    // Variables declaration - do not modify
    private javax.swing.JButton SS;
    private javax.swing.JTextField jTextField1;
    // End of variables declaration
   
}


>> jTextField1.addFocusListener(this);  

The focus listener will be added on the JPanel and not the text-field.
you mean
  this.addFocusListener(this);

I also try this but it is not working
I solved it
the link above you give by that I solve my problem I did this

in panel 2 constructor I wrote this line
 KeyboardFocusManager.getCurrentKeyboardFocusManager().addPropertyChangeListener(new FocusChangeListener());

in panel 2 class I created this inner class

   class FocusChangeListener implements PropertyChangeListener {
        public void propertyChange(PropertyChangeEvent evt) {
            Component oldComp = (Component)evt.getOldValue();
            Component newComp = (Component)evt.getNewValue();
            if ("focusOwner".equals(evt.getPropertyName())) {
                if (oldComp != null)              
                   tf.requestFocus();
            }
        }
    }