Reputation: 3213
I want to create my own custom buttons (or that is have a template to create mine own custom buttons). What methods do I need to override to make the same sized button that the JButton makes if I want it to look that same. Because I thought that if you extend a JButton to another button class, a custom button, that had the same size and look as the JButton. But when I put my button up and in the form it makes it really tiny and does not look like a normal button.
What I want is to have at least the same looking button as the JButton has. But I also want to have the add in borders, text and color to foreground and background? What are all of the methods that I would need to override and does anyone have a simple example?
Class: (JButton) -- My Button
import javax.swing.JButton;
public class DougCustomButton extends JButton {
DougCustomButton() {
super();
setBorder(BorderFactory.createLineBorder(Color.BLUE));
}
@Override
public void setPreferredSize(int a, int b) {
//What would I put in here, do I need super()
}
}
Class: (JPanel)
import java.awt.Dimension;
import java.awt.Graphics;
import javax.swing.JPanel;
public class genericPanel2 extends JPanel {
private static final long serialVersionUID = 1L;
DougCustomButton b2 = new DougCustomButton();
public genericPanel2() {
add(b2);
setPreferredSize(new Dimension(50, 30));
}
}
Class: (JFrame)
import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.Dimension;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
public class genericFrame2 {
Dimension dimension = new Dimension(450,450);
JFrame f = new JFrame("Not resizable");
genericPanel2 genericPanel = new genericPanel2();
public static boolean RIGHT_TO_LEFT = false;
public genericFrame2() {
initUI();
}
public static void addComponentsToPane(Container pane) {
}
public final void initUI() {
f.add(genericPanel);
f.setSize(dimension);
f.setResizable(true);
f.setLocationRelativeTo(null);
f.setTitle("Draw Line Test");
//addComponentsToPane(f.getContentPane());
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
genericFrame2 ex = new genericFrame2();
ex.f.setVisible(true);
}
}
Screen Shot:
My custom button is the size of a pixel? What is probably causing that?
Upvotes: 0
Views: 425
Reputation: 201429
You can set a Border
on a regular JButton
, just like every other JComponent - perhaps add a Constructor like
public DougCustomButton() {
super();
setBorder(BorderFactory.createLineBorder(Color.BLUE));
// and a font
Font aFont = new Font("Serif", Font.ITALIC | Font.BOLD);
setFont(aFont.deriveFont(18.0f));
}
Read more on How to Use Borders and Font.deriveFont(float).
Upvotes: 1
Reputation: 347184
Sizing information is provided to the layout manager via the getPreferred/Minimum/MaximumSize
methods. These generally delegate to the installed UI delegate, which uses, things like, the text, icon and margin properties to determine the size of the button.
Generally speaking, if you want to "customise" a button, you can use the Action
API, which provides the means to provide a self contained entity which can easily be applied to different components, such as menus, buttons and even used by the key bindings API
Take a look at How to use Actions for more details
Upvotes: 1