Changing JTextFields when not within a certain range & changing JPanel background using text from JTextFields

I have looked around and found various bits and pieces of information that have helped me so far but I am currently stuck with the following problems:

  1. My JTextFields do not change to 0 if they’re outside of the range of (0-255).

  2. The JPanel does not change colors based off what is typed in the JTextFields until one of the buttons is clicked.

This is the code that I currently have:

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

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener; 

public class App1 extends JFrame implements ActionListener
    {
            JPanel upperBase, lowerBase, jpRed, jpGreen, jpBlue, jpChanging;
            JLabel jLRed, jLGreen, jLBlue;
            JTextField jtf_red, jtf_green, jtf_blue;
            JButton rP, rM, gP, gM, bP, bM;

        public static void main(String[] args) 
    {
        App1 app = new App1();
    } 

    App1()
{
    // Creates Container
    // Sets Layout for Container
    Container cp = this.getContentPane();
    cp.setLayout(new BoxLayout (cp, BoxLayout.Y_AXIS));

     // Sets Window Title and Size
     // Exits Application on Close
    this.setTitle("Application 1");
    this.setSize(800, 600);
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    // Creates 6 JPanels
    upperBase = new JPanel();
    lowerBase = new JPanel();
    jpRed = new JPanel();
    jpGreen = new JPanel();
    jpBlue = new JPanel();
    jpChanging =  new JPanel();

    // Adds upper/lowerBase to Container
    cp.add(upperBase);
    cp.add(lowerBase);

    // Sets Layout for upper/lowerBase
    upperBase.setLayout(new GridLayout(1,3));
    lowerBase.setLayout(new GridLayout(1,1));

    // Sets Color for jpRed/Green/Blue
    jpRed.setBackground(Color.RED);
    jpGreen.setBackground(Color.GREEN);
    jpBlue.setBackground(Color.BLUE);
    jpChanging.setBackground(Color.BLACK);

    // Adds jpRed/Green/Blue to upperBase & adds jpChanging to lowerBase
    upperBase.add(jpRed);
    upperBase.add(jpGreen);
    upperBase.add(jpBlue);
    lowerBase.add(jpChanging);

    // Setup for Red JPanel (jpRed)
    jLRed = new JLabel("Red");
    jtf_red = new JTextField("0", 10);

    rP = new JButton("+");
    rM = new JButton("-");
    rP.addActionListener(this);
    rM.addActionListener(this);
    rP.setPreferredSize(new Dimension(25, 25));
    rM.setPreferredSize(new Dimension(25, 25));

    jpRed.add(jLRed);
    jpRed.add(rP);
    jpRed.add(jtf_red);
    jpRed.add(rM);


    // Setup for Green JPanel (jpGreen)
    jLGreen = new JLabel("Green");
    jtf_green = new JTextField("0", 10);

    gP = new JButton("+");
    gM = new JButton("-");
    gP.addActionListener(this);
    gM.addActionListener(this);
    gP.setPreferredSize(new Dimension(25, 25));
    gM.setPreferredSize(new Dimension(25, 25));

    jpGreen.add(jLGreen);
    jpGreen.add(gP);
    jpGreen.add(jtf_green);
    jpGreen.add(gM);

    // Setup for Blue JPanel (jpBlue)
    jLBlue = new JLabel("Blue");
    jtf_blue = new JTextField("0", 10);

    bP = new JButton("+");
    bM = new JButton("-");
    bP.addActionListener(this);
    bM.addActionListener(this);
    bP.setPreferredSize(new Dimension(25, 25));
    bM.setPreferredSize(new Dimension(25, 25));

    jpBlue.add(jLBlue);
    jpBlue.add(bP);
    jpBlue.add(jtf_blue);
    jpBlue.add(bM);

    // Visibility
    this.setVisible(true);
}

public void actionPerformed(ActionEvent e) {
    int red = Integer.parseInt(jtf_red.getText());
    int green = Integer.parseInt(jtf_green.getText());
    int blue = Integer.parseInt(jtf_blue.getText());

    // PLUS & MINUS, NO DECREMENTS PAST 0 and INCREMENTS PAST 255
    if(e.getSource() == rP)
    {
        red++;
        jtf_red.setText("" + red);

        if (red > 255)
        {
            jtf_red.setText("" + 255);
        }
    }
    else if(e.getSource()  == rM)
    {
        red--;
        jtf_red.setText("" + red);

        if (red < 0)
        {
            jtf_red.setText("" + 0);

        }
    }

    if(e.getSource() == gP)
    {
        green++;
        jtf_green.setText("" + green);

        if (green > 255)
        {
            jtf_green.setText("" + 255);
        }
    }
    else if(e.getSource()  == gM)
    {
        green--;
        jtf_green.setText("" + green);

        if (green < 0)
        {
            jtf_green.setText("" + 0);
        }
    }

    if(e.getSource() == bP)
    {
        blue++;
        jtf_blue.setText("" + blue);

        if (blue > 255)
        {
            jtf_blue.setText("" + 255);
        }
    }
    else if(e.getSource()  == bM)
    {
        blue--;
        jtf_blue.setText("" + blue);

        if (blue < 0)
        {
            jtf_blue.setText("" + 0);
        }
    }       

    // LIMITS (0-255)
    if (red < 0)
    {
        jtf_red.setText("" + 0);
    }
    else if (red > 255)
    {
        jtf_red.setText("" + 255);
    }

    if (green < 0)
    {
        jtf_green.setText("" + 0);
    }
    else if (green > 255)
    {
        jtf_green.setText("" + 255);
    }

    if (blue < 0)
    {
        jtf_blue.setText("" + 0);
    }
    else if (blue > 255)
    {
         jtf_blue.setText("" + 255);
    }

    jpChanging.setBackground(new Color(red, green, blue));
}

}

Any help would be greatly appreciated!

If more context/instruction is required, I have attached my assignment’s guidelines. As well as what the program is supposed to look like (my program looks like it and I’m more worried about functionality than the program looking exactly alike).

This is my first time posting so I hope I did everything right! Thank you for your time!

Answer

I asked classmates how they went about it and I figured it out.

For one, my buttons, my code is bad (I put a segment of it below to highlight my point). I don’t know how to go about explaining it but

if(e.getSource() == rP)
    {
        red++;
        jtf_red.setText("" + red);

        if (red < 255)
        {
            jtf_red.setText("" + 255);
        }
    }
    else if(e.getSource()  == rM)
    {
        red--;
        jtf_red.setText("" + red);

        if (red < 0)
        {
            jtf_red.setText("" + 0);

        }

Red gets incremented in the red++; line but then does not check if it is over 255.

The proper way would have been to move the red++; to the if statement inside of

if(e.getSource() == rP)
  {
    if (red < 255)
    {
       red++;
       ...
    }
   }

and so on and so forth for the other buttons.

The solution to my first problem

My JTextFields do not change to 0 if they’re outside of the range of (0-255).

I needed to add the ActionListener to the JTextFields after having coded the if statements that would set the JTextFields to 0 if values were entered that weren’t inside the range.

The solution to my second problem

The JPanel does not change colors based off what is typed in the JTextFields until one of the buttons is clicked.

With the ActionListener added to both buttons and JTextFields, I created an if statement that checked if the source were the buttons or JTextFields to change the background.

Leave a Reply

Your email address will not be published. Required fields are marked *