I have two combo box the items first one is (women and men).I want when user select women in first combo box the list of women's dress will appear in second combo box and when men is selected the list of men's dress will appear in second one.Can do this functionality by using JCombo box? if yes how can I do that give me example please.
any help will be appreciated.
Check out how to work with models in How to Use Combo Boxes and How to Use Lists totorials. According to a selection in the first combo box - rebuild, filter or perhaps replace the model of the second combo box. You can use/extend DefaultComboBoxModel - a default model used by a JComboBox. For example consider this snippet:
final JComboBox genderComboBox = null;
final JComboBox itemComboBox = null;
final DefaultComboBoxModel hisModel = new DefaultComboBoxModel(new String[]{"a", "b", "c"});
final DefaultComboBoxModel herModel = new DefaultComboBoxModel(new String[]{"x", "y", "z"});
genderComboBox.addActionListener (new ActionListener () {
public void actionPerformed(ActionEvent e) {
if ("Men".equals(genderComboBox.getSelectedItem())){
itemComboBox.setModel(hisModel);
} else {
itemComboBox.setModel(herModel);
}
}
});
Alternatively, upon selection in the first combo you can rebuild the items in the second one manually, ie: using JComboBox methods removeAllItems() and addItem().
You have to add event listener to the first combobox. This way you will know when its selection changes, you can interrogate it and fill your second combobox out with appropriate data.
More information is at http://docs.oracle.com/javase/tutorial/uiswing/components/combobox.html#listeners
Related
I have a JTable and in one column I have a JComboBox for each of the rows. I am dynamically adding rows when I press a button. The selection made in the combobox will determine what calculation is carried out for that particular row. For arguments sake lets say that the options for the combobox are: option 1, option 2, option 3 and option 4.
The issue I am having is as follows:
Say I have added 2 rows and select any option from the combobox for row 1, when I go to make a selection in the combobox for row 2 the same selection is ticked as was made for row 1. There seems to be some kind of memory. How can I disable this, so that the default selection is always -1 (i.e. non of the options selected)? I would like to have complete control over this.
Here is an example snippet of code just considering option 1:
String labels[] = {"Option 1", "Option 2", "Option 3", "Option4"};
JComboBox comboBox = new JComboBox(labels);
comboBox.setSelectedIndex(-1);
ItemListener itemListener = new ItemListener() {
public void itemStateChanged(ItemEvent itemEvent) {
int state = itemEvent.getStateChange();
ItemSelectable is = itemEvent.getItemSelectable();
if (selectedString(is) == "Option 1" & state == ItemEvent.SELECTED){
System.out.println("A");
}
}
};
comboBox.addItemListener(itemListener);
Thanks very much for your time and help :)
First of all don't use "==" when comparing strings. Instead you should be using the equals(...) method:
if (someString.equals(anotherString))
// do something
However, that is not the cause of the problem.
You are using the JComboBox incorrectly for a JTable. You should NOT be using a ItemListener (or any listener).
The combo box is just used as an editor for the table. That means when you select a value from the combo box, the TableModel of the table is updated. So if you have custom logic based on the selected value you need to override the setValueAt(...) method of your TableModel.
#Override
public void setValueAt(Object value, int row, int column)
{
super.setValueAt(value, row, column);
// add your custom logic here
}
How can I disable this, so that the default selection is always -1
The value displayed in the combo box is taken from the TableModel. So if you set the default value to be null the combo box will not have a selection when you start editing.
Read the section from the Swing tutorial on How to Use Tables for more information and working examples. Keep the tutorial link handy for future reference on Swing basics.
I've got two JComboBox. First one has model with elements "one", "two", "three" and second has model with elements "three", "four". And when user choose element "three" in first JComboBox, he can't choose element "three" in second one JComboBox. And in reverse - when user choose element "three" in second one JComboBox, he can't choose same element in first one CB. How can i do this in Java ?
Thank you for answers.
One more question. When i dynamically create ComboBoxes (when someone clicked the button - in button actionListener), and every new ComboBox has same model - same list of elements. How can i check this case ? Same like Blip said ?
In my opinion you could add ItemListener to the 2 JComboBox. If the user selects the object "three" in any of the JComboBox then the object "three" could be removed from the other JComboBox's model. And if the user deselects the object "three" in one JComboBox then the object "three" can be added to the model of the other JComboBox.
You could implement in the following way:
Lets have have the two JComboBox stored in variables box1 and box2 which could be implemented as :
JComboBox<String> box1 = new JComboBox<>(new String[]{"one", "two", "three"});
JComboBox<String> box2 = new JComboBox<>(new String[]{"three", "four"});
Now add itemListener to both these JComboBox and pass it to a method say boxItemSelected:
box1.addItemListener(new ItemListener(){
itemStateChanged(ItemEvent e){
boxItemSelected(e);
}
});
box2.addItemListener(new ItemListener(){
itemStateChanged(ItemEvent e){
boxItemSelected(e);
}
});
Now to implement the boxItemSelected(e)
void boxItemSelected(ItemEvent e){
//Check the item selected/deselected is "three" else do nothing.
if (e.getItem().equals("three")){
//Find the box on which this action was not performed to change its model.
JComboBox<String> oppositeBox;
if(e.getSource().equals(box1)){
oppositeBox = box2;
}else{
oppositeBox = box1;
}
//Check the item is selected or deselected to remove or add "three" to item list.
if(e.getStateChange() == ItemEvent.SELECTED){
oppositeBox.removeItem("three");
}else{
oppositeBox.addItem("three");
}
}
}
Addition Information
Here if you have more than 1 items that are overlapping and can be selected only in one JComboBox, a slight modification of the method boxItemSelected could be take care of your problem as Illustrate below:
Change the line in the above code from:
if (e.getItem().equals("three"))
to
if (e.getItem().equals("three") || e.getItem().equals("<new item>") || ....)
And change
oppositeBox.removeItem("three");
oppositeBox.addItem("three");
to
oppositeBox.removeItem(e.getItem());
oppositeBox.addItem(e.getItem());
Here under any circumstance the user cannot select the same items in both the JComboBox. And all these happen behind the scene without the knowledge of the user using the user-interface.
When i dynamically create ComboBoxes (when someone clicked the button
- in button actionListener), and every new ComboBox has same model - same list of elements. How can i check this case ? Same like Blip said
?
In response to the above I am considering that all the items in all the JComboBox same and are stored in a List variable, and if 1 item is selected in any one of the JComboBox then that item cannot be selected in rest of the JComboBox. If my assumption is correct, then I suggest you do the following:
Create a List<JComboBox<?>> say boxes and initialise it as below:
List<JComboBox<?>> boxes = new ArrayList<>();
In your JButton (the button that dynamically JComboBox) variable's ActionListener implementation create a variable say items as an instance of List to store the items of the JComboBox and add the items
List<String> items = new Vector<>();
items.add("One");
items.add("Two");
......
Now remove the items from from the items variable that are selected in other JComboBox that are dynamically generated:
Iterator<JComboBox<?>> iterator = boxes.iterator();
while(iterator.hasNext()){
JComboBox<?> existing = iterator.next();
items.remove(existing.getSelectedItem());
}
Now after initialisation of the JComboBox instance say box set the Model of the box to the previously initialised and trimmed List variable items
box.setModel(new DefaultComboBoxModel(items));
now add the JComboBox variable box to the List variable boxes:
boxes.add(box);
Also in this above mentioned ActionListener implementation add the ItemListener to box, the newly instantiated variable of JComboBox and pass it to the method boxItemSelected:
box.addItemListener(new ItemListener(){
itemStateChanged(ItemEvent e){
boxItemSelected(e);
}
});
Now the implementation of the boxItemSelected has to be changed to accommodate the changes:
void boxItemSelected(ItemEvent e){
//Create an iterator to iterate over the boxes
Iterator<JComboBox<?>> iterator = boxes.iterator();
while(iterator.hasNext()){
//Get the current instance of comboBox from the list
JComboBox<?> current = iterator.next();
//If the box in which the select or de-select
//event has occurred is the current comboBox then do nothing.
if(e.getSource().equals(current)(
continue;
}
//If the event is select then remove the Item from the
//current comboBox else add the Item to the current comboBox.
if(e.getStateChange() == ItemEvent.SELECTED){
current.removeItem(e.getItem());
}else{
current.addItem(e.getItem());
}
}
}
So this is definitely possible. The best way I think would be to check the selected index of both boxes and then perform your logic based on the results of that index value. In the example below you can see that demonstrated. What is happening is there is an ActionListener on ComboBox1 and 2. If the values match, I used index but you can also get the string value to make sure they don't match up.
public class SOF extends JFrame {
private JPanel mainPanel, comboPanel;
private JComboBox jcb1, jcb2;
public SOF()
{
super("Combo Box Example");
mainPanel = new JPanel();
mainPanel.add(configureCombo());
add(mainPanel);
setSize(200,200);
setLocationRelativeTo(null);
setVisible(true);
jcb1.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e)
{
//You can replace this with, jcb2.getSelectedItem if you don't know the indexes or if they will be random.
if((jcb1.getSelectedIndex() == 2) && (jcb2.getSelectedIndex() == 0))
{
JOptionPane.showMessageDialog(null, "Cannot select 3 in both field.");
jcb1.setSelectedIndex(-1);
jcb2.setSelectedIndex(-1);
}
//ANOTHER OPTION
If((jcb1.getSelectedValue().equals("three") && (jcb2.getSelectedValue().equals("three")
{
LOGIC
}
}
});
jcb2.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e)
{
//You can replace this with, jcb2.getSelectedItem if you don't know the indexes or if they will be random.
if((jcb2.getSelectedIndex() == 0) && (jcb1.getSelectedIndex() == 2))
{
JOptionPane.showMessageDialog(null, "Cannot select 3 in both field.");
jcb1.setSelectedIndex(-1);
jcb2.setSelectedIndex(-1);
}
}
});
}
private JPanel configureCombo()
{
String[] cb1List = {"one", "two", "three"};
String[] cb2List = {"three", "four"};
comboPanel = new JPanel(new GridLayout(1,2));
jcb1 = new JComboBox(cb1List);
jcb2 = new JComboBox(cb2List);
comboPanel.add(jcb1);
comboPanel.add(jcb2);
return comboPanel;
}
/**
* #param args the command line arguments
*/
public static void main(String[] args) {
SOF s = new SOF();
}
}
On either one if matching boxes are selected an error message will pop up and deselect both comboboxes.
You could futz with the combobox's model, but easier would be to just check the selected values when the user asks the program to accept the values, perhaps in the ActionListener of a JButton, and then if two of the same values have been selected, deselect them and warn the user with a JOptionPane. Alternatively, you could have code in listeners (either Actionlistener or ItemListener) added to both JComboBox's that check the other combobox's selected value to make sure that they are not the same, and if so, warn the user and deselect the erroneous selection.
I have been trying to determine why my JComboBox is displaying the 1st item in the list through numerous Google searches, but I'm struggling to find relevant help. It could be that I don't know the correct terminology (hence the overly specific title of this question) and thus not finding the information that would explain my issue. I checked out the JComboBox API, and few of the listeners and models that it uses, but they did not seem likely candidates.
The JComboBox in question is inside a JTable, so I am not aware if that changes the default behaviour of it. The code I am using is as below:
//row and col are final due to usage inside anonymous inner class
public TableCellEditor getCellEditor(final int row, final int col)
{
String[] listItems = new String[arrayList.getSize()];
int i = -1;
for(String s : arrayList)
{
i++;
listItems[i] = s;
}
JComboBox<String> box = new JComboBox<>(listItems);
box.addItemListener(new ItemListener()
{
public void itemStateChanged(ItemEvent e)
{
if(e.getStateChange() == ItemEvent.SELECTED)
{
if(e.getItem().equals("Add/Edit Projectile"))
{
//Where Editor is a JFrame that will be opened
new Editor();
}
}
}
});
DefaultCellEditor list = new DefaultCellEditor(box);
}
Please note that the Arraylist in my program does not contain Strings, but instead a more complicated set of custom objects that I believe would distract from the main issue.
I haven't included a Renderer for JComboBox's in the JTable as I was happy enough with the way it appeared, and figured that my problem was more going to be something I have neglected to implement in the model/implemented wrong.
I've also provided a couple of screenshots to better portray my problem. The first image is when the JComboBox is not selected, and simply displaying the currently selected item.
The second image is when I have just clicked the JComboBox to bring up the list. As depicted, it will immediately bring up that first item, no matter what it is.
If anyone has any suggestions as to where to look/solutions, I would be very grateful.
EDIT
My particular table has two columns, where the left column is a variable name, and the right column is the value associated with the variable. The tables role is to display the properties of a selected object, where each value for different variable for different objects are likely to not be the same.
In this particular case, the cell displays a JComboBox with all the available Projectiles in the game we are making. Each enemy has a different type of projectile it defaults to. So when I click on a different enemy in our game area, the table will display all of their current properties (defaults if they have not been changed).
Enemies do have a getter for the Projectile, so I could determine what the currently selected enemy is, get it's projectile, do a toString() to find how it is to be represented in the list, and do a setValueAt().
The only problem is at the moment it is always selecting the first item in the list when the list is expanded.
Unless the values for the JComboBox are dynamically generated for each row, you should be able to just prepare the CellEditor ahead of time, for example...
JComboBox cb = new JComboBox(new String[]{"1", "2", "3", "4"});
DefaultCellEditor editor = new DefaultCellEditor(cb);
JTable table = new JTable(new DefaultTableModel(5, 1));
table.getColumnModel().getColumn(0).setCellEditor(editor);
This will set the selected value of the editor to the value of the cell when the editing process starts
Updated
In the case where the combobox values are dynamically generate per row, you could do something more like...
JComboBox cb = new JComboBox();
DefaultCellEditor editor = new DefaultCellEditor(cb) {
#Override
public Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected, int row, int column) {
JComboBox editor = (JComboBox) getComponent();
String[] listItems = new String[arrayList.getSize()];
int i = -1;
for (String s : arrayList) {
i++;
listItems[i] = s;
}
DefaultComboBoxModel model = new DefaultComboBoxModel(listItems);
editor.setModel(model);
editor.setSelectedItem(value);
return editor;
}
};
JTable table = new JTable(new DefaultTableModel(5, 1));
table.getColumnModel().getColumn(0).setCellEditor(editor);
Note the use of editor.setSelectedItem(value);, this will set the selected value to the cells current value...
You could also re-use the model, clearing it each time and re-filling it with new values. You might find this more efficient if you have a large number of rows as you won't need to constantly create a new model each time a cell is edited
Thow this is an oldie...
Your problem is most likely you don't implement "equals" in the class used in the combo.
The Combo needs to select the current item when it is being prepared and does so by iterating through the elements of the model and selects the first one that is equal to the value in the cell. If none is encountered then it leaves the combo as is (either first element or the last used element in a previous cell edit)
This is how you should default to the previously selected element:
//...
Object selectedItem = box.getSelectedItem();
//Add some elements to the jComboBox
box.setSelectedItem(selectedItem);
I am having trouble with my code. What i'm trying to do is:
1. Create a Check Box, make it visible
2. When Check Box is selected display Combo Box, which will have few items for example ("1","2")
3. When 1 is selected from Combo Box then make 1 Text Field visible
4. When 2 is selected from Combo Box then make 2 Text Field's visible
What I am able to do is when Check Box is clicked, it displays the Combo Box with the items.
I am not able provide functionality to the items in the Combo Box, such as when Item1 is clicked then make 1 Text Field visible.
Please help needed.
My Code:
public void replacement_used(){
no_of_part_used_label.setVisible(false);
no_part_used_list.setVisible(false);
part_no_one_label.setVisible(false);
part_no_one_field.setVisible(false);
part_no_two_label.setVisible(false);
part_no_two_field.setVisible(false);
part_no_three_label.setVisible(false);
part_no_three_field.setVisible(false);
part_no_four_label.setVisible(false);
part_no_four_field.setVisible(false);
part_no_five_label.setVisible(false);
part_no_five_field.setVisible(false);
HandlerClass handler = new HandlerClass();
replacement_part_check_box.addItemListener(handler);
}
private class HandlerClass implements ItemListener{
public void itemStateChanged(ItemEvent event){
if (replacement_part_check_box.isSelected()){
no_of_part_used_label.setVisible(true);
no_part_used_list.setVisible(true);
}
x();
}
}
public void x(){
System.out.println("Start of x fucntion");
if( no_part_used_list.getSelectedItem().equals("1") ){
System.out.println("It is 1");
part_no_one_label.setVisible(true);
part_no_one_field.setVisible(true);
}
}
Well All you need to do is to add an ActionListener to your ComboBox.
May be first you should go through this link so that you can understand the basics of using combobox in swing http://docs.oracle.com/javase/tutorial/uiswing/components/combobox.html
Also you need to learn coding conventions so that your code could become more readable and understandable.
I have a combo box with a list of entries and when I choose an entry eg dog I want to display an image of a dog beside the combo box.
Would anybody have any examples of this in swt that I could take a look at?
Ann.
Add a SelectionListener to your combo box.
combo.addSelectionListener( new SelectionAdapter() {
public void widgetSelected( SelectionEvent event ) {
// ...
}
} );
On the widgetSelectedmethod, get the selection index - using combo.getSelectionIndex() -, map it to your image and display it wherever you want (e.g. on a Label: label.setImage(image)).