Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: object not accessible

  1. #1
    Junior Member
    Join Date
    Feb 2010
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default object not accessible

    /* 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();
     }
    }
    Last edited by helloworld922; February 22nd, 2010 at 07:27 PM.


  2. #2
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: object not accessible

    There is no way for another class to reference an object it has no knowledge of (ie. in another class). You must somehow pass that object to your sd class. Also, as a side note in this case it isn't necessary to create a new action listener for each component. This makes your code somewhat sloppy and also wastes memory.

    public class sd
    {
         private JTextField field;
         public sd(JTextField textField)
         {
              this.field = textField;
         }
     
         public void actionPerformed(ActionEvent e)
         {
              String se = e.getActionCommand();
              try
              {
                   t1.setText("You pressed " + se);
              }
              catch (Exception ex)
              {
                   System.out.println("Error");
              }
         }
    }

    // where adding the action listener
    sd actionListener = new sd(t1);
    B1.addActionListener(actionListener);
    B2.addActionListener(actionListener);
    B3.addActionListener(actionListener);
    B4.addActionListener(actionListener);
    B5.addActionListener(actionListener);
    B6.addActionListener(actionListener);
    B7.addActionListener(actionListener);
    B8.addActionListener(actionListener);
    B9.addActionListener(actionListener);

    As a side note, I would strongly recommend choosing variable names that would be meaningful not only to you, but also to others who would read your code. Choose names that represent what that variable contains, being descriptive yet some-what brief. See the variable names I chose as an example.

Similar Threads

  1. write object to sql db
    By wolfgar in forum JDBC & Databases
    Replies: 3
    Last Post: May 18th, 2010, 10:03 AM
  2. Class and Object
    By jyothishey in forum Object Oriented Programming
    Replies: 2
    Last Post: January 25th, 2010, 08:48 AM
  3. Object o = new Object(); need help
    By zeeshanmirza in forum Object Oriented Programming
    Replies: 11
    Last Post: January 6th, 2010, 10:01 PM
  4. static final object
    By kalees in forum Object Oriented Programming
    Replies: 3
    Last Post: December 21st, 2009, 12:29 PM
  5. Object Injection
    By Json in forum Java Programming Tutorials
    Replies: 1
    Last Post: December 4th, 2009, 04:08 AM