i hava a code called home.java

import javax.swing.*;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.util.Random;
import java.util.*;
import java.text.*;

 
 class home extends JFrame implements ActionListener, Runnable
 {
	JButton btndetect,btntest,btnstart,btnexit,btntransfer;
	ImageIcon ic,ic1,bc;
        JLabel lbltitle;
        JTextArea textArea ;

        JScrollPane scrollPane;
        JPanel panel;
        Thread t;
	
 public home()
 {
 	setSize(800,570);
	setTitle(" CAMERA SETUP ");

	
	
	/*ic1=new ImageIcon("ready3.jpg");*/
	JTextArea textArea = new JTextArea( 5, 10);
        textArea.setPreferredSize(new Dimension(100, 100));
        JScrollPane scrollPane = new JScrollPane(textArea);
        textArea.setEditable(false);

        JPanel panel = new JPanel();
     
	lbltitle= new JLabel(ic1);	

	btndetect=new JButton(" Detect web cam ",ic);
	btntest=new JButton(" Test cam ");
	btnstart=new JButton("Start Survellance");
        btntransfer=new JButton("File Transfer");
        btnexit=new JButton("Exit");


	Container cp=getContentPane();
	cp.setLayout(null); 

	//cp.setBackground(Color.white);
	Insets ins=getInsets();

	textArea.setBounds(400,100,200,400);
	btntest.setBounds(40,200,150,50);
        btntransfer.setBounds(230,200,150,50);
	btnstart.setBounds(40,300,150,50);
        btndetect.setBounds(230,300,150,50);
        btnexit.setBounds(120,400,170,50);

	lbltitle.setBounds(0,0,850,570);
	//lbltitle.setBounds(220,20,130,104);
	
	btndetect.addActionListener(this);
	btntest.addActionListener(this);
        btntransfer.addActionListener(this);
	btnstart.addActionListener(this);
	btnexit.addActionListener(this);
        
        cp.add(panel, "north");
        cp.add(scrollPane);
        cp.add(textArea);
	cp.add(btndetect);
	cp.add(btntest);
        cp.add(btntransfer);
	cp.add(btnstart);
	cp.add(btnexit);
        cp.add(lbltitle);		

	cp.setBackground(Color.black);
     
	
  }



public void run()
{

  try
      {
               		 
          while(t.isAlive())
           {		
	Thread.sleep(1000);
	
	File f = new File("check/test.jpg");
	 if(f.exists())                  
	{
                    

	   String fname = f.getAbsolutePath();

                   FileInputStream instream = new FileInputStream(fname);

	int insize = instream.available();

	byte inBuf[] = new byte[insize];

                 int bytesread = instream.read(inBuf,0,insize);
	instream.close();
		                     	
		
 
                  }                    

            }
       }
         catch(Exception e)
         {
             System.out.println(e);
        }


}

public void actionPerformed(ActionEvent ae)
  {
				
	if(ae.getSource() == btndetect)
	{

		textArea.setText("initialized");
       detect sc=new detect();
		sc.show();
		setVisible(false);
	}
 	
	else 
	if(ae.getSource() == btntest)
	{

		

	 try
        {
            JWebCam myWebCam = new JWebCam ( "Web Cam Capture" );
            myWebCam.setVisible ( true );
 
            if ( !myWebCam.initialise() )
            {
                System.out.println ("Web Cam not detected / initialised");
            }
        }
        catch ( Exception ex )
        {
            ex.printStackTrace();
        }

	}
                    else if(ae.getSource() == btnstart)
                   {

	
		WebCam sc=new WebCam("vfw:Microsoft WDM Image Capture (Win32)");
		sc.show();

		setVisible(false);
                   }
             else 
                 if (ae.getSource() == btntransfer)
                  {
                   
                    transfer t=new transfer();
	            t.show();

                                       }


	else
	{
		System.exit(0);
	}
	
  }


  public static void main(String args[])
  {
 	home h=new home();
	h.show();
  }
}

now i need to add text in textArea whenver a button is clicked..
i.e sppose i click button "start surveillance" ..i want "surveillance started" to appear in JtextArea of the same Jframe...
czm sm1 peez assist me..
any kind of help wil b appreciated..thnx

