how to change the text of a button once that button is pressed

I have a button called Connect once I press the connect button I want the text of that button to be disconnect. So when the user starts the application it will be connect and once the button is clicked it will be disconnect when the user wants to close the application they will hit disconnect.

Is that possible to do this?

Answers


Netbeans is very intuitive and easy to use... try this:

Declare

private javax.swing.JButton jButton2;

Construct and set Lsitener

   jButton2 = new javax.swing.JButton();
    jButton2.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            jButton2ActionPerformed(evt);
        }
    });

Get Click Action Event

private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {                                         
        jButton2.setText("Hello");
    } 

Hi the below code snippet will work for you

 connectBtn.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {
            connectBtn.setText("Dissconnect");
        }

    });

connectBtn is instance of your Button Component


Need Your Help

Java Netbeans does not have enough memory to compile

java netbeans

Currently Im doing my FYP for my bachelors degree.

Text to Columns is getting a "method 'range' of object '_global' failed"

vba excel-vba excel

I took the macro code from using the "text to columns" wizard and put it in my code. But it's not working on a consistent basis.