troubleshooting Question

Focus transfer of Component in Card Layout in Java

Avatar of NomanAhmed
NomanAhmed asked on
Editors IDEs
8 Comments1 Solution1428 ViewsLast Modified:
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
   
}


ASKER CERTIFIED SOLUTION
Mayank S
Principal Technologist

Our community of experts have been thoroughly vetted for their expertise and industry experience.

Log in to continue reading
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform for $9.99/mo
View membership options
Unlock 1 Answer and 8 Comments.
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
See how we're fighting big data
The Value of Experts Exchange in My Daily IT Life

Experts Exchange (EE) has become my company's go-to resource to get answers. I've used EE to make decisions, solve problems and even save customers. OutagesIO has been a challenging project and... Keep reading >>

Mike

Owner of Outages.IO
Phoenix, Arizona, United States
Member Since 2016
Join a full scale community that combines the best parts of other tools into one platform.
Unlock 1 Answer and 8 Comments.
View membership options
“All of life is about relationships, and EE has made a virtual community a real community. It lifts everyone's boat.”
William Peck

Member since 2004