Recommended Answers

All 12 Replies

lines 124 to 184 are action performed on button click.

It isn't working because on line 33 you have

JTextArea textArea = new JTextArea( 5, 10);

but you should have

textArea = new JTextArea(5,10);

. If you leave it as the way you have it, then you are creating a completely different JTextArea than the one you are referring to in your actionPerformed method.

hey thnx...bt i stil am nt getting any kinda text(i.e "initialized" or "surveillance started") in the text Area when i click d button...!!!
can u tell me hw can i display the text on button click..n i want it(text) to remain ther til i close the window..!!!
n ny other suggestions to improve the Gui wil also b much appreciated...!!
This is basically ma final year projct n am new to java..so am really lookin fwd to u guys..!!

You made the same mistake with almost all of your other Objects as well (such as JPanel, JScrollPane, etc). Why don't you fix those mistakes, then repost your code and I will take a look at it. And don't use chat speak or whatever you are using, it is almost impossible to figure out what you're saying. Use English.

import javax.swing.*;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.util.Random;
import java.util.*;
import java.text.*;

 
 class home extends JFrame implements ActionListener, Runnable
 {
	JButton btndetect,btntest,btnstart,btnexit,btntransfer;
	ImageIcon ic,ic1,bc;
        JLabel lbltitle;
        JTextArea textArea ;

        JScrollPane scrollPane;
        JPanel panel;
        Thread t;
	
 public home()
 {
 	setSize(800,570);
	setTitle(" CAMERA SETUP ");

/*ic1=new ImageIcon("ready3.jpg");*/
	
        textArea = new JTextArea(5,10);
        textArea.setPreferredSize(new Dimension(100, 100));
        scrollPane = new JScrollPane(textArea);
        textArea.setEditable(false);

        panel = new JPanel();
     
	lbltitle= new JLabel(ic1);	

	btndetect=new JButton(" Detect web cam ",ic);
	btntest=new JButton(" Test cam ");
	btnstart=new JButton("Start Survellance");
        btntransfer=new JButton("File Transfer");
        btnexit=new JButton("Exit");


	Container cp=getContentPane();
	cp.setLayout(null); 
        Insets ins=getInsets();

	textArea.setBounds(400,100,200,400);
	btntest.setBounds(40,200,150,50);
        btntransfer.setBounds(230,200,150,50);
	btnstart.setBounds(40,300,150,50);
        btndetect.setBounds(230,300,150,50);
        btnexit.setBounds(120,400,170,50);

	lbltitle.setBounds(0,0,850,570);
	
	
	btndetect.addActionListener(this);
	btntest.addActionListener(this);
        btntransfer.addActionListener(this);
	btnstart.addActionListener(this);
	btnexit.addActionListener(this);
        
        cp.add(panel, "north");
        cp.add(scrollPane);
        cp.add(textArea);
	cp.add(btndetect);
	cp.add(btntest);
        cp.add(btntransfer);
	cp.add(btnstart);
	cp.add(btnexit);
        cp.add(lbltitle);		

	cp.setBackground(Color.black);
     
	
  }



public void run()
{

  try
      {
               		 
          while(t.isAlive())
           {		
	Thread.sleep(1000);
	
	File f = new File("check/test.jpg");
	 if(f.exists())                  
	{
                    

	   String fname = f.getAbsolutePath();

                   FileInputStream instream = new FileInputStream(fname);

	int insize = instream.available();

	byte inBuf[] = new byte[insize];

                 int bytesread = instream.read(inBuf,0,insize);
	instream.close();
		                     	
		
 
                  }                    

            }
       }
         catch(Exception e)
         {
             System.out.println(e);
        }


}

public void actionPerformed(ActionEvent ae)
  {
				
	if(ae.getSource() == btndetect)  //action for button detect webcam
	{

	textArea.setText("initialized");
        detect sc=new detect();
		sc.show();
		setVisible(false);
	}
 	
	
else 
         if(ae.getSource() == btntest)    //action for button "test webcam"
	{

	    try
                 {
            JWebCam myWebCam = new JWebCam ( "Web Cam Capture" );
            myWebCam.setVisible ( true );
 
            if ( !myWebCam.initialise() )
            {
                System.out.println ("Web Cam not detected / initialised");
            }
        }
        catch ( Exception ex )
        {
            ex.printStackTrace();
        }

	}
            
       
 
        else if(ae.getSource() == btnstart) //action to "start surveillance"
                {

		WebCam sc=new WebCam("vfw:Microsoft WDM Image Capture (Win32)");
		sc.show();
                 setVisible(false);
                   }

         else if (ae.getSource() == btntransfer)
                  {
                   
                    transfer t=new transfer();
	            t.show();

                                       }


	else
	{
		System.exit(0);
	}
	
  }


  public static void main(String args[])
  {
 	home h=new home();
	h.show();
  }
}

