Results 1 to 6 of 6

Thread: Use of JComboBox class

  1. #1
    Join Date
    Dec 2009
    Posts
    67

    Use of JComboBox class

    Hi All,

    I need your help to get solution over my confusion. I am confused about the JComboBox class of java programming language. I am not getting how to use this 'JComboBox class' of java. I am not aware about it's constructors as well as it's method. If you have sound knowledge about the JComboBox class of java, then pleas try to share it with me. Your any help over JComboBox class would be greatly appreciable.

  2. #2
    Join Date
    May 2008
    Posts
    2,297

    Re: Use of JComboBox class

    A JComboBox class component is used to combine a button or editable field and a drop-down list. The user can select a value from the drop-down list, which appears at the user's request. If you make the combo box editable, then the combo box includes an editable field into which the user can type a value. A JComboBox class is originally taken from the 'javax.swing.JComponent' of java programming.

  3. #3
    Join Date
    Oct 2005
    Posts
    2,393

    Re: Use of JComboBox class

    Hi,

    If you want to aware about the use of JComboBox class of java, then please go through the following example of JComboBox class:
    Code:
    import javax.swing.JComboBox;
    import javax.swing.JTextArea;
    
    public class EditComboBoxDM {
      public static void main(String args[]) {
        String labels1[] = { "A", "B", "C", "D","E", "F", "G", "H","I", "J" };
        JFrame frame1 = new JFrame("Editable JComboBox");
        frame1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        Container contentPane = frame1.getContentPane();
    
        final JComboBox comboBox1 = new JComboBox(labels);
        comboBox1.setMaximumRowCount(5);
        comboBox1.setEditable(true);
        contentPane.add(comboBox1, BorderLayout.NORTH);
    
        final JTextArea textArea1 = new JTextArea();
        JScrollPane scrollPane = new JScrollPane(textArea1);
        contentPane.add(scrollPane, BorderLayout.CENTER);
    
        ActionListener actionListener = new ActionListener() {
          public void actionPerformed(ActionEvent actionEvent) {
            textArea1.append("Selected: " + comboBox1.getSelectedItem());
            textArea1.append(", Position: " + comboBox1.getSelectedIndex());
            textArea1.append(System.getProperty("line.separator"));
          }
        };
        comboBox1.addActionListener(actionListener);
    
        frame1.setSize(400, 200);
        frame1.setVisible(true);
      }
    }

  4. #4
    Join Date
    May 2008
    Posts
    2,389

    Re: Use of JComboBox class

    Hi friend,

    Please refer following methods of JComboBox class along with their different parameters:
    • setSelectedIndex(int Index1)
    • setRenderer(ListCellRenderer aRenderer1)
    • setPrototypeDisplayValue(Object prototypeDisplayValue1)
    • setPopupVisible(boolean bl1)
    • setModel(ComboBoxModel aModel1)
    • setMaximumRowCount(int cnt)

  5. #5
    Join Date
    Feb 2008
    Posts
    1,852

    Re: Use of JComboBox class

    Below I have given the example JComboBox class, please study it carefully:
    Code:
    import javax.swing.JComboBox;
    
    public class JFontComboBox extends JFrame implements ActionListener {
      JLabel fontLabel = new JLabel("The fox jumps over the lazy dog.");
    
      private JComboBox fontComboBox01;
    
      public FontComboBox() {
        setTitle("ComboBoxTest");
        setSize(300, 300);
        addWindowListener(new WindowAdapter() {
          public void windowClosing(WindowEvent e) {
            System.exit(0);
          }    });
    
        fontComboBox01 = new JComboBox();
        fontComboBox01.setEditable(true);
        fontComboBox01.addItem("Serif");
        fontComboBox01.addItem("DialogInput");
        fontComboBox01.addActionListener(this);
    
        JPanel p1 = new JPanel();
        p1.add(fontComboBox01);
        getContentPane().add(p1, "North");
        getContentPane().add(fontLabel, "Center");
      }
    
      public void actionPerformed(ActionEvent evt) {
        JComboBox source = (JComboBox) evt.getSource();
        String item = (String) source.getSelectedItem();
        fontLabel.setFont(new Font(item, Font.PLAIN, 12));
      }
    
      public static void main(String[] args) {
        JFrame frame1 = new JFontComboBox();
        frame1.show();
      }
    }

  6. #6
    Join Date
    Jan 2008
    Posts
    1,521

    Re: Use of JComboBox class

    Hello friend,

    If you want to know about the constructors of JComboBox class, then refer following:
    1. JComboBox(Vector itm) : It is used to for the creation of JComboBox with specified Vector.
    2. JComboBox(): It is used to for the creation of JComboBox with a default data model.
    3. JComboBox(Object[] itm): It is used to for the creation of JComboBox with specified arrar list.
    4. JComboBox(ComboBoxModel aMdl): It is used to for the creation of JComboBox with specified ComboBoxModel.

Similar Threads

  1. Replies: 8
    Last Post: 08-10-2011, 11:06 PM
  2. How to use JComboBox.AccessibleJComboBox class?
    By ScarFace 01 in forum Software Development
    Replies: 4
    Last Post: 18-02-2010, 09:59 AM
  3. Replies: 5
    Last Post: 12-02-2010, 06:23 PM
  4. How to initialize a jcombobox ?
    By Ron1 in forum Software Development
    Replies: 3
    Last Post: 03-04-2009, 11:07 PM
  5. Good news for CBSE CLASS X & CLASS IX - visit learnnext
    By surya_expert in forum Education Career and Job Discussions
    Replies: 0
    Last Post: 13-12-2008, 12:09 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,714,272,338.10738 seconds with 17 queries