How to Swap Buttons in Java
22 hours ago, BuckGup said:I a trying to replace what a button says once I hit it. I am doing this
display.setText(""); display.setText(CALLING_DISPLAY_TEXT); topLeftButton.setEnabled(false); topLeftButton.setVisible(false); topRightButton.setVisible(false); topMiddleButton2.setEnabled(true);topMiddleButton is what I am replacing and topMiddleButton2 is what's going in it's place. I don't know if I need to add the second button to the Jpanel as even when I set the visibilty to false and it's disabled it shows up as a box and messes the formatting up. I am not sure if this is the best way to do it. As I just need to replace the text and modify the function a little
Use an ActionListener to modify the text (or any other attribute) of the button. Use the ActionEvent.getSource() method, cast the result to JButton (I assume you're using JButton, otherwise cast to whatever Button you're using), and perform whatever modifications you need on that (JButton.setText(), in your case). You only need one button, not two, this way.
You can also perform any other actions in this ActionListener.
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now