Ok i have posted the code as per the changes you suggested:
but i still am not getting the following:
1.Scroll pane
2.& i would like to know if

textArea.setText ("initialized");

(i.e line 130 )is the correct syntax to display message in the textArea because i'am still getting an empty textArea.
3. i would also like to display the system date n time in the textArea,showing what date & time was the button pressed.

Another question: what is the reason that you are using Runnable and implementing Runnable?

And instead of calling show(), you should be calling setVisible(true) on the JFrame. The show() method is deprecated, i.e. not used anymore.

The reason your code didn't work is because in your section of code where you were setting the JTextArea to say "initialized", you were also calling setVisible(false) which made the entire JFrame disappear. Hence you could not see the JTextArea anymore.

import javax.swing.*;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.util.Random;
import java.util.*;
import java.text.*;

 
 class Home extends JFrame implements ActionListener, Runnable
 {
	JButton btndetect,btntest,btnstart,btnexit,btntransfer;
	ImageIcon ic,ic1,bc;
        JLabel lbltitle;
        JTextArea textArea ;

        JScrollPane scrollPane;
        JPanel panel;
        Thread t;
	
 public Home()
 {
 	setSize(800,570);
	setTitle(" CAMERA SETUP ");

/*ic1=new ImageIcon("ready3.jpg");*/
	
        textArea = new JTextArea(5,10);
        textArea.setPreferredSize(new Dimension(100, 100));
        scrollPane = new JScrollPane(textArea);
        textArea.setEditable(false);

        panel = new JPanel();
     
	lbltitle= new JLabel(ic1);	

	btndetect=new JButton(" Detect web cam ",ic);
	btntest=new JButton(" Test cam ");
	btnstart=new JButton("Start Survellance");
        btntransfer=new JButton("File Transfer");
        btnexit=new JButton("Exit");


	Container cp=getContentPane();
	cp.setLayout(null); 
        Insets ins=getInsets();

	textArea.setBounds(400,100,200,400);
	btntest.setBounds(40,200,150,50);
        btntransfer.setBounds(230,200,150,50);
	btnstart.setBounds(40,300,150,50);
        btndetect.setBounds(230,300,150,50);
        btnexit.setBounds(120,400,170,50);

	lbltitle.setBounds(0,0,850,570);
	
	
	btndetect.addActionListener(this);
	btntest.addActionListener(this);
        btntransfer.addActionListener(this);
	btnstart.addActionListener(this);
	btnexit.addActionListener(this);
        
        cp.add(panel, "north");
        cp.add(scrollPane);
        cp.add(textArea);
	cp.add(btndetect);
	cp.add(btntest);
        cp.add(btntransfer);
	cp.add(btnstart);
	cp.add(btnexit);
        cp.add(lbltitle);		

	cp.setBackground(Color.black);
     
	
  }



public void run()
{

  try
      {
               		 
          while(t.isAlive())
           {		
	Thread.sleep(1000);
	
	File f = new File("check/test.jpg");
	 if(f.exists())                  
	{
                    

	   String fname = f.getAbsolutePath();

                   FileInputStream instream = new FileInputStream(fname);

	int insize = instream.available();

	byte inBuf[] = new byte[insize];

                 int bytesread = instream.read(inBuf,0,insize);
	instream.close();
		                     	
		
 
                  }                    

            }
       }
         catch(Exception e)
         {
             System.out.println(e);
        }


}

public void actionPerformed(ActionEvent ae)
  {
				
	if(ae.getSource() == btndetect)  //action for button detect webcam
	{
		System.out.println("set to initialized");
		textArea.setText("initialized");
        //detect sc=new detect();
		//sc.show();
		//setVisible(false);
	}
 	
	
else 
         if(ae.getSource() == btntest)    //action for button "test webcam"
	{

	    try
                 {
           // JWebCam myWebCam = new JWebCam ( "Web Cam Capture" );
           // myWebCam.setVisible ( true );
 
           
        }
        catch ( Exception ex )
        {
            ex.printStackTrace();
        }

	}
            
       
 
        else if(ae.getSource() == btnstart) //action to "start surveillance"
                {

		//WebCam sc=new WebCam("vfw:Microsoft WDM Image Capture (Win32)");
		//sc.show();
                 setVisible(false);
                   }

         else if (ae.getSource() == btntransfer)
                  {
                   
                    //transfer t=new transfer();
	            //t.show();

                                       }


	else
	{
		System.exit(0);
	}
	
  }


  public static void main(String args[])
  {
 	Home h=new Home();
	h.setVisible(true);
  }
}

