Im trying to create a simple RGM model that changes the text colour to the colour set by the user using three TextFields (Red,Green,Blue). Once i input numbers into the three fields then press a "Create" button, the colour of the text will change to the colour set by the integers (R,G,B). If when the button is pressed, the content of any text fields is not an integer, the invalid field(s) should be cleared and an appropriate message should appear. Text fields containing integers must never be cleared though.
I am struggling to do the part where the where if anything that isnt an integer is entered. I have tried try and catch methods on each colour integer (R,G,B) like so:
try{
R = Integer.parseInt(Tred.getText());
}catch (Exception ex){
out.setText("Invalid Input in Red.");
R = 0;
Tred.setText("");
}
Though this does nothing for me. Here is my full actionPerformed which handles what happens when the buttons are pressed:
@Override
public void actionPerformed(ActionEvent e) {
try{
try{
R = Integer.parseInt(Tred.getText());
}catch (Exception ex){
out.setText("Invalid Input in Red.");
R = 0;
Tred.setText("");
}
try{
G = Integer.parseInt(Tgreen.getText());
}catch (Exception ex){
out.setText("Invalid Input in Green.");
G = 0;
Tgreen.setText("");
}
try{
B = Integer.parseInt(Tblue.getText());
}catch (Exception ex){
out.setText("Invalid Input in Blue.");
B = 0;
Tblue.setText("");
}
inputColor = new Color(R,G,B);
switch (theOp){
case 'C':
out.setText("My Name");
out.setForeground(inputColor);
break;
case 'R':
out.setText("Welcome");
out.setForeground(Color.green);
Tred.setText("");
Tgreen.setText("");
Tblue.setText("");
break;
}
}catch(Exception e1){
out.setText("Invalid Input captured");
out.setForeground(Color.black);
}
}
}
Would i maybe need to use an if statement for each colour integer ?
[UPDATE] As requested, here is an executable example of my code:
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class Ex1 extends JApplet {
//Here i create the 3 primary colour integers
int R = 0,G = 0,B = 0;
//inputColor will be the color the user created
Color inputColor = new Color(R,G,B);
TextField Tred,Tgreen,Tblue;
JButton buttCreate,buttReset;
JLabel sR,sG,sB,out;
JPanel topPanel,centrePanel;
public void init()
{
this.setSize(700, 100);
topPanel = new JPanel();
centrePanel = new JPanel();
buttCreate = new JButton("Create!");
buttCreate.addActionListener(new ButtonHandler(this,'C'));
buttReset = new JButton("Reset");
buttReset.addActionListener(new ButtonHandler(this,'R'));
Tred = new TextField("",3);
Tgreen = new TextField("",3);
Tblue = new TextField("",3);
sR = new JLabel("R:");
sG = new JLabel("G:");
sB = new JLabel("B:");
out = new JLabel
("Welcome to CE203 Assignment 1,submitted by: Lewis Horsley 1405160");
out.setForeground(Color.green);
topPanel.add(sR);
topPanel.add(Tred);
topPanel.add(sG);
topPanel.add(Tgreen);
topPanel.add(sB);
topPanel.add(Tblue);
topPanel.add(buttCreate);
topPanel.add(buttReset);
centrePanel.add(out);
this.add(centrePanel, BorderLayout.CENTER);
this.add(topPanel, BorderLayout.NORTH);
}
class ButtonHandler implements ActionListener {
Ex1 theApplet;
char theOp;
ButtonHandler(Ex1 app, char op){
this.theApplet = app;
this.theOp = op;
}
@Override
public void actionPerformed(ActionEvent e) {
try{
try{
R = Integer.parseInt(Tred.getText());
}catch (Exception ex){
out.setText("Invalid Input in Red.");
R = 0;
Tred.setText("");
}
try{
G = Integer.parseInt(Tgreen.getText());
}catch (Exception ex){
out.setText("Invalid Input in Green.");
G = 0;
Tgreen.setText("");
}
try{
B = Integer.parseInt(Tblue.getText());
}catch (Exception ex){
out.setText("Invalid Input in Blue.");
B = 0;
Tblue.setText("");
}
inputColor = new Color(R,G,B);
switch (theOp){
case 'C':
out.setText("My Name");
out.setForeground(inputColor);
break;
case 'R':
out.setText("Welcome");
out.setForeground(Color.green);
Tred.setText("");
Tgreen.setText("");
Tblue.setText("");
break;
}
}catch(Exception e1){
out.setText("Invalid Input captured");
out.setForeground(Color.black);
}
}
}
}