sb33 sb33 - 1 month ago 11
Java Question

RGB JTextfields - changes colour even if other boxes are empty when 3 values are required

If anything is entered in the blue textfield (tblue) even when the others are empty, it changes the text to that colour, if a box is empty or contains letters, it should produce an error message. This error doesn't happen for the other boxes and it works as it's supposed to, even though it's the same code.

package FirstCE203Project;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

class CE203_2016_Ex1 extends JFrame {

JTextField tred, tgreen, tblue;
JLabel message; //to reference later on
JButton goButton, reset;


public CE203_2016_Ex1() {

JPanel panel1 = new JPanel(); //creates panels for the boxes that will hold the rgb values
JPanel panel2 = new JPanel();
JPanel panel3 = new JPanel();

tred = new JTextField("Red", 10);
tgreen = new JTextField("Green", 10); //creates boxes for rgb values
tblue = new JTextField("Blue", 10);

panel2.setLayout(new GridBagLayout()); //sets text to the centre of the panel

panel1.add(tred);
panel1.add(tgreen); //adding panels to frame
panel1.add(tblue);


add(panel1, BorderLayout.SOUTH); //adding panels to frame //location of panels on frame
add(panel2, BorderLayout.CENTER);
add(panel3, BorderLayout.NORTH);


message = new JLabel("hello"); //text
message.setForeground(new Color(255, 0, 0)); //original text set to red

JButton goButton = new JButton("Change"); //adds button to change colour
JButton reset = new JButton("Reset");


panel1.add(goButton);
panel2.add(message);
panel3.add(reset); //adding buttons to panels

goButton.addActionListener(new ButtonHandler(this));
reset.addActionListener(new ButtonHandler1(this)); //creating action listener

}


class ButtonHandler implements ActionListener {

private CE203_2016_Ex1 theApp;

public ButtonHandler(CE203_2016_Ex1 theApp) {
this.theApp = theApp;
}

@Override
public void actionPerformed(ActionEvent e) {
int a=0, b=0, c=0;

try {
a = Integer.parseInt(theApp.tred.getText());

if (a < 0) {
a = 200; //if statements for values above and below the targets set
tred.setText("200");
}

if (a > 255) {
a = 255;
tred.setText("255");
}
message.setText("CE203 Assignment submitted by: Steven Beresford - 1404398");
message.setForeground(new Color(a, b, c)); //changes colour to desired input
}

catch(NumberFormatException ex) {

message.setText("invalid input! please enter numbers only"); //text
message.setForeground(new Color(0, 0, 0)); //error message set to black
tred.setText("");
}

try {
b = Integer.parseInt(theApp.tgreen.getText());

if (b < 0) {
b = 200;
tgreen.setText("200");
}

if (b > 255) {
b = 255;
tgreen.setText("255");
}
message.setText("CE203 Assignment submitted by: Steven Beresford - 1404398");
message.setForeground(new Color(a, b, c)); //changes colour to desired input
}
catch(NumberFormatException ey) {

message.setText("invalid input! please enter numbers only"); //text
message.setForeground(new Color(0, 0, 0)); //original text set to red
tgreen.setText("");

}

try {
c = Integer.parseInt(theApp.tblue.getText());

if (c < 0) {
c = 200;
tblue.setText("200");
}

if (c > 255) {
c = 255;
tblue.setText("255");
}
message.setText("CE203 Assignment submitted by: Steven Beresford - 1404398");
message.setForeground(new Color(a, b, c)); //changes colour to desired input
}
catch(NumberFormatException ez) {

message.setText("invalid input! please enter numbers only"); //text
message.setForeground(new Color(0, 0, 0)); //original text set to red
tblue.setText("");
}

}
}


class ButtonHandler1 implements ActionListener {

private CE203_2016_Ex1 theApp;

public ButtonHandler1(CE203_2016_Ex1 theApp) {
this.theApp = theApp;
}

@Override
public void actionPerformed(ActionEvent e) {
message.setText("CE203 Assignment submitted by: Steven Beresford - 1404398");
message.setForeground(new Color(255, 0, 0)); //resets message back to red
tred.setText("Red");
tgreen.setText("Green");
tblue.setText("Blue");
}
}

public static void main(String[] args) {
JFrame app = new CE203_2016_Ex1();
app.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
app.setSize(700, 700);
app.setVisible(true);
}
}

Answer

It actually does what you want, but the text is then overridden immediately (and this is why you can't see the change).

Let's say that you enter an invalid value for green (b).

The message will be set to "invalid input ...".

try {
    b = Integer.parseInt(theApp.tgreen.getText());
    ...
} catch (NumberFormatException ey) {
    message.setText("invalid input! please enter numbers only");
    ...
}

but then you have a valid input for blue (c):

try {
    c = Integer.parseInt(theApp.tblue.getText());
    ...
    message.setText("CE203 Assignment submitted by: Steven Beresford - 1404398");
    ...
}

and this will override the "invalid input ..." value with "CE203 Assignment ...".


A possible solution:

    @Override
    public void actionPerformed(ActionEvent e) {
        int a = 0, b = 0, c = 0;
        boolean isValidA = true, isValidB = true, isValidC = true;

        try {
            a = Integer.parseInt(theApp.tred.getText());
        } catch (NumberFormatException ex) {
            isValidA = false;
        }

        try {
            b = Integer.parseInt(theApp.tgreen.getText());
        } catch (NumberFormatException ex) {
            isValidB = false;
        }

        try {
            c = Integer.parseInt(theApp.tblue.getText());
        } catch (NumberFormatException ex) {
            isValidC = false;
        }

        if (!isValidA) {
            tred.setText("");
        } else {
            if (a < 0) {
                a = 200;
                tred.setText("200");
            } else if (a > 255) {
                a = 255;
                tred.setText("255");
            }
        }

        if (!isValidB) {
            tgreen.setText("");
        } else {
            if (b < 0) {
                b = 200;
                tgreen.setText("200");
            } else if (b > 255) {
                b = 255;
                tgreen.setText("255");
            }
        }

        if (!isValidC) {
            tblue.setText("");
        } else {
            if (c < 0) {
                c = 200;
                tblue.setText("200");
            } else if (c > 255) {
                c = 255;
                tblue.setText("255");
            }
        }
        if (isValidA && isValidB && isValidC) {
            message.setText("CE203 Assignment submitted by: Steven Beresford - 1404398");
            message.setForeground(new Color(a, b, c));
        } else {
            message.setText("invalid input! please enter numbers only");
            message.setForeground(new Color(0, 0, 0));
        }

    }