Jump to content

  • Log In with Google      Sign In   
  • Create Account

We're offering banner ads on our site from just $5!

1. Details HERE. 2. GDNet+ Subscriptions HERE. 3. Ad upload HERE.


Help with adding an array of strings to a combobox (also lists as i've tried before).


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
3 replies to this topic

#1 Enerjak   Members   -  Reputation: 235

Like
0Likes
Like

Posted 31 October 2012 - 11:20 PM

Ok, well, my problem is this: I want to add the name of the items to a combo box based on how many items there are in the away.
now i tried this:

public boolean addTabs()
{
  String[] names;
  HardwareStock stock = store.getStock();
 
  HardwareItem Item = null;

  if(stock.listSize() >= 0)
  {
   for(int index = 0; index < stock.listSize(); index++)
   {
    Item = stock.elementAt(index);
  
    if(Item != null)
    {
	 this.itemSelection.setModel(new DefaultComboBoxModel(new String[]
	   {
	    "Pick an item",
	    store.getStock().elementAt(index).getItemName()
	   }));
    }
   }
  }
  if(Item != null)
  {
   return true;
  }
  else
  {
   return false;
  }
}

but sadlly it only added one item name after the <pick an item> where it should of added all 8 (size of the away so far....) any items? please ask me anything I know this seems vague, but my professor always says "if you need help, ask for it"

Sponsor:

#2 ppgamedev   Members   -  Reputation: 311

Like
1Likes
Like

Posted 01 November 2012 - 03:48 AM

You are replacing the ComboBoxModel with a new one every time you process an Item.

Try something like this:
[source lang="java"] public boolean addTabs() { HardwareStock stock = store.getStock(); itemSelection.setModel(createItemSelectionModel(stock)); return stock.listSize() > 0; } private ComboBoxModel<String> createItemSelectionModel(HardwareStock stock) { MutableComboBoxModel<String> model = new DefaultComboBoxModel<String>(); model.addElement("Pick an item"); for (int i = 0; i < stock.listSize(); i++) { model.addElement(stock.elementAt(i).getItemName()); } return model; }[/source]

#3 Enerjak   Members   -  Reputation: 235

Like
0Likes
Like

Posted 01 November 2012 - 07:36 AM

I honestly love you man thanks!!

#4 ppgamedev   Members   -  Reputation: 311

Like
1Likes
Like

Posted 01 November 2012 - 08:21 AM

You could walk the extra mile with a "renderer".

I honestly love you man thanks!!

But I warn you: I'm happily married Posted Image

[source lang="java"] private void createItemSelection() { itemSelection = new JComboBox<HardwareItem>(); itemSelection.setModel(createItemSelectionModel()); itemSelection.setRenderer(createItemSelectionRenderer()); } public boolean addTabs() { HardwareStock stock = store.getStock(); itemSelection.removeAllItems(); itemSelection.addItem(null); for (int i = 0; i < stock.listSize(); i++) { itemSelection.addItem(stock.elementAt(i)); } return stock.listSize() > 0; } @SuppressWarnings("unchecked") private ListCellRenderer<HardwareItem> createItemSelectionRenderer() { return new BasicComboBoxRenderer() { private static final long serialVersionUID = 1L; @Override public Component getListCellRendererComponent( @SuppressWarnings("rawtypes") JList list, Object value, int index, boolean isSelected, boolean cellHasFocus ) { Component rendererComponent = super.getListCellRendererComponent( list, value, index, isSelected, cellHasFocus); HardwareItem item = (HardwareItem) value; String text = (item == null) ? "Pick an item" : item.getItemName(); setText(text); return rendererComponent; } }; } private ComboBoxModel<HardwareItem> createItemSelectionModel() { MutableComboBoxModel<HardwareItem> model = new DefaultComboBoxModel<HardwareItem>(); model.addElement(null); return model; }[/source]

For some reason I cannot properly visualise the code with my browser Posted Image

Edited by ppgamedev, 01 November 2012 - 08:34 AM.





Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS