Results 1 to 6 of 6
Like Tree3Likes
  • 1 Post By ShadowWalker
  • 2 Post By Tolls

Thread: action listener for more than one object in an applet

  1. #1
    noobplus's Avatar
    noobplus is offline Senior Member
    Join Date
    Feb 2012
    Location
    chennai,south of India(south east of asia)
    Posts
    232
    Rep Power
    3

    Default action listener for more than one object in an applet

    Java Code:
    import java.applet.*;
    import java.awt.*;
    import java.awt.event.*;
    
    import javax.swing.JTextField;
    
    public class expbutton extends Applet implements ActionListener{
    	/**
    	 * 
    	 */
    	private static final long serialVersionUID = 1L;
    	int count=1;
    	public void start() {
    		setLayout(new BorderLayout());	
    		
    	Panel pa=new Panel();
    	JTextField jtext=new JTextField("1text");
    	JTextField jtext2=new JTextField("2text");
    	Button bu1=new Button("clickme");
    	bu1.addActionListener(this);
    	pa.add(jtext);
            pa.add(jtext2);
            jtext2.addActionListener(this);
    	pa.add(bu1);
    	
    	add(pa,"South");
    	
    	}
    
    	@Override
    	public void actionPerformed(ActionEvent arg0) {
    		
    		String arg= arg0.getActionCommand();
    		
    		
    		
    		if(arg.equals("clickme")&&count%3==0) {setBackground(Color.BLACK); count++;System.out.println(count);}
    		else if(arg.equals("clickme")&&count%3==1) {setBackground(Color.BLUE); count++;System.out.println(count);}
    		else if(arg.equals("clickme") &&count%3==2) {setBackground(Color.GREEN.darker());count-=2;}
    		repaint();	
    	}
    }
    I've added action command for button "click me", I want object "jtext2" to show the "count"
    can I override actionPerformed() on two objects in applet?

    thanks

  2. #2
    ShadowWalker is offline Senior Member
    Join Date
    Mar 2013
    Location
    Greece
    Posts
    113
    Rep Power
    0

    Default Re: action listener for more than one object in an applet

    i am not an very good with applets but why don't you just but the count inside the jtext2? for example
    change the line 18 with : JTextField jtext2 = new JtextField(count+""); // count+ "" make the int number count a string..
    so each time that user will click the button the count will change and the label of the textField will change too.. there is a way to do that ! make a try !

  3. #3
    noobplus's Avatar
    noobplus is offline Senior Member
    Join Date
    Feb 2012
    Location
    chennai,south of India(south east of asia)
    Posts
    232
    Rep Power
    3

    Default Re: action listener for more than one object in an applet

    thx Do you know swing? I need to do accessing data from notepad through either applet or swing.. that's why...

  4. #4
    ShadowWalker is offline Senior Member
    Join Date
    Mar 2013
    Location
    Greece
    Posts
    113
    Rep Power
    0

    Default Re: action listener for more than one object in an applet

    if you want to have access to other text files you can use the File_handing system..
    more info here : Java 103 : File-handling under Java
    see the File input example!
    but why you need access to other files anyway?
    noobplus likes this.

  5. #5
    noobplus's Avatar
    noobplus is offline Senior Member
    Join Date
    Feb 2012
    Location
    chennai,south of India(south east of asia)
    Posts
    232
    Rep Power
    3

    Default Re: action listener for more than one object in an applet

    Actually this is my task:
    "write a program to store information abut staff and student, the staff will have a unique staff ID, name, data of joining,qualification position and salary. A student will have an unique register number, name, a list of subjects and respective marks. A method must be provided to display the person details(be it a student or staff). Use inheritence, Interfaces, Templates(Generics) wherever necessary"
    *if you read that huge paragraph..
    I want user to input names in JTextField () in applet or swing, and store it in a notepad, all the user's details

    ETA: thanks for the link, I studied how to use
    Java Code:
    Buffered reader(new FileReader("input.txt"));
    there..
    same with BufferedWriter.. Now I want to store "JTextField" into the BufferedWriter..
    Last edited by noobplus; 07-18-2013 at 08:26 PM.

  6. #6
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,006
    Rep Power
    20

    Default Re: action listener for more than one object in an applet

    Why are you using Applet rather than JApplet?
    Applet was superceded by JApplet some 15 years ago.
    DarrylBurke and noobplus like this.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

Similar Threads

  1. Need more action listener help
    By ndsmith20 in forum New To Java
    Replies: 9
    Last Post: 01-19-2013, 01:12 AM
  2. action listener
    By skuskusas in forum New To Java
    Replies: 4
    Last Post: 09-04-2012, 07:13 PM
  3. Can anyone help me with an action listener?
    By mdCollins1 in forum New To Java
    Replies: 5
    Last Post: 03-21-2012, 04:07 AM
  4. Replies: 2
    Last Post: 12-11-2011, 10:44 PM
  5. Action Listener? how to use this?
    By jeffrey in forum New To Java
    Replies: 2
    Last Post: 10-12-2009, 08:51 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •