5

I found a similar question here:
overriding methods without subclassing in Java

But mine a little different, I have two classes, 1 GUI based, and the other just methods to modify elements in first class. If it just editing the basic function, I meet no problem, but now I want to override a jbutton in first class method from the second class without inheriting it. Where do I have to start?

I have temporary solution which is that the second class extends JButton, override method I want to, and add that class to my GUI class (anonymous object or not, is doesn't matter). But I want to discover a way to find a way to my question, is it even possible? Thanks :)

Edit
Here's sample code:

First class, as it only a button in jframe, I only add these in constructor:
ButtonOverrider bo=new ButtonOverrider(); -> this is the overrider class
button=bo.overridePaintComponent(bo); //first try
button=bo.overridePaintComponent(); //second try
bo.overridePaintComponent(bo); //third try

And here's the ButtonOverrider method:

public JButton ButtonOverrider(JButton button) {
  button = new JButton() {
    @Override
    protected void paintComponent(Graphics g) {
      Graphics2D g2 = (Graphics2D) g.create();
      GradientPaint gp = new GradientPaint(0, 0,
      Color.blue.brighter().brighter(), 0, getHeight(),
      getBackground().darker().darker());

      g2.setPaint(gp);
      g2.fillRect(0, 0, getWidth(), getHeight());
      g2.dispose();

      super.paintComponent(g);
      super.setContentAreaFilled(false);
      super.setFocusPainted(false);
      super.setBorder(new LineBorder(Color.yellow, 2));
      super.setText("Shuro");
    }
  };
  return button;
}
5
  • can you post minimal sample code showing what you want Commented Jun 8, 2013 at 8:52
  • overriding in its formal sense not possible without subclassing in my humble opinion Commented Jun 8, 2013 at 8:55
  • @RC. question updated Commented Jun 8, 2013 at 9:14
  • It seems to me like what you want to do is the decorator pattern. A Decorator is a class which implements an interface and encapsulates an object of the same interface. Each method can be delegated to the encapsulated object, execute code before and/or after doing so or do something entirely different. Decorators have the nice ability to be stackable - you can wrap an object in a decorator in a decorator in a decorator. Commented Jun 8, 2013 at 9:21
  • You say your example is different to in the other question - a GUI class, a class with methods, an anonymous class etc, are all classes - therefore they all follow the rules of what being a class is. Commented Jun 8, 2013 at 9:32

1 Answer 1

7

Where do I have to start?

With inheritance. That's the only way that overriding makes any sense. It's not clear why you don't want to use inheritance, but that really is the only way of overriding a method. Whether you use an anonymous class or a named one is irrelevant, but it must extend the class in order to override the method. That's simply the way overriding works in Java.

EDIT: The code you've shown in your updated question does use inheritance by creating an anonymous inner class... but it doesn't do what I expect you want it to, because it creates a new object rather than overriding the method on the existing object. Your parameter value is never used.

Sign up to request clarification or add additional context in comments.

3 Comments

@A-SM: That is using inheritance. That's an anonymous inner class - you're extending JButton. But you're also completely ignoring the parameter - it's creating a new button.
:x I didn't have idea if that was inheritance, but if it is, then there should be a way to do what I write in my question right? That code doesn't change anything in my button.
@A-SM: No, there isn't be a way to do what you've asked, because you can't change the type of an existing object. As I said, you're completely ignoring the parameter, which is why that code doesn't work.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.