I've set following properties for one of my buttons.
try {
name.setIcon(new javax.swing.ImageIcon(ImageIO.read(newFile("./src/main/java/resorces/Default.png"))));
} catch (IOException ex) {
Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
}
name.setBorderPainted(false);
name.setContentAreaFilled(false);
name.setFocusPainted(false);
name.setOpaque(false);
My problem is that I have a bunch of buttons just like this one. I was wondering if it's possible to make a method that would take in button's name and set all the properties for me.
Example:
public void SetProperties(??? x){
try {
x.setIcon(new javax.swing.ImageIcon(ImageIO.read(newFile("./src/main/java/resorces/Default.png"))));
} catch (IOException ex) {
Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
}
x.setBorderPainted(false);
x.setContentAreaFilled(false);
x.setFocusPainted(false);
x.setOpaque(false);
}
Thanks for help!
You could...
Use a "builder pattern"
The concept is to provide a workflow which allows you to apply all the properties you want and then have that object "built", for example...
nb: Note, a common pattern for a builder is store the properties in some kind of cache/lookup, which are then applied when you call build, but in this case, it's just easier to apply them directly to the button itself
nb: Obviously, I've only supplied a small subset of properties you might want to specify for a button, you'll need to add the rest ;)
And then you can build the button using something like...
Yes, I know, I was getting to it. Once you have a "base" builder, you could make one or more "custom" extensions, which could apply default values directly, for example...
which could then be used something like...
You could...
Use a "factory pattern", for example...
which could be used something like...
The factory pattern could allow you to supply a multiple number of different types of buttons, configured different based on your common needs.
You could...
Combine the two concepts, for example...