• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Devaka Cooray
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Knute Snortum
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Ron McLeod
  • Piet Souris
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Frits Walraven
  • Ganesh Patekar

Couldn't add items to a JCombboBox at runtime[very...... urgent......]

 
Ranch Hand
Posts: 202
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, I have come with this query again for it is very urgent.I have a problem adding items to a JComboBox at runtime.I receive an error:
"Exception in thread 'main' java.lang.RuntimeException: cannot use this methos with a noin-mutable data model.
My code is :
*******************
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.io.*;
import javax.swing.border.*;
class NewLstModel extends AbstractListModel
{
String values[];
public NewLstModel()
{
// System.out.println("this is the constructor");
try
{
File[] fl=File.listRoots();
values=new String[fl.length+5];
values[0]="Document Folders";
values[1]=" My Documents";
values[2]=" Desktop";
values[3]="My Computer";
values[4]=" All Local HardDrives";
System.out.println("Drives: "+fl.length);
int j=5;
for(int i=0;i<fl.length;i++)
{
String temp=fl[i].toString();
//if(fl[i].isFile())
if(temp.equalsIgnoreCase("A:\\"))
temp=" 3.5 Floppy "+temp;
else
temp=" Local Drive "+temp;
values[j]=temp;
System.out.println("Value: "+values[j]);
j++;
}
}catch(Exception e){System.out.println(e);}
}//end of constructor;
public Object getElementAt(int index)
{
return values[index];
}
public int getSize()
{
return values.length;
}
}//end of class NewLstModel.
class NewComboModel extends NewLstModel implements ComboBoxModel
{
Object item;
public void setSelectedItem(Object itm)
{
item= itm;
}
public Object getSelectedItem()
{
return item;
}
}//end of class newComboModel.
public class LstCom extends JPanel implements ActionListener
{
JComboBox c1;
public LstCom()
{
setBackground(Color.red);
JList l1=new JList(new NewLstModel());
l1.setVisibleRowCount(7);
JScrollPane jpane=new JScrollPane(l1);
add(jpane);
NewComboModel ncm=new NewComboModel();
c1=new JComboBox(ncm);
add(c1);
c1.setEditable(true);
c1.addActionListener(this);
c1.setMaximumRowCount(13);
c1.setBackground(new Color(453045));
c1.setForeground(Color.black);
c1.addItem("New Item");
//c1.setSelectedBackground(Color.blue);
System.out.println(c1.isPopupVisible());
c1.setPopupVisible(true);
//c1.checkMutableComboBoxModel();
int cnt=c1.getItemCount();
c1.setModel(ncm);
System.out.println(c1.getModel());
//c1.configureEditor(c1.getEditor(),"Edit_four");
//c1.insertItemAt("Four",cnt+1);
/*try
{
File[] fl=File.listRoots();
for(int i=0;i<fl.length;i++)
System.out.println(fl[i]);
// c1.addItem("Four");
}catch(Exception e){System.out.println(e+ "error");}*/
//System.out.println(c1.getActionCommand());
}
public void actionPerformed(ActionEvent ae)
{
Object it=c1.getSelectedItem();
c1.setPopupVisible(true);
int l=c1.getSelectedIndex();
//System.out.println(it);
ComboBoxEditor cbe;
cbe=c1.getEditor();
//System.out.println("Editor: "+cbe);
System.out.println("item: "+cbe.getItem());
cbe.setItem(it);
//c1.setEditable(false);
//c1.setSelectedIndex(l+1);
// System.out.println(c1.getActionCommand());
}
public static void main(String args[])
{
JFrame jf1=new JFrame("ComboList Models");
LstCom b1= new LstCom();
jf1.getContentPane().add("Center",b1);
jf1.getContentPane().setBackground(Color.red);
jf1.setSize(200,300);
jf1.setVisible(true);
jf1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}//end of LstCom().
End Of Code:
*************
Is there any error in this code to receive an error.I request Nathan to look into this post immediately to get me solved with this problem.If u could suggest me with a simpler code please do so immediately with the code.
Happy middling with java.
Netharam.
 
Ranch Hand
Posts: 1492
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Netharam,
The error is telling you that you must implement a mutable (i.e., changeable) model instead of a fixed one like you have done. Your ComboModel should look like the one below and you will be good to go.

You will need to import java.util.* to get it to compile but then it works great!
Regards,
Manfred.
 
Bartender
Posts: 4121
IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
netharam ram,

Please don't post multiple times... it makes it harder for everyone to keep up with the thread. I am closing this one. I think this problem has been solved, but if anyone wants to post to this thread, please post it to this one instead.

-Nate
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!