JComboBox in Java swing

By | April 20, 2012

This shows you how to add JcomboBox to JPanel

import java.awt.*;
import java.awt.event.*;
import java.text.DecimalFormat;

import javax.imageio.ImageIO;
import javax.swing.*;

public class Example implements WindowListener
{
  JFrame content_frame = new JFrame();
  JPanel panel = new JPanel();
  JComboBox Combonote;
  public Example()
  {
  	    content_frame.setTitle("Contents");
		content_frame.addWindowListener(this);
		content_frame.setSize(300, 300);
		content_frame.setVisible(true);
		content_frame.setLocationRelativeTo(null);
		content_frame.setResizable(false);    ///cannot maximize
		content_frame.setVisible(true);

		showComboBox();
  }
  private void showComboBox()
  {
	  String test[]= {"[select Exercise...]","Exercise1","Exercise2","Exercise3","Exercise4","Exercise5"};
	  panel.setBackground(Color.lightGray);
	  panel.setVisible(true);
	  Combonote = new JComboBox(test);
	  Combonote.setBounds(250, 240, 250, 30);
	  panel.add(Combonote);

	  panel.setBounds(250, 240, 250, 30);
	  content_frame.getContentPane().add(panel);
	  Combonote .addItemListener(new ItemListener()
	  {
	  	  public void itemStateChanged(ItemEvent ie)
	      {
	      	  if(ie.getStateChange() == ie.SELECTED)
	      	  {
	      		    if(Combonote.getSelectedItem().equals("Exercise1"))
	  		  		{
	                                          //code...
	                }
	          }
	      }
	  });
  }

  public static void main(String[] args)
  {
    new Example();
  }

@Override
public void windowActivated(WindowEvent arg0) {
	// TODO Auto-generated method stub

}

@Override
public void windowClosed(WindowEvent arg0) {
	// TODO Auto-generated method stub

}

@Override
public void windowClosing(WindowEvent arg0) {
	// TODO Auto-generated method stub

}

@Override
public void windowDeactivated(WindowEvent arg0) {
	// TODO Auto-generated method stub

}

@Override
public void windowDeiconified(WindowEvent arg0) {
	// TODO Auto-generated method stub

}

@Override
public void windowIconified(WindowEvent arg0) {
	// TODO Auto-generated method stub

}

@Override
public void windowOpened(WindowEvent arg0) {
	// TODO Auto-generated method stub

}
}


Website Pin Facebook Twitter Myspace Friendfeed Technorati del.icio.us Digg Google StumbleUpon Premium Responsive

Leave a Reply

Your email address will not be published. Required fields are marked *