Showing posts with label jcombobox in java swing. Show all posts
Showing posts with label jcombobox in java swing. Show all posts

Monday, 8 April 2019

JComboBox in java |Online Help

JComboBox in java:

The JComboBox in java is a class which can be used to add a drop-down list of item to a container. The JComboBox is a combination of text and drop-down list. The JComboBox class has to constructors as follow:

JComboBox()

This constructor creates an empty JComboBox.

JComboBox(object[] items)

This constructor is used to create a JComboBox that contains the element in the specified array.

Methods for JComboBox in java:

The most commonly used JComboBox() method s are as follows:

void addItemListener(ItemListener Handler)

This method is used to configure an event handler for the combo box.

void setSelectedIndex(int selection)

The setSelectedIndex() method is used to set the index of the selcted item.

int getSelectedIndex()

The getSelectedIndex() method is used to written the index of the selected item.

void setFont(Font strfont)

The setFont() method is used to set the font for the JLabel.

void setBackground(Color bgcolor)

The setBackground() method is used to set the color of the background of the JLabel.

void setForeground(Color strcolor)

The setForeground() method is used to set the color of the string for the JLabel.

Sample Program for JCombo in java:

import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
/*<applet code="jcombobox" width=500 height=500></applet>*/
public class jcombobox extends JApplet
{
JComboBox jcb1=new JComboBox();
JTextField jtf;
public void init()
{
Container c=getContentPane();
jcb1.addItem("undergraduate");
jcb1.addItem("postgraduate");
jcb1.addItem("research");
c.setLayout(new FlowLayout());
c.add(jcb1);
}
}