Data from Textfield to Label

SWING Part 1, adding Button

Adding one JTextField

Add one JTextField to our window.
Swing JTextField
Go to its properties and change its name to t1
Swing JTextField Properties

Adding one button

Create one Button on our window.
Go to its properties and change the text on it to Copy
Change its name to b1 ( read the part 1 on how to add button)

Add one Label

Create one Lable by selecting one JLable from the components
Swing JLable adding
Go to its properties and change the text on its to Output
Swing JLabel Properties
Change its name to l1

Now all our components are ready and we will add event to our button( b1 ) to copy the text ( from t1 ) and place the same in our Lable ( l1 )

Doble click the button on our design Panel.

On the source panel check that this code is written for the event or ActionListener()
We used getText() to read the text written at textfield t1.
b1.addActionListener(new ActionListener() {
	public void actionPerformed(ActionEvent e) {
	String str=t1.getText(); // read the value of text field. 
	l1.setText(str);  // add the text to Label
	}
});
If you are getting any error saying l1 cannot be resolved. Then go to Class declaration and ensure that our three components are declared.
public class Basic {

	private JFrame frame;
	private JTextField t1;
	private JLabel l1;
	.........
	.......
Now our output is here .
Swing button textfield output

Displaying Message box

We can show the message box like this.
JOptionPane.showMessageDialog(null,"welcome to plus2net");
Swing Message box

Data Transfer from one window to other

From one JTextField in first window we can transfer and display entered data in another window
Transfering Data from one window to other

swing

Subscribe

* indicates required
Subscribe to plus2net

    plus2net.com



    Post your comments , suggestion , error , requirements etc here




    We use cookies to improve your browsing experience. . Learn more
    HTML MySQL PHP JavaScript ASP Photoshop Articles FORUM . Contact us
    ©2000-2024 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer