简体   繁体   中英

Getting Methods from another Class in Java

I'm new to Java, so please bear with me if this is the stupidest thing you have ever heard.

So, basically I'm creating a form in Java. When it is submitted, it is validated, and if there in an error, I want the specific form item to turn red. I've managed to do so by calling the method setAsError()

public void setAsError(){
    this.setBackground(new Color(230,180,180));
}

But the problem is, my form items (Includes Text fields, Combo Boxes and other swing classes) already extends the Java components.

public class KComboBox extends JComboBox {

public KComboBox(String[] s){
    super(s);
    //There's other stuff in here too
}

I want to add the setAsError() method to my form items. I realize that I could individually add the method to all the necessary classes, but this seems a bit odd for OO programming.

In any case, the end result should be, when I do

myFormItem.setAsError()

the field should turn red.

Any help would be appreciated, and thanks in advance.

Java supports multiple interface inheritance, which allows an object to inherit many method signatures from different interfaces.

So create new interface with setAsError method add implement for all relevant classes.

Something like:

public interface ErrorItf {
 public void setAsError();
}

After, add it to your class:

public class KComboBox extends JComboBox implements ErrorItf{

...

  @override
  public void setAsError(){
    this.setBackground(new Color(230,180,180));
  }

}

Now instead to call your class, call by interface, like:

ErrorItf element = getComboBox();
element.setAsError(); 

When you send a message to an object (aka element.setAsError() ) even though you don't know what specific type it is JComboBox or JTextarea ... . That's called polymorphism

As a side note, example how interface can help in your case

在此处输入图片说明

public void setAsError(JComponent component){
    if(component instanceof JTextField){
    JTextField field =  (JTextField) component;
    field.setBackground(Color.RED);
    }else if(component instance of JTextArea){
    JTextArea area = (JTextArea) component;
    area.setBackground(Color.RED);
    }
}  

By simply adding a JComponent argument to your setError() , you can achieve what you want.

Now, say you found that the user has left the 'name' field in the form empty. When he/she clicks the submit button, I am sure you are checking for empty condition.

if(nameField.getText().equals("")){
    this.setAsError(nameField);
}  

Now, I am pretty sure you need to reset the background color to what it was once you found out the error has gone away from that field so I suggest you also make a resetError(JComponent compo) where you reset the color to default.
Now, background color can differ from Windows to Mac to Ubuntu. What you need to do is that get the background color, at the start of the application, and store it.
When the error condition is gone, set it to the default value you stored.

Your best bet here is to create an interface. Just write

Interface SetAsErrorable{
    void setAsError();
}

In global scope (or in its own file). Then add to all your form item classes implements SetAsErrorable . IE KComboBox extendsJComboBox implements SetAsErrorable{ .

Sounds like using an interface would be good...

public interface errorInterface {
 public setAsError();
}

public class KComboBox extends JComboBox implements errorInterface {

 @Override
 setAsError() {
    this.setBackground(new Color(230,180,180));
 }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM