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