Results 1 to 3 of 3
Thread: swing doubt
- 01-30-2012, 06:57 PM #1
Member
- Join Date
- Jan 2012
- Posts
- 1
- Rep Power
- 0
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
- 01-30-2012, 07:04 PM #2
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!
- 01-31-2012, 08:59 AM #3
Similar Threads
-
Doubt about 09
By maya700 in forum New To JavaReplies: 2Last Post: 08-17-2010, 06:37 PM -
Doubt
By karthikeyan_raju in forum Advanced JavaReplies: 11Last Post: 11-05-2009, 04:48 PM -
Java Swing JTable Simple Doubt
By hemanthjava in forum AWT / SwingReplies: 1Last Post: 11-26-2008, 01:46 PM -
doubt
By shaju_inspira in forum Sun Java Wireless ToolkitReplies: 0Last Post: 01-18-2008, 08:38 AM -
doubt in jms
By veena in forum Enterprise JavaBeans (EJB)Replies: 2Last Post: 01-05-2008, 01:42 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks