Guest User

Untitled

a guest
Mar 21st, 2018
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.90 KB | None | 0 0
  1. DefaultComboBoxModel model = new DefaultComboBoxModel( yourStringArray );
  2. comboBox.setModel( model );
  3.  
  4. public class ComboPanel extends JPanel {
  5.  
  6. JComboBox jcbo;
  7. // this is constructor
  8. public ComboPanel(ArrayList<String> items) {
  9. jcbo = new JComboBox();
  10. // getting exiting combo box model
  11. DefaultComboBoxModel model = (DefaultComboBoxModel) jcbo.getModel();
  12. // removing old data
  13. model.removeAllElements();
  14.  
  15. for (String item : items) {
  16. model.addElement(item);
  17. }
  18.  
  19. // setting model with new data
  20. jcbo.setModel(model);
  21. // adding combobox to panel
  22. this.add(jcbo);
  23. }
  24. }
  25.  
  26. JComboBox op=new JComboBox(new String[] {"d","e","f"});
  27. op.removeAllItems();
  28. String[] new_entries=new String[] {"a","b","c"}
  29. for (String s : new_entries) {
  30. op.insertItemAt(s, op.getItemCount());
  31. }
  32. op.setSelectedIndex(0);
Add Comment
Please, Sign In to add comment