import java.awt.Color;

import java.awt.Dimension;

import java.awt.FlowLayout;

import java.awt.Frame;

 

import javax.swing.JComboBox;

import javax.swing.JFrame;

import javax.swing.JOptionPane;

import javax.swing.JPanel;

import javax.swing.UIManager;

import javax.swing.event.PopupMenuEvent;

import javax.swing.event.PopupMenuListener;

 

public class CreatePlaceHolderinJComboBox extends JFrame{

JComboBox<String> cmbPlaceholder=new JComboBox<String>();

JPanel mainpanel=new JPanel();

JFrame frame;

public CreatePlaceHolderinJComboBox() {

init();

cmp();

Action();

}

private void Action() {

cmbPlaceholder.addPopupMenuListener(new PopupMenuListener() {

public void popupMenuWillBecomeVisible(PopupMenuEvent arg0) {

cmbPlaceholder.removeAllItems();

cmbPlaceholder.addItem("");

cmbPlaceholder.addItem("Admin");

cmbPlaceholder.addItem("Super Admin");

cmbPlaceholder.addItem("Executive");

}

public void popupMenuWillBecomeInvisible(PopupMenuEvent arg0) {

if(cmbPlaceholder.getSelectedIndex()==0){

cmbPlaceholder.removeAllItems();

cmbPlaceholder.addItem("User Type");

cmbPlaceholder.addItem("Admin");

cmbPlaceholder.addItem("Super Admin");

cmbPlaceholder.addItem("Executive");

}

}

public void popupMenuCanceled(PopupMenuEvent arg0) {

cmbPlaceholder.removeAllItems();

cmbPlaceholder.addItem("User Type");

cmbPlaceholder.addItem("Admin");

cmbPlaceholder.addItem("Super Admin");

cmbPlaceholder.addItem("Executive");

}

});

}

private void cmp() {

add(mainpanel);

mainpanel.setLayout(new FlowLayout());

mainpanel.add(cmbPlaceholder);

cmbPlaceholder.setPreferredSize(new Dimension(150,35));

cmbPlaceholder.addItem("User Type");

cmbPlaceholder.addItem("Admin");

cmbPlaceholder.addItem("Super Admin");

cmbPlaceholder.addItem("Executive");

}

private void init() {

setSize(new Dimension(500,400));

setVisible(true);

setLocationRelativeTo(null);

setTitle("Place Holder");

setResizable(false);

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

}

public static void main(String[] args) {

try{

UIManager.setLookAndFeel("com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel");

}

catch (Exception e) {

JOptionPane.showMessageDialog(null,e);

}

CreatePlaceHolderinJComboBox placeholder=new CreatePlaceHolderinJComboBox();

}

 

}