1

I am displaying the code that I wrote for changing the text when a button is clicked. Now I am trying to get the default text by clicking the button again, but unable to do so. Please advice.

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

public class Actions extends JFrame implements ActionListener{

    JPanel pnl=new JPanel();
    ImageIcon ic1=new ImageIcon("/home/mudit/Downloads/duke.png");
    JButton btn1=new JButton("Click",ic1);
    JTextArea ta=new JTextArea(5,37);

    public static void main(String[]args)   
     {
        Actions gui=new Actions();
     } 


    public Actions()
     {
        super("Swing Window");
        setSize(500,300);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        add(pnl);
        btn1.setHorizontalTextPosition(JButton.CENTER);
        btn1.setVerticalTextPosition(JButton.BOTTOM);
        ta.setLineWrap(true);
        ta.setWrapStyleWord(true);
        pnl.add(btn1);
        pnl.add(ta);
        btn1.setEnabled(true);
        ta.setText("Button is enabled");
        btn1.addActionListener(this);
        setVisible(true);
    }
    public void actionPerformed(ActionEvent event)
     {
        if(event.getSource()==btn1)
        {
            ta.setText("Button is Clicked");

        }
     }   
  }
mKorbel
  • 109,525
  • 20
  • 134
  • 319
Mudit
  • 11
  • 1
  • 2

2 Answers2

1

You can check the text of the button with an if-statement:

if (event.getSource() == btn1) {
    if (ta.getText().equals("Button is enabled")) {
        ta.setText("Button is Clicked");
    } else {
        ta.setText("Button is enabled");
    }
}
pzaenger
  • 11,381
  • 3
  • 45
  • 46
1

You could check actual text with the default text for example.

JPanel pnl=new JPanel();
ImageIcon ic1=new ImageIcon("/home/mudit/Downloads/duke.png");
JButton btn1=new JButton("Click",ic1);
JTextArea ta=new JTextArea(5,37);
String defaultText = "Button is enabled";

public static void main(String[]args)   
 {
    Actions gui=new Actions();
 } 


public Actions()
 {
    super("Swing Window");
    setSize(500,300);
    setDefaultCloseOperation(EXIT_ON_CLOSE);
    add(pnl);
    btn1.setHorizontalTextPosition(JButton.CENTER);
    btn1.setVerticalTextPosition(JButton.BOTTOM);
    ta.setLineWrap(true);
    ta.setWrapStyleWord(true);
    pnl.add(btn1);
    pnl.add(ta);
    btn1.setEnabled(true);
    ta.setText(defaultText);
    btn1.addActionListener(this);
    setVisible(true);
}
public void actionPerformed(ActionEvent event)
 {
    if(event.getSource()==btn1)
    {
        if(ta.getText().equals(defaultText))
              ta.setText("Button is Clicked");
        else
              ta.setText(defaultText);
    }
 }   }

Or in a simpler way you could use a boolean flag to do it so:

 JPanel pnl=new JPanel();
ImageIcon ic1=new ImageIcon("/home/mudit/Downloads/duke.png");
JButton btn1=new JButton("Click",ic1);
JTextArea ta=new JTextArea(5,37);
boolean hasBeenSet = false;

public static void main(String[]args)   
 {
    Actions gui=new Actions();
 } 


public Actions()
 {
    super("Swing Window");
    setSize(500,300);
    setDefaultCloseOperation(EXIT_ON_CLOSE);
    add(pnl);
    btn1.setHorizontalTextPosition(JButton.CENTER);
    btn1.setVerticalTextPosition(JButton.BOTTOM);
    ta.setLineWrap(true);
    ta.setWrapStyleWord(true);
    pnl.add(btn1);
    pnl.add(ta);
    btn1.setEnabled(true);
    ta.setText(defaultText);
    btn1.addActionListener(this);
    setVisible(true);
}
public void actionPerformed(ActionEvent event)
 {
    if(event.getSource()==btn1)
    {
        if(!hasBeenSet){
              ta.setText("Button is Clicked");
              hasBeenSet = true;
        }
        else{
              ta.setText(defaultText);
              hasBeenSet = false;
        }
    }
 }   }
Giuseppe
  • 307
  • 3
  • 18