Quantcast
Channel: Programming Forums
Viewing all articles
Browse latest Browse all 51036

Enable/Disable button question

$
0
0
Hey there,

My UI is really straight forward. It has an"OK" button, a "cancel" button and a "disable" button.

Basically, what I want to do is, when I click the "disable" button, it disables the ok and cancel buttons.

Once the disable button is clicked, and the other buttons are disabled, the disable button should then turn to an enable button, which will re-enable the buttons.

Here is what I have so far. The buttons there, but they all just say "clicked" when clicked on. I'm not sure how to disable the buttons and etc.

Please forgive me in advance, I'm super new to this. Be easy on me please!

/**
 * JFrameForm class to represent our form
 * @author Sean Morrow
 */

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

public class JFrameForm extends JFrame implements ActionListener {
    
    // class variable to hold defaults for JFrame dimensions/location
    private static final int FRAME_WIDTH = 300;
    private static final int FRAME_HEIGHT = 200;
    private static final int FRAME_X = 150;
    private static final int FRAME_Y = 250;
    
    // form elements
    private JButton btnOk;
    private JButton btnCancel;
    private JButton btnDisable;
    
    // --------------------------------------------- constructor method
    public JFrameForm() {
        // initialization
        this.setTitle("My customized JFrame object!");
        this.setSize(FRAME_WIDTH, FRAME_HEIGHT);
        this.setLocation(FRAME_X, FRAME_Y);
        this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        
        // get reference to the contentPane (JPanel object)
        Container contentPane = this.getContentPane();
        // set the layout manager for the contentPane
        // APPROACH I
        //FlowLayout flowLayout = new FlowLayout();
        //contentPane.setLayout(flowLayout);
        // APPROACH II
        contentPane.setLayout(new FlowLayout());
        
        // add our JButton objects to the contentPane
        btnOk = new JButton("ok");
        btnOk.addActionListener(this);
        contentPane.add(btnOk);
        btnCancel = new JButton("cancel");
        btnCancel.addActionListener(this);
        contentPane.add(btnCancel);
        btnDisable = new JButton("disable");
        btnDisable.addActionListener(this);
        contentPane.add(btnDisable);
    }
    
    public void actionPerformed(ActionEvent event) {
        //JOptionPane.showMessageDialog(this, "You clicked a button! JOY!"); 
        
        // hardcode both to proper names on buttons
        btnOk.setText("ok");
        btnCancel.setText("cancel");
        // btnDisable.setText("disable");
        // which button was clicked to get us here?
        JButton clickedButton = (JButton) event.getSource(); 
        clickedButton.setText("clicked");
        
        
        
    }
    public void actionPerforme(ActionEvent event) {
        btnDisable.setText("enable");
        JButton clickedButton = (JButton) event.getSource(); 
        clickedButton.setText("clicked");
        
    }
    
    
}


Viewing all articles
Browse latest Browse all 51036

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>