I'm using JFrame GUI to drag and drop elements and I'm currently working on combo box. Below is the GUI.

enter image description here

When the Button "Press" is clicked, it will add numbers into the combo box as below:

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         
    for (int i = 0; i<10; i++) {
        String str_rep = Integer.toString(i);
        jComboBox1.addItem(str_rep);
    }
    jComboBox1.setSelectedItem(null);
 }

Once the numbers are added into the combo box, I set the selected item to null because I didn't want the combo box to display the first element once the button is pressed.

However, the issue I'm facing currently is that I coded the jLabel2 to display what is being selected based on the combo box, but once the "Press" button is pressed, the label automatically display the first number which is 0, even if I set the combo box selected item to null.

private void jComboBox1ActionPerformed(java.awt.event.ActionEvent evt) {
    jLabel2.setText(jComboBox1.getSelectedItem().toString());
}    

enter image description here

Is there any way I can prevent the label from displaying the first number once the button is pressed? It should remain as label until I select a number from the combo box, then it will be updated to that number.

1 Answers

0
Prasad Karunagoda On

In ComboBoxAndLabel class, I tried to recreate your problem.

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

public class ComboBoxAndLabel {

  public static void main(String[] args) {

    JLabel jLabel2 = new JLabel("jLabel2");

    JComboBox<String> jComboBox1 = new JComboBox<>();
    jComboBox1.addActionListener(new ActionListener() {
      @Override
      public void actionPerformed(ActionEvent e) {
        jLabel2.setText(jComboBox1.getSelectedItem().toString());
      }
    });

    JButton button = new JButton("Press");
    button.addActionListener(new ActionListener() {
      @Override
      public void actionPerformed(ActionEvent e) {
        for (int i = 0; i<10; i++) {
          String str_rep = Integer.toString(i);
          jComboBox1.addItem(str_rep);
        }
        jComboBox1.setSelectedItem(null);
      }
    });

    JFrame frame = new JFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.getContentPane().add(button, BorderLayout.NORTH);
    frame.getContentPane().add(jComboBox1, BorderLayout.CENTER);
    frame.getContentPane().add(jLabel2, BorderLayout.SOUTH);
    frame.setBounds(400, 300, 400, 300);
    frame.setVisible(true);
  }
}

When I run above program, I see below error in the console (I guess you also get this error).

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
  at ComboBoxAndLabel$1.actionPerformed(ComboBoxAndLabel.java:21)
  at javax.swing.JComboBox.fireActionEvent(JComboBox.java:1258)
  at javax.swing.JComboBox.setSelectedItem(JComboBox.java:586)
  at ComboBoxAndLabel$2.actionPerformed(ComboBoxAndLabel.java:35)
  at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2022)
  at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2348)
  at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402)
  at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)
  at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:252)

The problem here is, jComboBox1.addItem() method sets the selected item when the combo box is empty (See the source code of DefaultComboBoxModel.addElement()). That's why you have had to call jComboBox1.setSelectedItem(null).

But if you use jComboBox1.setModel() instead of adding items one by one, then selected item will NOT be set automatically. So, you don't need to call jComboBox1.setSelectedItem(null) as well. You can change jButton1ActionPerformed like this:

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
    DefaultComboBoxModel<String> comboBoxModel = new DefaultComboBoxModel<>();
    for (int i = 0; i<10; i++) {
      String str_rep = Integer.toString(i);
      comboBoxModel.addElement(str_rep);
    }
    jComboBox1.setModel(comboBoxModel);
}

Full executable solution:

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

public class ComboBoxAndLabelSolution {

  public static void main(String[] args) {

    JLabel jLabel2 = new JLabel("jLabel2");

    JComboBox<String> jComboBox1 = new JComboBox<>();
    jComboBox1.addActionListener(new ActionListener() {
      @Override
      public void actionPerformed(ActionEvent e) {
        jLabel2.setText(jComboBox1.getSelectedItem().toString());
      }
    });

    JButton button = new JButton("Press");
    button.addActionListener(new ActionListener() {
      @Override
      public void actionPerformed(ActionEvent e) {
        DefaultComboBoxModel<String> comboBoxModel = new DefaultComboBoxModel<>();
        for (int i = 0; i<10; i++) {
          String str_rep = Integer.toString(i);
          comboBoxModel.addElement(str_rep);
        }
        jComboBox1.setModel(comboBoxModel);
      }
    });

    JFrame frame = new JFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.getContentPane().add(button, BorderLayout.NORTH);
    frame.getContentPane().add(jComboBox1, BorderLayout.CENTER);
    frame.getContentPane().add(jLabel2, BorderLayout.SOUTH);
    frame.setBounds(400, 300, 400, 300);
    frame.setVisible(true);
  }
}