Multiple JComboBoxes with same data, every item choosable once - java

I'm growing desperate here ...:
I do have a FRAME with 40 JComboBoxes. At first, they offer all the same items.
If I choose on item in one specific ComboBox, it should be selected there and not be available in all the other 39 Boxes anymore.
I tried to use a ComboBoxModel for a long while, but now I think it doesnt make any sense: the Moment i remove the selected item from the model, it gets removed from the Box that it got selected in, too.
So does it make sense to do it like this:
Have 40 MyComboBoxes in the class GUI
Every MyComboBox implements an ItemListener
If an item is selected, the item gets removed from the other 39 lists;
if its deselected, it gets added to the other 39 lists
(but if I want to do it like that, the listener mustnt be an own class but the
itemEventChanged-method must be implemented anonymously in the GUI?!)
There's a better way, isnt it? Thanks a lot for your help!

for(int x =0;x<YourComboBoxArray.length();x++
{
if(e.getSource()==YourComboBoxArray[x])
{
try
{
ArrayList <String> OptionsList = new ArrayList();
for(int i=0;i<YourComboBoxArray[x].getItemCount();i++)
{
OptionsList.add(TeamPlayercmbx[x].getItemAt(i).toString();
}
DefaultComboBoxModel DCMB = new DefaultComboBoxModel(OptionsList.toArray());
YourComboBoxArray[x+1].setModel(DCMB);
YourComboBoxArray[x+1].removeItem(YourComboBoxArray[x].getSelectedItem());
}
catch(Exception ex)
{
//Log your errors or whatever you want to do if it's the last ComboBox in the Array
}
}
}
This assumes you have an action listener on each index of the Combo Box Array

Related

How to Give Two Forms to JCombobox Items in Java Swing

i am stuck with a new problem, don't know if this works but here i have list of JCombobox as follow.
JCombobox comboBox = new JComboBox();
comboBox.addItem("UserName");
comboBox.addItem("Password");
comboBox.addItem("DLNo 20 b");
comboBox.addItem("DLNo 20 b");
i want to print my database column names which are more than 40!
when i select the Combobox it must internally print my custom item here.
Here i tried with this code but i am not satisfied with this
if(comboBox.getSelectedIndex()==0)
{
System.out.println("U_NAME");
}
if(comboBox.getSelectedIndex()==1)
{
System.out.println("P_NAME");
}
if(comboBox.getSelectedIndex()==2)
{
System.out.println("DL_NO_20_b");
}
if(comboBox.getSelectedIndex()==3)
{
System.out.println("DL_NO_20_b");
}
is there any better way to over come this, like mapping objects
You could create a class ComboBoxItem with a name- and a columnName-attribute.
Use instances of this class for the ComboBox.
In the ComboBoxItem-class, overwrite the toString()-method to return the name, so it gets displayed as wished in the ComboBox. Add a getColumnName()-method to return the columnName, so you could invoke getSelectedItem().getColumnName().

JavaFX - Cannot Refresh/Update ListView

Well it appears I have been stumped by one of the simplest ListView implementations out there. For a few days I have found it impossible to properly reallocate a JavaFX ListView. I am working on making an EntityView completely dynamic, being able to remove elements whenever needed through a ContextMenu. So I have a ListView that is populated by an ArrayList, which we will refer to as "renderable". When you select "Remove" in the context menu, it removes the Entity from the renderable list, which also happens to be the "value" of the List Cell on whom I right clicked. Afterwards, I wish to refresh the ListView and remove now the nonexistent cell. So by creating a new ObservableList with the new renderable list (which removes the correct entity and that works fine), I set the items in the ListView, which does jack shit. I can set the list to null in this case, which removes all the elements. But I cannot reset the list with the new array and it remove the now missing entity. Somebody point me in the right direction please!
When I use the method I stated above, it removes it from the list, but not visually. There becomes an unusable cell at the bottom of the list, which has its name.
public void createContextMenu(final Entity curr, MouseEvent me){
MenuItem[] items = {new MenuItem("EDIT TYPE"), new MenuItem("REMOVE")};
ContextMenu menu = new ContextMenu(items);
menu.show(list, me.getScreenX(), me.getScreenY());
items[1].setOnAction(new EventHandler<ActionEvent>(){
public void handle(ActionEvent arg0) {
CanvasTab tab = (CanvasTab) core.canvasTabbedPane.getSelectionModel().getSelectedItem();
Kanvas k = tab.canvas;
k.renderable.remove(curr);
System.out.println(k.renderable);
k.redraw();
EntityView.this.list.getItems().remove(curr);
ObservableList<Entity> temp = FXCollections.observableList(k.renderable);
EntityView.this.list.setItems(temp);
}
});
}
This is the context menu:

JList - selecting option

how do i go do this, based on input in textfield, you get some results inside jlist, after you select option in jlist you then get an action, code examples would be appreciated... this is what i got so far:
final DefaultListModel<String> locations = new DefaultListModel<String>();
getTextFieldSearch().addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
for(int i=0;i<10;++i) {
locations.add(i, "blah");
}
}
});
JList<String> list_racentRaces = new JList<String>(locations);
Start by taking a look at How to Use Lists, which has lots of awesome code examples.
The basic idea would be to...
When your actionPerformed method is triggered, create a new DefaultListModel, assuming you don't have your own implementation, fill it with all the new items you need and apply it to the instance of list_racentRaces
If you want to maintain what was previously in the list, you should consider starting with a DefaultListModel and simply add the new items to it as you need to...
Then, attach a ListSelectionListener to list_racentRaces and when the valueChanged event is triggered, find the selected item(s) and do what ever you need to based on these result(s)
You can find more details and examples through How to Write a List Selection Listener

