1
private String stringComboBoxElement[] = {"Roll", "SetCode", "Answers", "Others"};

contentPane = new JPanel();

contentPane.setLayout(null);

comboBoxfields = new JComboBox(stringComboBoxElement);

    comboBoxfields.setBounds(180, 50, 120, 30);
    comboBoxfields.setFont(new Font("sansserif", Font.TRUETYPE_FONT | Font.PLAIN, 15));
    comboBoxfields.setBorder(new SoftBevelBorder(BevelBorder.LOWERED));

    if(stringComboBoxElement.length > 5)
        comboBoxfields.setMaximumRowCount(5);
    else
        comboBoxfields.setMaximumRowCount(stringComboBoxElement.length);

    comboBoxfields.addItemListener(new ItemListener() {

        @Override
        public void itemStateChanged(ItemEvent e) {
            if(e.getStateChange() == ItemEvent.SELECTED)
                textFieldName.setText((String) e.getItem());
        }
    });
    contentPane.add(comboBoxfields);

i did everything but the JComboBox doesn't appear in the window . What's the problem and how can i fix it ? To upload the full code isn't possible. The problem lies here without it everything is working properly. Can anyone help me please?

mKorbel
  • 109,525
  • 20
  • 134
  • 319
  • 3
    1st error)Using absolute position with null layout `contentPane.setLayout(null);` it's discouraged, apps have to work in different resolutions, 2nd) You are not adding your `contentPane` in any Frame – nachokk Mar 22 '14 at 18:23
  • Java GUIs might have to work on a number of platforms, on different screen resolutions & using different PLAFs. As such they are not conducive to exact placement of components. To organize the components for a robust GUI, instead use layout managers, or [combinations of them](http://stackoverflow.com/a/5630271/418556), along with layout padding & borders for [white space](http://stackoverflow.com/q/17874717/418556). – Andrew Thompson Mar 23 '14 at 00:53

1 Answers1

2

The following code works for me:

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.*;

public class Example {
    private static final String COMBO_BOX_ELEMENTS[] = {"Roll", "SetCode", "Answers", "Others"};

    public static void main(String[] args) {
      JFrame window = new JFrame("Example");

      JPanel contentPane = new JPanel();

      contentPane.setLayout(new GridBagLayout());

      final JComboBox comboBoxfields = new JComboBox(COMBO_BOX_ELEMENTS);

      comboBoxfields.setFont(new Font("sansserif", Font.TRUETYPE_FONT | Font.PLAIN, 15));
      comboBoxfields.setBorder(new SoftBevelBorder(BevelBorder.LOWERED));

      comboBoxfields.setMaximumRowCount(5);

      comboBoxfields.addActionListener(new ActionListener() {
          public void actionPerformed(ActionEvent actionEvent) {
                System.out.println(
                    "'"  + comboBoxfields.getSelectedItem().toString() + "'"
                    + " was selected");
          }
      });

      contentPane.add(comboBoxfields);

      window.add(contentPane); // I guess this is what you need to do.
      window.setSize(500, 500);
      window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      window.setVisible(true);
    }
}

As mentioned by @nachokk you need to add contentPane to a JFrame.

Benjamin Albert
  • 738
  • 1
  • 7
  • 19