Results 1 to 3 of 3

Thread: swing doubt

  1. #1
    Join Date
    Jan 2012
    Posts
    1
    Rep Power
    0

    Default swing doubt

    I wrote the following code where i can add button's at run time(give button name and click on button).
    What i want is :-) How can i perform a specific action if a run-time added button is pressed.................. SUGGEST ME

    Java Code:
    import javax.swing.*;
    import java.awt.*;
    import java.util.*;
    import java.awt.event.*;
    public class ALD2 implements ActionListener
    {
    		JButton button;
    		JTextField jtf1;
    		JFrame frame;
    		
    		public static void main(String args[])
    		{
    		ALD2 ald=new ALD2();	
    		ald.go();
    		}
    
    		void go()
    		{
    		frame=new JFrame();
    		button=new JButton("click me");
    		jtf1=new JTextField(20);
    		frame.getContentPane().add(BorderLayout.EAST,button);
    		frame.getContentPane().add(BorderLayout.CENTER,jtf1);
    		button.addActionListener(this);
    		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		frame.setSize(300,300);
    		frame.setVisible(true);
    		}
    
    		public void actionPerformed(ActionEvent ae)
    		{
    		String str=jtf1.getText();
    
    
    		if(ae.getSource()==button)
    		{
    		frame.getContentPane().add(new JButton(str);
                                frame.validate();
                                frame.repaint();
    		}
    		
    		}
    }
    Last edited by JosAH; 01-30-2012 at 08:25 PM. Reason: added [code] ... [/code] tags

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    2,833
    Rep Power
    6

    Default Re: swing doubt

    You have to add an ActionListener to it, exactly like any other JButton.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    DarrylBurke's Avatar
    DarrylBurke is offline Moderator
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    9,917
    Rep Power
    16

    Default Re: swing doubt

    Moved from 'New to Java'

    db
    Why do they call it rush hour when nothing moves? - Robin Williams

Similar Threads

  1. Doubt about 09
    By maya700 in forum New To Java
    Replies: 2
    Last Post: 08-17-2010, 06:37 PM
  2. Doubt
    By karthikeyan_raju in forum Advanced Java
    Replies: 11
    Last Post: 11-05-2009, 04:48 PM
  3. Java Swing JTable Simple Doubt
    By hemanthjava in forum AWT / Swing
    Replies: 1
    Last Post: 11-26-2008, 01:46 PM
  4. doubt
    By shaju_inspira in forum Sun Java Wireless Toolkit
    Replies: 0
    Last Post: 01-18-2008, 08:38 AM
  5. doubt in jms
    By veena in forum Enterprise JavaBeans (EJB)
    Replies: 2
    Last Post: 01-05-2008, 01:42 PM

Tags for this Thread

Posting Permissions

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