-1

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);

        }
    }
}


}
Volken
  • 255
  • 1
  • 4
  • 23
  • *"Though this does nothing for me"* - Neither does your out of context code snippet for us. Consider providing a [runnable example](https://stackoverflow.com/help/mcve) which demonstrates your problem. This is not a code dump, but an example of what you are doing which highlights the problem you are having. This will result in less confusion and better responses – MadProgrammer Oct 20 '15 at 22:39
  • The reason i refrained from posting why executable code is before i was told to try and only show the relevant snippets. I will update. – Volken Oct 20 '15 at 23:35
  • Have a look at the link which is provided in my example which should provide you with some idea of what we are trying to look for – MadProgrammer Oct 20 '15 at 23:42
  • Your posted example seems to work fine for me. I can enter invalid text into one or more of the fields and the invalid fields are updated correctly, according to your specifications – MadProgrammer Oct 20 '15 at 23:46
  • Variable names should NOT start with an upper case character. Some of your variable names are correct and some are not. Be consistent and follow Java conventions if you want people to read your code. – camickr Oct 21 '15 at 00:53

1 Answers1

0

Great question, next time please give us some executable code to help us better understand and solve your problem. From what I can see without being able to run the code you just missed a little thing. Instead of jumping right into the try catch statement, do the type changes outside of the statement and store that as a variable first.

For example:

String inputconverter=Tred.getText();

Regardless if it is an integer or not, this will not give an error so it can be left out of a try catch statement, and you don't even need to use the: R = Integer.parseInt(Tred.getText()); as you never actually use R for any aspect of the program. Instead go right into:

Integer.parseInt(inputconverter)

Hope this helps!

Josh A
  • 51
  • 6