0

I just wanted to know if there was a way of making this code simpler. Thanks.

private void btnBlueActionPerformed(java.awt.event.ActionEvent evt) { btnConvert.setBackground(new java.awt.Color(84, 209, 241)); //Changes the colors to blue btnReset.setBackground(new java.awt.Color(84, 209, 241)); //Changes the colors to blue btnClose.setBackground(new java.awt.Color(84, 209, 241)); //Changes the colors to blue btnInfo.setBackground(new java.awt.Color(84, 209, 241)); //Changes the colors to blue txtIncome.setBackground(new java.awt.Color(127, 228, 255)); //Changes the colors to blue txtPayable.setBackground(new java.awt.Color(127, 228, 255));//Changes the colors to blue txtStatus.setBackground(new java.awt.Color(127, 228, 255)); //Changes the colors to blue txtIncome.setForeground(new java.awt.Color(89, 89, 89)); //Changes the colors to blue txtPayable.setForeground(new java.awt.Color(89, 89, 89)); //Changes the colors to blue txtStatus.setForeground(new java.awt.Color(89, 89, 89)); //Changes the colors to blue } 
1
  • 1
    Use constants. Create variables for each of those and re-use them. Commented Feb 25, 2014 at 23:23

3 Answers 3

2

Instead of re-creating the same color over and over you could just create each one once, and then assign them as necessary:

Color btnColor = new Color(84, 209, 241); Color txtColor1 = new Color(127, 228, 255); Color textcolor2 = new Color(89, 89, 89); 

Then:

private void btnBlueActionPerformed(java.awt.event.ActionEvent evt) { btnConvert.setBackground(btnColor); btnReset.setBackground(btnColor); btnClose.setBackground(btnColor); btnInfo.setBackground(btnColor); txtIncome.setBackground(txtColor1); txtPayable.setBackground(txtColor1); txtStatus.setBackground(txtColor1); txtIncome.setForeground(textcolor2); txtPayable.setForeground(textcolor2); txtStatus.setForeground(textcolor2); } 
Sign up to request clarification or add additional context in comments.

1 Comment

OP should note that they have to add import java.awt.*; to the top of their file.
0

Looks like your objects should actually be parts of collections that are grouped together.

// assuming "mainMenuButtons" (or whatever) contains your first four objects // note I don't know what color (84, 209, 241) actually is for(Button btn : mainMenuButtons) { btn.setBackground(new java.awt.Color(84, 209, 241)); } 

Secondly those colors are immutable. There's no need to create so many objects

Color reddish = new java.awt.Color(84, 209, 241); // then just use reddish everywhere instead of creating a new color each time. 

Comments

0

To simplify both the code and changes you may want to make in the future, do the following:

  1. Create a container for components that should share the same foreground or background
  2. Create a container for colors (indexed by state)
  3. Make a small ComponentUtil Class that lets you change colors for all members of a container

When an action changes the state of the components, you pick up the correct color for the component and call ComponentUtil.setBackground(components, color); or ComponentUtil.setForeground(components, color);

import java.awt.Color; import java.awt.Component; public class ComponentUtil { public static void setBackground(Component[] components, Color color) { for (int i=0; i < components.length; i++) { components[i].setBackground(color); } } public static void setForeground(Component[] components, Color color) { for (int i=0; i < components.length; i++) { components[i].setForeground(color); } } } 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.