I'm trying to extend a class to eventually make a custom button. I read in several places that it's best to extend as high as possible on the heirarchy (for better polymorphism?), so I'm trying to extend JComponent
:
import javax.swing.*;
import java.awt.*;
public class TestButton extends JComponent {
private static final long serialVersionUID = 1L;
public TestButton() {
super();
}
}
And the code that calls this is:
b1 = new TestButton();
basePanel.add(b1,gbc); // (gbc is GridBagConstraints object)
The thing is, my JComponent
isn't displayed in my layout. If I extend the class as JButton
, it shows no problem. What's the deal?
Update:
FYI, this is sort of a noob conceptual question, I'm far from proficient here obviously.
Here's a picture to describe. The only thing changed is extends ______
.
What should be happening is a purple-filled block, the same height as the yellow block on the bottom.
What is happening is a default sized block that has no background (the black is from the JFrame).
It is a blank component (basically a template). It has no properties. You have to add your own graphical elements by overriding the paintComponent method and then add logical elements by overriding the update method.