0

enter image description here

How i can create dynamic component as shown in picture? Component image could be change at run time. Component border and backgrond color should be changable at run time.

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
  • 1
    Just do `setBackground()`, `setBorder()` and `setIcon()` when you need to (probably inside a listener like an `ActionListener`). That should work just fine. – Lukas Rotter Nov 01 '15 at 14:14
  • But how i can render image inside camera icon as shown in picture – Gaurav vijayvargiya Nov 01 '15 at 14:34
  • My ideas would be to either make images of all combinations and use them, or, add a new JPanel with transparency to the button with a `GridLayout(2, 2)` and add panels with the corresponding image (like the bell) to it. (The camera would be the icon of the whole button) – Lukas Rotter Nov 01 '15 at 14:43
  • 1
    I'd use a variation of this [approach](http://stackoverflow.com/a/10862262/230513). – trashgod Nov 01 '15 at 16:31

1 Answers1

0

You can create a Button as below,

JButton button = new JButton();

try {
    Image img = ImageIO.read(getClass().getResource("resources/water.bmp"));
    button.setIcon(new ImageIcon(img));
} catch (IOException ex) {
    //catch exceptions
}

You can call

button.setIcon(new ImageIcon(img));

To change the button image at run time.

You can call

button.setBackground()

to change the background at runtime.

Have a look at JButton Documentation for more information.

Achintha Gunasekara
  • 1,165
  • 1
  • 15
  • 29