How to Check(Marked as checked) buttons in a program(or a checkbox) from just one Button?

Here is my question.Lets say that somebody made a checkbox in java and he is using it in a GUI interface so that the user can select a variety of options.Then the programmer wants to create a button inside the checkbox so that when the user checks that button all the other options will be checked as well.And when he unchecks that button of course all the other buttons will be unchecked.How is that possible in java?
Example :
o1 = new JCheckBox("Option 1");
o2 = new JCheckBox("Option 2");
o3 = new JCheckBox("Option 3");
All = new JCheckBox("All");
.....
CheckBoxHandler listener = new CheckBoxHandler();
All.addItemListener(listener);
......
Lets assume that the following code is on a class that was created as it implements ItemListener
public class CheckBoxHandler implements ItemListener
{
public void itemStateChanged(ItemEvent e)
{
if (e.getSource()==All)
if (e.getStateChange()==ItemEvent.SELECTED)
{
.... <------ (!!!)Here inside I am currently stack and I do not know how to
add the correct code in order to have all the other buttons checked.
}
}
}
Any help provided is really appreciated :)
Make use of the concept of array.
JCheckBox[] checkboxes = new JCheckBox[10];
When you need to apply some operation to all checkboxes, iterate over the array:
for( JCheckbox cb : checkboxes )
cb.doStuff();
You can call setSelected() on the JCheckbox (inherited from AbstractButton):
...
o1.setSelected(true); // or false
...
As #Juvanis mentions, instead of having three different references for o1, o2 and o3, you should use an array. Then, you can do something like
for( JCheckbox cb : checkboxes ) {
cb.setSelected(true);
}
to set all checkboxes in the array as checked.
CheckBoxes can have ActionListeners. Why not add an ActionListener to all the checkboxes that then checks if the one selected is checked or not and calls setSelected(true) or setSelected(false) on each one?
If you have a known small number of checkboxes (such as the 3 you talked about), you may just want to hard code it. However, if you need to make it modular or have a large number of check boxes, you can always store them in a data structure (as Juvanis said, an array would probably work nicely) and loop it

Selection on one in an Array of JComboBoxes fires the action for all

I create an array of combo boxes in a for loop like so:
for(int i = 0; i < 5; i++) {
...
comboStudy[i] = new JComboBox(studyModel);
comboStudy[i].addActionListener(new studyListener());
comboStudy[i].setActionCommand("" + i);
...
}
The listener is an instance inner class:
public class studyListener implements ActionListener {
public void actionPerformed(ActionEvent evt) {
int i = Integer.parseInt(evt.getActionCommand());
// do some stuff that requires i and also access
// to the instance members of the containing class
}
}
The problem I now face is that whenever I make a selection at run time in comboStudy[0] the action event gets fired 5 times. The first time i is 4, decreasing each time until it gets to 0.
I also tried it using an ItemListener, but it has the same problem.
Please help!
This is because you are using the same ComboBoxModel in all of your JComboBoxes.
Each JComboBox is a listener of the ComboxBoxModel and the ComboBoxModel will notify each listener whenever there is a change to the data model. When you select an item in a JComboBox, the ComboBoxModel changes which in turn fires events to each JComboBox. This is why you see events occurring on each JComboBox.

Categories

Resources