/* hi guys.When i am willing to generate an event by clicking the button b1 to write text in textfield t1,there is an exception.t1 is not accessible in action listener class.I even declared t1 as public but in vain.Please help.*/
import java.io.*; import java.lang.*; import java.util.*; import java.awt.*; import javax.swing.*; import java.awt.event.*; class mynew extends JFrame { public mynew() { super("MyFrame"); setSize(200,200); JPanel row1=new JPanel(); JCheckBox c1=new JCheckBox("yeah",true); JCheckBox c2=new JCheckBox("nay",false); JPanel row2=new JPanel(); JLabel l1=new JLabel("Name:",JLabel.RIGHT); JTextField t1=new JTextField(); JTextField t11=new JTextField("Mohd.fazal"); JTextField t111=new JTextField("Mohd.fazal"); JLabel l2=new JLabel("Class:",JLabel.RIGHT); JTextField t12=new JTextField("Mohd.Shahid"); JTextField t2=new JTextField("B.E.3rd Year"); JTextField t112=new JTextField("Mohd.fazal"); JLabel l3=new JLabel("RollNO:",JLabel.RIGHT); JTextField t3=new JTextField("Number 52"); JTextField t13=new JTextField("Mohd.Shahid"); JTextField t113=new JTextField("Mohd.fazal"); JPanel row3=new JPanel(); JButton B1=new JButton("B1"); JButton B2=new JButton("B2"); JButton B3=new JButton("B3"); row3.setBackground(Color.yellow); JPanel row4=new JPanel(); JButton B4=new JButton("B4"); JButton B5=new JButton("B5"); JButton B6=new JButton("B6"); row4.setBackground(Color.yellow); JPanel row5=new JPanel(); JButton B7=new JButton("B7"); JButton B8=new JButton("B8"); JButton B9=new JButton("B9"); row5.setBackground(Color.yellow); Container pane=getContentPane(); pane.setBackground(Color.red); GridLayout lm=new GridLayout(5,1,10,10); pane.setLayout(lm); FlowLayout f1=new FlowLayout(FlowLayout.CENTER,10,10); row1.setLayout(f1); row1.add(c1); row1.add(c2); pane.add(row1); GridLayout g1=new GridLayout(3,3,10,10); row2.setLayout(g1); row2.add(l1); row2.add(t1); row2.add(t11); row2.add(t111); row2.add(l2); row2.add(t2); row2.add(t12); row2.add(t112); row2.add(l3); row2.add(t3); row2.add(t13); row2.add(t113); pane.add(row2); FlowLayout g2=new FlowLayout(FlowLayout.CENTER,10,10); row3.setLayout(g2); row3.add(B1); row3.add(B2); row3.add(B3); pane.add(row3); FlowLayout g3=new FlowLayout(FlowLayout.CENTER,10,10); row4.setLayout(g3); row4.add(B4); row4.add(B5); row4.add(B6); pane.add(row4); FlowLayout g4=new FlowLayout(FlowLayout.CENTER,10,10); row5.setLayout(g4); row5.add(B7); row5.add(B8); row5.add(B9); pane.add(row5); B1.addActionListener(new sd()); B2.addActionListener(new sd()); B3.addActionListener(new sd()); B4.addActionListener(new sd()); B5.addActionListener(new sd()); B6.addActionListener(new sd()); B7.addActionListener(new sd()); B8.addActionListener(new sd()); B9.addActionListener(new sd()); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setVisible(true); } } class sd implements ActionListener { public void actionPerformed(ActionEvent e) { String se=e.getActionCommand(); try { t1.setText("You pressed "+se); } catch(Exception ex) { System.out.println("Error"); } } } public class mynew1 { public static void main(String[] arguments)throws Exception { mynew obj=new mynew(); } }