How to Read From Combobox in Java
Java Swing | JComboBox with examples
JComboBox is a part of Coffee Swing package. JComboBox inherits JComponent class . JComboBox shows a popup carte du jour that shows a list and the user can select a option from that specified list . JComboBox can exist editable or read- just depending on the choice of the programmer .
Constructor of the JComboBox are:
- JComboBox() : creates a new empty JComboBox .
- JComboBox(ComboBoxModel Chiliad) : creates a new JComboBox with items from specified ComboBoxModel
- JComboBox(E [ ] i) : creates a new JComboBox with items from specified array.
- JComboBox(Vector items) : creates a new JComboBox with items from the specified vector
Commonly used Methods are :
- addItem(East item) : adds the item to the JComboBox
- addItemListener( ItemListener l) : adds a ItemListener to JComboBox
- getItemAt(int i) : returns the item at index i
- getItemCount(): returns the number of items from the list
- getSelectedItem() : returns the item which is selected
- removeItemAt(int i) : removes the element at index i
- setEditable(boolean b) : the boolean b determines whether the combo box is editable or not .If true is passed so the combo box is editable or vice versa.
- setSelectedIndex(int i): selects the element of JComboBox at alphabetize i.
- showPopup() :causes the combo box to display its popup window.
- setUI(ComboBoxUI ui): sets the L&F object that renders this component.
- setSelectedItem(Object a): sets the selected item in the combo box display area to the object in the argument.
- setSelectedIndex(int a): selects the item at index anIndex.
- setPopupVisible(boolean v): sets the visibility of the popup.
- setModel(ComboBoxModel a) : sets the data model that the JComboBox uses to obtain the list of items.
- setMaximumRowCount(int count): sets the maximum number of rows the JComboBox displays.
- setEnabled(boolean b): enables the combo box so that items can be selected.
- removeItem(Object anObject) : removes an particular from the particular listing.
- removeAllItems(): removes all items from the item listing.
- removeActionListener(ActionListener l): removes an ActionListener.
- isPopupVisible() : determines the visibility of the popup.
- addPopupMenuListener(PopupMenuListener 50) : adds a PopupMenu listener which will listen to notification messages from the popup portion of the philharmonic box.
- getActionCommand() : returns the action control that is included in the event sent to action listeners.
- getEditor(): returns the editor used to paint and edit the selected item in the JComboBox field.
- getItemCount() : returns the number of items in the list.
- getItemListeners(): returns an assortment of all the ItemListeners added to this JComboBox with addItemListener().
- createDefaultKeySelectionManager() : returns an example of the default key-selection manager.
- fireItemStateChanged(ItemEvent e) : notifies all listeners that have registered interest for notification on this event type.
- firePopupMenuCanceled() : notifies PopupMenuListeners that the popup portion of the philharmonic box has been canceled.
- firePopupMenuWillBecomeInvisible() : notifies PopupMenuListeners that the popup portion of the philharmonic box has become invisible.
- firePopupMenuWillBecomeVisible() : notifies PopupMenuListeners that the popup portion of the combo box will become visible.
- setEditor(ComboBoxEditor a): sets the editor used to paint and edit the selected item in the JComboBox field.
- setActionCommand(String a) : sets the action command that should be included in the outcome sent to actionListeners.
- getUI() : returns the look and experience object that renders this component.
- paramString() : returns a cord representation of this JComboBox.
- getUIClassID() : returns the name of the Look and feel class that renders this component.
- getAccessibleContext() : gets the AccessibleContext associated with this JComboBox
The following programs will illustrate the use of JComboBox
1. Program to create a uncomplicated JComboBox and add elements to information technology .
Java
import java.awt.event.*;
import coffee.awt.*;
import javax.swing.*;
class solve extends JFrame implements ItemListener {
static JFrame f;
static JLabel 50, l1;
static JComboBox c1;
public static void main(String[] args)
{
f = new JFrame( "frame" );
solve s = new solve();
f.setLayout( new FlowLayout());
String s1[] = { "Jalpaiguri" , "Mumbai" , "Noida" , "Kolkata" , "New Delhi" };
c1 = new JComboBox(s1);
c1.addItemListener(south);
50 = new JLabel( "select your city " );
l1 = new JLabel( "Jalpaiguri selected" );
fifty.setForeground(Color.ruby-red);
l1.setForeground(Color.blue);
JPanel p = new JPanel();
p.add(fifty);
p.add(c1);
p.add together(l1);
f.add(p);
f.setSize( 400 , 300 );
f.evidence();
}
public void itemStateChanged(ItemEvent e)
{
if (e.getSource() == c1) {
l1.setText(c1.getSelectedItem() + " selected" );
}
}
}
Output :
2. Program to create two checkbox one editable and other read only
Java
import java.awt.event.*;
import java.awt.*;
import javax.swing.*;
class solve extends JFrame implements ItemListener {
static JFrame f;
static JLabel fifty, l1, l3, l4;
static JComboBox c1, c2;
public static void principal(String[] args)
{
f = new JFrame( "frame" );
solve due south = new solve();
String s1[] = { "Jalpaiguri" , "Mumbai" , "Noida" , "Kolkata" , "New Delhi" };
Cord s2[] = { "male person" , "female person" , "others" };
c1 = new JComboBox(s1);
c2 = new JComboBox(s2);
c1.setSelectedIndex( three );
c2.setSelectedIndex( 0 );
c1.addItemListener(south);
c2.addItemListener(due south);
c1.setEditable( true );
l = new JLabel( "select your city " );
l1 = new JLabel( "Jalpaiguri selected" );
l3 = new JLabel( "select your gender " );
l4 = new JLabel( "Male person selected" );
l.setForeground(Color.red);
l1.setForeground(Color.blue);
l3.setForeground(Color.red);
l4.setForeground(Color.blueish);
JPanel p = new JPanel();
p.add(fifty);
p.add(c1);
p.add(l1);
p.add together(l3);
p.add(c2);
p.add(l4);
p.setLayout( new FlowLayout());
f.add together(p);
f.setSize( 400 , 400 );
f.show();
}
public void itemStateChanged(ItemEvent e)
{
if (e.getSource() == c1) {
l1.setText(c1.getSelectedItem() + " selected" );
}
else
l4.setText(c2.getSelectedItem() + " selected" );
}
}
Output :
3. Plan to create a checkbox and add or remove items from it .
Java
import java.awt.event.*;
import java.awt.*;
import javax.swing.*;
class solve11 extends JFrame implements ItemListener, ActionListener {
static JFrame f;
static JLabel l, l1;
static JComboBox c1;
static JTextField tf;
public static void main(String[] args)
{
f = new JFrame( "frame" );
solve11 due south = new solve11();
f.setLayout( new FlowLayout());
Cord s1[] = { "Jalpaiguri" , "Mumbai" , "Noida" , "Kolkata" , "New Delhi" };
c1 = new JComboBox(s1);
tf = new JTextField( 16 );
JButton b = new JButton( "Add" );
JButton b1 = new JButton( "REMOVE" );
b.addActionListener(s);
b1.addActionListener(s);
c1.addItemListener(south);
l = new JLabel( "select your city " );
l1 = new JLabel( "Jalpaiguri selected" );
l.setForeground(Color.blood-red);
l1.setForeground(Colour.blue);
JPanel p = new JPanel();
p.add(l);
p.add together(c1);
p.add together(l1);
p.add(tf);
p.add together(b);
p.add(b1);
f.setLayout( new FlowLayout());
f.add together(p);
f.setSize( 700 , 200 );
f.bear witness();
}
public void actionPerformed(ActionEvent due east)
{
Cord due south = eastward.getActionCommand();
if (s.equals( "Add together" )) {
c1.addItem(tf.getText());
}
else {
c1.removeItem(tf.getText());
}
}
public void itemStateChanged(ItemEvent east)
{
if (e.getSource() == c1) {
l1.setText(c1.getSelectedItem() + " selected" );
}
}
}
Output :
Annotation : the higher up programs might not run in an online compiler please use an offline IDE
Source: https://www.geeksforgeeks.org/java-swing-jcombobox-examples/
0 Response to "How to Read From Combobox in Java"
Post a Comment