Reputation: 937
I have a JComboBox
which I create with the following code:
employeeDeletetxt = new JComboBox(buildComboBoxmodel("SELECT employee_id, employee_first_name FROM employees"));
employeeDeletetxt.setSelectedItem(null);
Now, when I run the program the selection is set to null
so nothing is displayed in the combo box. I have an ActionListener
for a delete button. The ActionListener
would delete certain records and when this happens I need the data in the JComboBox
to reflect the recent changes. I use the following code:
employeeDeletetxt.removeItem(employeeDeletetxt.getSelectedItem());
employeeDeletetxt.setSelectedItem(null);
The problem is that the text displayed in the JComboBox
is not empty after these lines are called. However, the item which the JComboBox
points to is actually null
because I get the following error message when I call the ActionListener
directly after I have called it the previous time:
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
How can I set the text to be empty?
Upvotes: 2
Views: 1427
Reputation: 937
I found the solution to the problem and would like to share it with everyone in case someone else has the same problem. As I stated above, the AutoCompleteDecorator was what was causing the problem in the first place. If I removed it then the program would work as expected. However I wanted to provide the user with the ability to search. The solution is to use AutoCompletion instead which is found in the following link: http://www.orbital-computer.de/JComboBox/source/AutoCompletion.java. now, in the program, instead of using: AutoCompleteDecorator.decorate(employeeDeletetxt); I use: AutoCompletion.enable(employeeDeletetxt); This way I allow the user to use the autocomplete option while also being able to set the current selection of the combo box to null.
Upvotes: 2
Reputation: 375
I tried to implement what you described and got to this:
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
public class TestGui extends JFrame{
JPanel contentPane = new JPanel();
JButton button = new JButton("Press me!");
JComboBox comboBox = new JComboBox(new String[] {"None", "Help"});
public TestGui() {
initalise();
}
private void initalise() {
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
this.comboBox.setSelectedItem(null);
this.contentPane.setLayout(new GridLayout(2,1));
this.contentPane.add(comboBox);
this.contentPane.add(button);
this.button.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
comboBox.removeItem(comboBox.getSelectedItem());
comboBox.setSelectedItem(null);
}
});
this.setContentPane(this.contentPane);
this.pack();
this.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
new TestGui();
}
});
}
}
My program runs exactly the way you described. Maybe you can find your problem.
Upvotes: 1
Reputation: 59
The problem is that you are setting the current item to null which is different than setting it to an empty string. You can do something like:
employeeDeletetxt.setSelectedItem("").
What I would probably do is create a method that has a loop that creates an array of strings for your combobox and always have the first element be the empty string. That way, once the array is built, you add the rest of your elements retrieved from your query. If your query returns no results, you will just have the text box with the one blank element.
Upvotes: -1