0

Is there any way to color a JButton? I want to change it's color after it's pressed to show that it has been pressed. Is there a way that I can do this?

Also, in a JFrame, is there a way to make it so that the whole contents of the window will stretch to the fill the frame when the window is maximized?

Thanks, I appreciate it.

user3268401
  • 319
  • 1
  • 7
  • 21
  • *"I want to change it's color after it's pressed to show that it has been pressed"* Do you want the button to stay pressed? What if they press it again? – MadProgrammer Apr 08 '14 at 23:41

1 Answers1

0

Here's a link for changing JButton color on every state.

How to change a JButton color on mouse pressed?

For the JFrame, you can set the Layout of the JFrame to BorderLayout and add the JPanel in the center of the JFrame, so it will fill all the JFrame even when resized.

Example:

frame.setLayout(new BorderLayout());
frame.add(panel,BorderLayout.CENTER);
Community
  • 1
  • 1
CMPS
  • 7,733
  • 4
  • 28
  • 53
  • What if I'm using a FlowLayout? Do you know of any easy way to do this so I don't have to redo the whole program with a BorderLayout instead? – user3268401 Apr 08 '14 at 23:27
  • You're using FlowLayout for what, the JFrame or the JPanel ? @user3268401 – CMPS Apr 08 '14 at 23:31
  • @user3268401 If for the JFrame, then remove what was added to the JFrame and add them to a new JPanel of Layout FlowLayout. Then this JPanel add it to the JFrame of BorderLayout in the Center using the above example in my answer – CMPS Apr 08 '14 at 23:36
  • @user3268401 No, `FlowLayout` repects the preferred size of the component, therefore it won't be appropriate. `BorderLayout`, `GridLayout` and `GridBagLayout` all provide the functionality you're looking for – MadProgrammer Apr 08 '14 at 23:41
  • @MadProgrammer Why borderLayout is not appropriate ? Btw I agree that you can achieve this also with GridLayout and GridBagLayout – CMPS Apr 08 '14 at 23:45
  • 1
    @AmirBawab I agree that `BorderLayout` is one of the layout managers that would achieve this, `FlowLayout` can't, sorry if I didn't make myself clear the first time – MadProgrammer Apr 08 '14 at 23:59