Home Java Example Java Swing Changing the Label of a JButton Component in Java

Related Tutorials


 
 

Share on Google+Share on Google+

Changing the Label of a JButton Component in Java

Advertisement
This section illustrates you how to change the label of a button in java swing.

Changing the Label of a JButton Component in Java

     

This section illustrates you how to change the label of a button in java swing. JButton is the component of javax.swing.*; package.

The following program helps you to change the label of the button. In this program, the label of the button is changed from "Click Me" to "Roseindia.net" and vice versa whenever you click on the button as shown below:

   Before:
Button Without change the label.

  After:
Changed label of the button.

In this program, addActionListener() method has been added to the button to register the action listener and then if you click on the button, the generated action event is captured in the actionPerformed(ActionEvent e) method. In the actionPerformed(ActionEvent e){ we check the label of the button. If the label of the button is "Roseindia.net", it will change the button label to "Click Me" otherwise it will change the label to "Roseindia.net".

setText(String):
Above method sets the label of the button.

Here is the code of the program:

import javax.swing.*;
import java.awt.event.*;

public class ChangeButtonLabel{
  JButton button;
  public static void main(String[] args){
  ChangeButtonLabel cl = new ChangeButtonLabel();
  }

  public ChangeButtonLabel(){
  JFrame frame = new JFrame("Change JButton Lebel");
  button = new JButton("Click Me");
  button.addActionListener(new MyAction());
  frame.add(button);
  frame.setSize(400400);
  frame.setVisible(true);
  frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  }

  public class MyAction implements ActionListener{
  public void actionPerformed(ActionEvent e){
  String text = (String)e.getActionCommand();
  if (text.equals("Click Me")){
  button.setText("Roseindia.net");
  }
  else{
  button.setText("Click Me");
  }
  }
  }
}

Download this example.

Advertisement

If you enjoyed this post then why not add us on Google+? Add us to your Circles



Liked it!  Share this Tutorial


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: April 14, 2007

Related Tutorials

Discuss: Changing the Label of a JButton Component in Java   View All Comments

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments:4
tsegalem
December 10, 2011
hi

this helped me a little bit. but i need to help me with implementing 9(3,3)buttons the firs and the last row will have initial label.and i want to move the labels to the closest free button.(its submission is December 19/2011)
Antonio
January 30, 2012
Question for this code

Dear friend i have a similar problem to your example, but i have to put actionlistener in a different class outside of the the main class. to be more clear my problem is like that: import javax.swing.*; import java.awt.event.*; public class ChangeButtonLabel{ JButton button; public static void main(String[] args){ ChangeButtonLabel cl = new ChangeButtonLabel(); } public ChangeButtonLabel(){ JFrame frame = new JFrame("Change JButton Lebel"); button = new JButton("Click Me"); button.addActionListener(new MyAction()); frame.add(button); frame.setSize(400, 400); frame.setVisible(true); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } } import java.awt.event.ActionEvent; import java.awt.event.ActionListener; public class MyAction implements ActionListener{ public void actionPerformed(ActionEvent e){ String text = (String)e.getActionCommand(); if (text.equals("Click Me")){ button.setText("Roseindia.net"); } else{ button.setText("Click Me"); } } } I get an error that buttons is not cannot be resolved, any help wold be available.
ankit
September 12, 2012
java

when i click on any rectangle then co-ordinates changed....
sathish
March 25, 2013
super example

java super example
DMCA.com