Home > Net >  Is it possible to make a toggleable button that changes text when clicked
Is it possible to make a toggleable button that changes text when clicked

Time:02-10

I have made a JFrame that shows a start button, and changes to stop when clicked. How to make it so that it changes its text to start when stop is clicked. Here is the source code:


    private JPanel contentPane;

    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    FRMCountdown frame = new FRMCountdown();
                    frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Create the frame.
     */
    public FRMCountdown() {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100, 100, 450, 300);
        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        setContentPane(contentPane);
        contentPane.setLayout(null);
        
        JButton Start_Stop_btn = new JButton("Start");
        Start_Stop_btn.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                Start_Stop_btn.setText("Stop");             
            }
            
        });
        Start_Stop_btn.setBounds(10, 188, 89, 23);
        contentPane.add(Start_Stop_btn);
    }
}``` 

CodePudding user response:

Oracle has a helpful tutorial, Creating a GUI With Swing. Skip the Learning Swing with the NetBeans IDE section.

Swing was designed to be used with layout managers. I used a FlowLayout to place one JButton. Null layouts and absolute positioning lead to problems.

Java field names start with a lower case letter, Java method names start with a lower case letter. Java class names start with an upper case letter.

Here's the modified code.

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class ToggleJButton {

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                new ToggleJButton();
            }
        });
    }
    
    public ToggleJButton() {
        JFrame frame = new JFrame("Toggle JButton");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);   
        
        frame.add(createMainPanel(), BorderLayout.CENTER);
        
        frame.pack();
        frame.setLocationByPlatform(true);
        frame.setVisible(true);
    }
    
    private JPanel createMainPanel() {
        JPanel panel = new JPanel(new FlowLayout());
        panel.setBorder(BorderFactory.createEmptyBorder(5, 100, 5, 100));
        
        JButton startStopButton = new JButton("Start");
        startStopButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent event) {
                JButton button = (JButton) event.getSource();
                String text = button.getText();
                if (text.contentEquals("Start")) {
                    text = "Stop";
                } else {
                    text = "Start";
                }
                button.setText(text);             
            }
            
        });
        panel.add(startStopButton);
        
        return panel;
    }
    
}
  •  Tags:  
  • Related