Java swing component replacement

I was working on my new project last night and found a requirement that I was not sure it was possible but I tried it out and it worked, obviously had a bit of google help but ultimately managed to accomplish just what I wanted to replace exiting initiated component with new component .

I found that very useful so created a quick example where I initialise a grid with 4 Jbuttons and once a button is pressed in this case I just it simply using the last initiated button to remove the initial internalframe with all the buttons and replace it with the secondary internals frame that hold 12 buttons.

Take a look #Java components being replaced after being initialised and made visible.



We will be replacing those objects located in f1 (internaFrame 1):

for(int i=1 ; i<=4 ; i++){
btnOne = new JButton(String.valueOf(i));
if (i == 4){
btnOne = new JButton("More");

For those objects located in f2 (internaFrame 2):

for(int i=1 ; i<=16 ; i++){
btnTwo = new JButton(String.valueOf(i));
if (i == 16){
btnTwo = new JButton("Less");
This is the full code if you want to try it out: java-object-relacement-01

Leave a Reply