-1

So I'm making a module that has three JTextField windows corresponding for 24 bit RGB color values "R", "G" and "B" with buttons for decrement/increment. The module is supposed to take these values and display the color. I did that and it works, but I need to ensure some details about the inputs and I don't know how to. These details are:

  1. if you enter a value outside of 0 and 255 it will be treated as 0
  2. if you click a decrement button when the value is 0 it won't drop it to -1
  3. if you click an increment button when the value is 255 it won't rise to 256. I have no idea how to do it. I tried to google it but I can't find what I need. I'd appreciate some guidance

EDIT: I tried to add some if statements to the ActionPerformed method but all it does is returns a whole bunch of errors when I run the module and try the value out of range

EDIT2: for example, I had this but it doesn't work :

@Override
public void actionPerformed(ActionEvent e) {
    String r,g,b;

    if (e.getSource() == tf1) {
        r = tf1.getText();
        this.r =Integer.parseInt(r);
        if (this.r < 0 && this.r > 255)
            this.r =0;
        color(); }

I have nothing for my buttons because I have completely no idea how to

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
zueyl
  • 11
  • 4
  • 2
    What have you tried so far? What did you find difficult? Where are you stuch? Show your effort and the code you have so far, please – Fureeish Nov 08 '18 at 17:26
  • I tried to do it with the if statements but it doesn't work. In general I have no idea how to solve it and where to start? I'm not looking for a solution just a guidance what I can use to make it work. I'll figure out the rest on my own – zueyl Nov 08 '18 at 17:30
  • 1
    Show us your attemtps then. Please, however, try to stay as close to [mcve] as possible. Carefully read what it is and apply that logic to make your example easy to be helped with – Fureeish Nov 08 '18 at 17:32
  • 2
    @zueyl Why do you not use a JSpinner since you want a number input field with up/down buttons? No need to work with a JTextField for this problem. – Progman Nov 08 '18 at 17:36
  • I edited my post with the code I had initially for it that didn't work – zueyl Nov 08 '18 at 17:36
  • @Progman it's a college assignment so it has been specified for us – zueyl Nov 08 '18 at 17:37
  • Your code is checking if r > 255 **AND** r < 0. A number cannot be greater than 255 and less than 0 so this statement will always return false. Did you mean **OR**? – Wrokar Nov 08 '18 at 17:46
  • @Wrokar yes you're right, but it still throws the same errors in the console – zueyl Nov 08 '18 at 17:49
  • @zueyl what is the error you're getting? Without knowing the exact error you're seeing in the console it's hard to go further. – Wrokar Nov 08 '18 at 17:51
  • *"I edited my post with the code"* So .. when will we see the MCVE suggested by @Fureeish. (In case you're confused, uncompilable code snippets != MCVE.) – Andrew Thompson Nov 09 '18 at 03:23
  • BTW - while the spinners suggested by @Progman are a good idea, I'd use a `JColorChooser` for this type of problem. – Andrew Thompson Nov 09 '18 at 03:24
  • Probably you need `repaint` : https://stackoverflow.com/questions/10768619/paint-and-repaint-in-java – Koray Tugay Nov 09 '18 at 03:25

1 Answers1

0

This sounds like you might need some data validation from your input sources. We can't see your code, but one way to validate would be have your getters/setters generated for your variables. (Depending on the editor you have, these can be generated rather quickly). Within the values setter - you should be able to do some data validation.

If you post some code, you might get more meaningful feedback.

More on getters and setters in Java: https://www.codejava.net/coding/java-getter-and-setter-tutorial-from-basics-to-best-practices

EDIT: Now that you've posted code. Change the && to || and that should take care of your issue.

It is still better to data validate this with a getter/setter.

Koray Tugay
  • 22,894
  • 45
  • 188
  • 319
Perry Moen
  • 41
  • 5