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!
Related
I am trying to make a Swing GUI in Netbeans. I create a jcombobox and I bind it (using a query component, a list component and a renderer) to an entity called 'Item', so that the combobox shows the names of the items that currently exist in the table "Item" and so far it works fine. However, I need to add an "All Items" field to the combobox. Does anybody have any hints on where I should start?
Try
List<String> listItems = classDAO.findElement();
DefaultComboBoxModel<String> comboModel = new DefaultComboBoxModel();
for(String string : listItems)
{
comboModel.addElement(string);
}
comboModel.addElement("All items");
JComboBox<String> comboBox = new JComboBox<>(comboModel);
You can manually add an item to the combo box after the items from the table have been added to the combo box:
comboBox.addItemAt("All Items", 0);
will insert a new item at the top of the combo box.
Here's the thing:
I created a 'cocktailbar' software, and I have the following classes:
Cocktail,
CocktailBar,
CreateNewCPanel,
HelloPanel,
SearchCPanel,
ShowAllCPanel,
CocktailMixerGUI,
Ingredients.
Now: When adding a new Cocktail in the CreateNewCPanel, I add the cocktail to a List in the CocktailBar class.
Box buttonBox = Box.createHorizontalBox();
JButton speicherButton = new JButton("Speichern");
speicherButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e){
neuerC.setCName(cName.getText());
neuerC.fuegeZubereitungHinzu(zubereitungTextArea.getText());
CocktailBar.addCocktail(neuerC);
Now I need to see all created cocktails in a 'dropdown' menu in the ShowAllCPanel. I've got the following:
//Adding the DropDown Menu, first a Box, then a ComboBox inside.
Box cDropDownBox = Box.createHorizontalBox();
cDropDownBox.add(Box.createHorizontalGlue());
JComboBox cChoose = new JComboBox();
groesseEinsetzen(cChoose, 500, 20);
cChoose.setAlignmentX(SwingConstants.LEFT);
cDropDownBox.add(cChoose);
But now I am wondering how do I get my List from the CocktailBar class into the ShowAllCPanel?
edit: forgot to mention: i have a getter in the CocktailBar class, and i already tried:
cChoose.addItem(CocktailBar.getCocktails());
within the comboBox in the ShowAllCPanel, but it doesnt show up anything in the dropdown.
thanks to #Do Re, i inserted this:
//Adding the DropDown Menu, first a Box, then a ComboBox inside.
Box cDropDownBox = Box.createHorizontalBox();
cDropDownBox.add(Box.createHorizontalGlue());
JComboBox cChoose = new JComboBox();
if (CocktailBar.getCocktails() != null){
for (Cocktail c : CocktailBar.getCocktails())
cChoose.addItem(c);
}
but still - when running, the dropdown list stays empty.
As you mentioned in the comments, you created a getter for the cocktails.
Try something like this
for (Coctail c : CocktailBar.getCocktails())
cChoose.addItem(c);
This iterates over the list of cocktails and adds each item seperately rather than adding a list of Cocktails at once.
Edit
try
cDropDownBox.revalidate();
cDropDownBox.repaint();
or
cChoose.revalidate();
cChoose.repaint();
I am writing a Java program and I ran into a problem. I have an ArrayList<JCheckBox> and I want to show some dialog window with these checkboxes, so I can choose some of them and I want another ArrayList<> of the selected objects as an outcome after closing that dialog. I think I can get results by adding ActionListener to those checkboxes, but I donĀ“t know how to pass that ArrayList<JCheckBox> to the dialog window..
So far I tried something like this:
ArrayList<JCheckBox> al = new ArrayList<JCheckBox>();
for (MyClass mc : sr.getFields().values())
{
JCheckBox box = new JCheckBox(mc.getType());
al.add(box);
}
JOptionPane.showConfirmDialog(null, al);
If I try to print the text in the checkbox, it is ok, but the dialog shows only a long line of some text that does not make any sense..
So, is there a way how to do that?
Thanks in advance..
The showConfirmDialog method has to interpret the message object in order to render it correctly and it doesn't know how to interpret an ArrayList, you have to add all your elements to an JPanel eg:
JPanel al = new JPanel();
for (MyClass mc : sr.getFields().values()){
JCheckBox box = new JCheckBox(mc.getType());
al.add(box);
}
JOptionPane.showConfirmDialog(null, al);
or an Object[] eg:
ArrayList<JCheckBox> al = new ArrayList<JCheckBox>();
for (MyClass mc : sr.getFields().values()){
JCheckBox box = new JCheckBox(mc.getType());
al.add(box);
}
Object[] obj = (Object[]) al.toArray(new Object[al.size()]);
JOptionPane.showConfirmDialog(ui, obj);
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));
}
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