ya..it's working without it.
but Sir,i need it to initialise the webcam when i click "BTNDETECT" as well as show it in the textArea once i come back to home.java after performing the initalization of webcam,but thats not happening.
It's either showing me the message in textarea but not performing the actual function of initialising the web cam & vice-versa.

& Sir neither is the scroll pane working...why is it so..!!!

The scroll pane should only work if there is enough text in the JTextArea to make scrolling necessary. And I don't know about the Webcam, I've never worked with those in Java before. Read the API.

ya Runnable is not really required.It works even without it.
But if i make setvisible (true) then it creates multiple instances of Home.java.It's showing "initialized" in text area only fr 1st instance of home.java
this is code for detect.java..this would give u a better idea:

import javax.swing.*;
 import java.awt.*;
 import java.awt.event.*;
 
 class detect extends JFrame implements ActionListener
 {
	JButton btndetect,btnback;
	JLabel lbltitle,back;
	ImageIcon ic1;
 
 public detect()
 {
	
 	setSize(850,580);
	setTitle(" Detect ");

	ic1=new ImageIcon("READY 4.jpg");
		
	back= new JLabel(ic1);	


	lbltitle= new JLabel(" Please attach webcam to the PC and click the detect button ");
	
	
	btndetect=new JButton(" Detect ");
	btnback=new JButton(" Back");
	
	
	Container cp=getContentPane();
	cp.setLayout(null); 

	cp.setBackground(Color.white);
	Insets ins=getInsets();
	
	lbltitle.setBounds(250,500,400,50);
	back.setBounds(0,0,850,512);
	btndetect.setBounds(250,230,100,50);
	btnback.setBounds(530,230,100,50);

	
	btndetect.addActionListener(this);
	btnback.addActionListener(this);
	

	cp.add(lbltitle);
	cp.add(btndetect);
	cp.add(btnback);
	cp.add(back);	
        
 	}


public void actionPerformed(ActionEvent ae)
  {
	
	if(ae.getSource() == btndetect)
	{
	
	try {

	 String detectpath = "C:/Program Files/JMF2.1.1e/bin/jmfinit.exe";

	Runtime r = Runtime.getRuntime();
	Process p;
	
	p = r.exec(detectpath);
	
	    } 
	      catch(Exception e)
		{
		  System.out.println(e);
		}
	
	}
			
	else if(ae.getSource() == btnback)
	{

		home sc=new home();
		sc.show();
		setVisible(false);
	} 	
	
  }
	
  public static void main(String args[])
  {
 	detect h=new detect();
	h.show();
  }
}

this code basically initializes Webcam by running "jmfinit.exe".

sir i just want u to see what happens when iclick button to go back to home.java(btnback i.e line 77 to 83)

this Gui basically has only 2 buttons one to detect & other to go back to main menu i.e home.java

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.