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
   
}


Editors IDEs

Avatar of undefined
Last Comment
NomanAhmed
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

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Avatar of NomanAhmed
NomanAhmed

ASKER

"

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
Avatar of Mayank S
Mayank S
Flag of India image

Pls post the code you used.
Avatar of NomanAhmed
NomanAhmed

ASKER

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
   
}


Avatar of Mayank S
Mayank S
Flag of India image

>> jTextField1.addFocusListener(this);  

The focus listener will be added on the JPanel and not the text-field.
Avatar of NomanAhmed
NomanAhmed

ASKER

you mean
  this.addFocusListener(this);

I also try this but it is not working
Avatar of NomanAhmed
NomanAhmed

ASKER

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();
            }
        }
    }
Editors IDEs
Editors IDEs

Development in most programming languages is frequently done with an editor or integrated development environment (IDE). An IDE is a software application that provide comprehensive facilities to computer programmers for software development. An IDE normally consists of a source code editor, build automation tools and a debugger. XCode, Visual Studio, Adobe Dreamweaver and Eclipse are some of the more popular development tools, but an editor or IDE can be anything from simple text editors to sophisticated programs. Related topics: All programming and development language, database and web based content management systems topics

25K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo