Showing posts with label remove. Show all posts
Showing posts with label remove. Show all posts

Remove component from JFrame during runtime

Source code below will show you, how to remove component from a JFrame during program runtime. Component that will be use in source code below is a group of JButton.

**************************************************************************
COMPLETE SOURCE CODE FOR : RemoveComponentOnJFrameAtRuntime.java
**************************************************************************


import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JButton;
import javax.swing.JOptionPane;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.Component;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class RemoveComponentOnJFrameAtRuntime extends JFrame implements ActionListener
{
JPanel panel;

public RemoveComponentOnJFrameAtRuntime()
{
super("Remove component on JFrame during runtime");
setLayout(new BorderLayout());

panel=new JPanel();
panel.setLayout(new FlowLayout());

JButton button1=new JButton("BUTTON 1");
JButton button2=new JButton("BUTTON 2");
JButton button3=new JButton("BUTTON 3");
JButton button4=new JButton("BUTTON 4");
JButton button5=new JButton("BUTTON 5");

panel.add(button1);
panel.add(button2);
panel.add(button3);
panel.add(button4);
panel.add(button5);

add(panel,BorderLayout.CENTER);

JButton removeButton=new JButton("Click here to remove button");
removeButton.addActionListener(this);

add(removeButton,BorderLayout.SOUTH);

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(500,500);
setVisible(true);
}

public void actionPerformed(ActionEvent event)
{
Component[]storeAllButtonInPanel=panel.getComponents();

if(storeAllButtonInPanel.length!=0)
{
panel.remove(storeAllButtonInPanel.length-1);
panel.revalidate();
validate();
repaint();
}

else
{
JOptionPane.showMessageDialog(this,"No button to remove");
}
}

public static void main(String[]args)
{
RemoveComponentOnJFrameAtRuntime rcojfat=new RemoveComponentOnJFrameAtRuntime();
}
}


**************************************************************************
JUST COMPILE AND EXECUTE IT
**************************************************************************

Remove JFrame title bar

Source code below will show you, how to remove title bar from a JFrame.

******************************************************************************
COMPLETE SOURCE CODE FOR : RemoveJFrameTitleBar.java
******************************************************************************


import javax.swing.JFrame;

public class RemoveJFrameTitleBar
{
public static void main(String[]args)
{
//Create a JFrame
JFrame frame=new JFrame();

//Method setUndecorated(true) will eliminate JFrame title bar
frame.setUndecorated(true);

//Set JFrame size to :
//Width : 400 pixels
//Height : 400 pixels
frame.setSize(400,400);

//Make JFrame visible. So we can see it.
frame.setVisible(true);
}
}


******************************************************************************
JUST COMPILE AND EXECUTE IT
******************************************************************************

Remove element from array

*************************************************
COMPLETE SOURCE CODE FOR : RemoveElementFromAnArray.java
*************************************************


public class RemoveElementFromAnArray
{
public static void main(String[]args)
{
//Now we want to remove last element,"crocodile"
String[]animal={"lion","tiger","elephant","crocodile"};

//Step 1:Create a temp array with sizeArrayAnimal-1
String[]temp=new String[animal.length-1];

//Step 2:Copy element from "lion" to "elephant" into array temp
System.arraycopy(animal,0,temp,0,(animal.length-1));

//Make animal array point to temp array
animal=temp;

//Print all element in animal
int temp2=0;
while(temp2!=animal.length)
{
System.out.println(animal[temp2]);
temp2=temp2+1;
}
}
}


*************************************************
JUST COMPILE AND EXECUTE IT
*************************************************

RELAXING NATURE VIDEO