Reputation:
I've created and populated a combobox by querying a database. When I have performed a few other functions in the program, I want to reset the combobox to the original value, i.e. a blank space. When I do this, I get an exception being thrown by the program.
The code is as follows:
to create the combo box:
tableNumberJComboBox = new JComboBox();
tableNumberJComboBox.setBounds( 168, 26, 80, 20 );
tableNumberJComboBox.setFont(new Font("Serif", Font.PLAIN, 12));
tableNumberJComboBox.addItem("");
waiterJPanel.add(tableNumberJComboBox);
and then to populate it:
private void loadTableNumbers()
{
try
{
myStatement = null;
myResultSet = null;
myStatement = myConnection.createStatement();
myResultSet = myStatement.executeQuery("SELECT tableNumber FROM restauranttables");
while (myResultSet.next())
{
tableNumberJComboBox.addItem(myResultSet.getInt(1));
}
myResultSet.close();
}
catch(SQLException sqlexception)
{
sqlexception.printStackTrace();
}
} // end method loadTableNumbers
and then to reset the table number to a blank space:
tableNumberJComboBox.setSelectedItem("");
and the exception is:
Exception in thread "AWT-EventQueue-0" java.lang.ClassCastException: java.lang.String cannot be cast to java.lang.Integer
I've tried setSelectedItem(0), but this didnt work. How do I set this back to the original white space?
Thanks in advance.
Upvotes: 0
Views: 6403
Reputation: 1
I faced the same problem, you could use:
tableNumberJComboBox.getSelectionModel().select(-1);
In my case (JavaFX), it helped!!! Hope it will help you too...
Upvotes: 0
Reputation: 324118
Don't create a new JComboBox, just use the existing combo box an reload the data:
Maybe something like:
tableNumberJComboBox.removeAllItems();
loadTableNumbers();
Edit:
The above suggestion works fine for me:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class SSCCE extends JPanel
{
private JComboBox<Integer> comboBox = new JComboBox<Integer>();
private int value = 10;
public SSCCE()
{
add( comboBox );
addItems();
JButton reset = new JButton( "Reset" );
reset.addActionListener( new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
comboBox.removeAllItems();
addItems();
}
});
add( reset );
}
private void addItems()
{
for (int i = 0; i < 5; i++)
comboBox.addItem( new Integer(value++) );
}
private static void createAndShowUI()
{
JLabel label = new JLabel(new ImageIcon("mong.jpg"));
JFrame frame = new JFrame("SSCCE");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add( new SSCCE() );
frame.setLocationByPlatform( true );
frame.pack();
frame.setVisible( true );
}
public static void main(String[] args)
{
EventQueue.invokeLater(new Runnable()
{
public void run()
{
createAndShowUI();
}
});
}
}
If this doesn't help then post a proper SSCCE of your own. And we don't have access to your database so you SSCCE should not include the database logic.
Upvotes: 0
Reputation: 2144
You could use tableNumberJComboBox.setSelectedItem(-1);
, if white space means that you did not selected anything. Is that what you wish to do?
Upvotes: 1