GeeCON Prague 2014*
The moose likes Swing / AWT / SWT and the fly likes Couldn't add items to a JCombboBox at runtime[very...... urgent......] Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Couldn Watch "Couldn New topic
Author

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

netharam ram
Ranch Hand

Joined: Aug 09, 2001
Posts: 202
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.
Paul Stevens
Ranch Hand

Joined: May 17, 2001
Posts: 2823
Here is a simple example that adds to the box on the click of a button.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Couldn't add items to a JCombboBox at runtime[very...... urgent......]