Part of my code
ArrayList<Item> i = g.getItems();
Vector itemsVector = new Vector(i);
JList items = new JList(iemsVector);
Later in the code I create new object which I want to add to JList. How can I do that?
Populate the JList with a DefaultListModel, not a vector, and have the model visible in the class. Then simply call addElement on the list model to add items to it.
Well you can not use directly that Array but use this this will might help you for the same.
DefaultListModel demoList = new DefaultListModel();
demoList.addElement("addElements");
JList listd = new JList(demoList);
That way you can add elemets into the LIST.
You may add it (new object) to the itemsVector (Vector). After adding an item into Vector object invoke the items.setListData(itemsVector); method.
Try with the add method, like this: items.add(newItem).
private javax.swing.JList<String> list1;
list1.setFont(new java.awt.Font("Tahoma", 0, 24));
DefaultListModel listModel1 = new DefaultListModel();
String st="Working hard";
listModel1.addElement(r);
list1.setModel(listModel1);
I'm using code similar to the following:
public void addRow(MyObject object)
{
Object[] objects = new Object[]{object.getSomeInt(), object.getSomeString()};
DefaultTableModel tableModel = (DefaultTableModel) table.getModel();
tableModel.addRow(objects);
}
Try this:
DefaultListModel model = new DefaultListModel();
JList list = new JList(model);
// Initialize the list with items
String[] items = { "A", "B", "C", "D" };
for (int i = 0; i < items.length; i++) {
model.add(i, items[i]);
}
source : java2s
Related
I have a JList which is of DefaultListModel. I am not trying to create a JComboBox which should display as its the elements, the elements in the JList.
What is the best way to achieve this ?
Thank you.
My code:
DefaultListModel<String> listModelTopic = new DefaultListModel<>();
//create the list
listTopic = new JList<>(listModelTopic);
//create comboBox
JComboBox comboBox = new JComboBox();
comboBox.setModel(new DefaultComboBoxModel(topicList.getModel());
Error: The constructor DefaultComboBoxModel(ListModel) is undefined
Use copyInto on DefaultListModel to copy all the values to an array.
String[] lstArray = new String[listModelTopic.getSize];
listModelTopic.copyInto(lstArray );
Then create DefaultComboBoxModel using this array.
DefaultComboBoxModel comboModel = new DefaultComboBoxModel(lstArray );
JComboBox comboBox = new JComboBox();
comboBox.setModel(comboModel );
Hope this helps!
I want to program a search function of the arraylist . Example I search for Simon, name that contains Simon should show up in the Jlist as shown below. The part I couldnt figure is what is the condition should I check for and what should I be adding.
Screenshot
Main
public static ArrayList al = new ArrayList();
al.add("Alica Wonderland");
al.add("Bob Jr");
al.add("Simon Tay");
al.add("Simon Corbell");
al.add("Simon Flyman");
al.add("Simon Jr");
al.add("David Copper");
Button Action
private void btnSearchActionPerformed(java.awt.event.ActionEvent evt) {
String value = txtSearch.getText();
listModel = new DefaultListModel();
JList ListAll = new JList(listModel);
if(al.contains(value)){
listModel.addElement(?Name that contains Simon?);
}
ListAll.setModel(listModel);
}
You could iterate over ArrayList elements:
for (Object s : al) {
if (((String)s).contains("Simon"))
listModel.addElement(s);
}
PS: You can avoid cast stuff by defining a type for ArrayList:
List<String> al = new ArrayList<String>();
Try this:
for(int i=0;i<al.size();i++)
if(al.get(i).contains(value))
listModel.addElement(al.get(i));
I think you mean something like this:
String name = "Simon";
for(int k = 0; k = yourArraylist.size() - 1; k++) {
if(yourArraylist.get(k).equals(name)) {.
your_Jlist.add(name);
//...
}
}
I binding an array to JComboBox like following:
String[] arr={"ab","cd","ef"};
final JComboBox lstA = new JComboBox(arr);
but I want bind array to JComboBox dynamically like following :
final JComboBox lstA = new JComboBox();
void bind()
{
String[] arr={"ab","cd","ef"};
// bind arr to lstA
}
How to do it?
A little odd workaround(mine :)), might useful to you
final JComboBox lstA = new JComboBox();
String[] arr={"ab","cd","ef"};
lstA.setModel(new JComboBox(arr).getModel());
build your JComboBox with a dynamic ComboBoxModel
JComboBox(ComboBoxModel<E> aModel)
like http://docs.oracle.com/javase/7/docs/api/javax/swing/DefaultComboBoxModel.html
m=new DefaultComboBoxModel();
j=JComboBox(m);
you can then add and remove elements:
m.addElement("ab")
m.addElement("cd")
or, if you only need to put the array in the combox:
new JComboBox(new Sring[]{"ab","cd","ef"})
final JComboBox lstA = new JComboBox();
void bind()
{
String[] arr={"ab","cd","ef"};
// bind arr to lstA
lstA.setModel(new DefaultComboBoxModel<String>(arr));
}
Is there a quickest way to shuffle the items of a DefaultListModel other than copying all elements in a separate arraylist, I'm using the code below, but it gives me a java.lang.ClassCastException :
....
private DefaultListModel<String> listModel = new DefaultListModel<String>();
...
Collections.shuffle((List<?>) listModel, new java.util.Random(seed));
...
Any suggestion
Not completely sure of the indices but something like this:
private DefaultListModel<String> listModel = new DefaultListModel<String>();
...
static void shuffle(DefaultListModel<String> mdl){
for(int i=0;i<mdl.size();i++){
int swapWith = (int)(Math.random()*(mdl.size()-i))+i;
if(swapWith==i) continue;
mdl.add(i, mdl.remove(swapWith));
mdl.add(swapWith, mdl.remove(i+1));
}
}
I need to populate a JComboBox with an ArrayList. Is there any way to do this?
Use the toArray() method of the ArrayList class and pass it into the constructor of the JComboBox
See the JavaDoc and tutorial for more info.
Elegant way to fill combo box with an array list :
List<String> ls = new ArrayList<String>();
jComboBox.setModel(new DefaultComboBoxModel<String>(ls.toArray(new String[0])));
I don't like the accepted answer or #fivetwentysix's comment regarding how to solve this. It gets at one method for doing this, but doesn't give the full solution to using toArray. You need to use toArray and give it an argument that's an array of the correct type and size so that you don't end up with an Object array. While an object array will work, I don't think it's best practice in a strongly typed language.
String[] array = arrayList.toArray(new String[arrayList.size()]);
JComboBox comboBox = new JComboBox(array);
Alternatively, you can also maintain strong typing by just using a for loop.
String[] array = new String[arrayList.size()];
for(int i = 0; i < array.length; i++) {
array[i] = arrayList.get(i);
}
JComboBox comboBox = new JComboBox(array);
DefaultComboBoxModel dml= new DefaultComboBoxModel();
for (int i = 0; i < <ArrayList>.size(); i++) {
dml.addElement(<ArrayList>.get(i).getField());
}
<ComboBoxName>.setModel(dml);
Understandable code.Edit<> with type as required.
I believe you can create a new Vector using your ArrayList and pass that to the JCombobox Constructor.
JComboBox<String> combobox = new JComboBox<String>(new Vector<String>(myArrayList));
my example is only strings though.
Check this simple code
import java.util.ArrayList;
import javax.swing.JComboBox;
import javax.swing.JFrame;
public class FirstFrame extends JFrame{
static JComboBox<ArrayList> mycombo;
FirstFrame()
{
this.setSize(600,500);
this.setTitle("My combo");
this.setLayout(null);
ArrayList<String> names=new ArrayList<String>();
names.add("jessy");
names.add("albert");
names.add("grace");
mycombo=new JComboBox(names.toArray());
mycombo.setBounds(60,32,200,50);
this.add(mycombo);
this.setVisible(true); // window visible
}
public static void main(String[] args) {
FirstFrame frame=new FirstFrame();
}
}
By combining existing answers (this one and this one) the proper type safe way to add an ArrayList to a JComboBox is the following:
private DefaultComboBoxModel<YourClass> getComboBoxModel(List<YourClass> yourClassList)
{
YourClass[] comboBoxModel = yourClassList.toArray(new YourClass[0]);
return new DefaultComboBoxModel<>(comboBoxModel);
}
In your GUI code you set the entire list into your JComboBox as follows:
DefaultComboBoxModel<YourClass> comboBoxModel = getComboBoxModel(yourClassList);
comboBox.setModel(comboBoxModel);
i think that is the solution
ArrayList<table> libel = new ArrayList<table>();
try {
SessionFactory sf = new Configuration().configure().buildSessionFactory();
Session s = sf.openSession();
s.beginTransaction();
String hql = "FROM table ";
org.hibernate.Query query = s.createQuery(hql);
libel= (ArrayList<table>) query.list();
Iterator it = libel.iterator();
while(it.hasNext()) {
table cat = (table) it.next();
cat.getLibCat();//table colonm getter
combobox.addItem(cat.getLibCat());
}
s.getTransaction().commit();
s.close();
sf.close();
} catch (Exception e) {
System.out.println("Exception in getSelectedData::"+e.getMessage());