display results from a combo box in swt - java

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)).

Related

Get rid of the extra line in my SWT ComboBoxCellEditor?

My SWT ComboBoxCellEditor has an extra line at the bottom.
Is there any way to get rid of it? It's there no matter how many elements I have.
The ComboBoxCellEditor uses a CCombo widget for editing. With CCombo#setVisibleItemCount() the number of visible items can be controlled.
Depending on when you know how many items should be visble you can configure the combo box. For example through overriding createControl
ComboBoxCellEditor editor = new ComboBoxCellEditor() {
#Override
protected Control createControl( Composite parent ) {
CCombo combo = ( CCombo )super.createControl( parent );
combo.setVisibleItemCount( 2 );
return combo;
}
};

When combo box is selected then display Combo Box and then when any Item in Combo Box is selected then make JTextField visible

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.

ComboBox problems in java

Can i do something like after i clicked the button then the combo box appear,the combo box only appear when i clicked the button.Is it even possible?
Here is my code:
search.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent ev) {
if(c3.getSelectedItem()=="First Floor"){
p1.add(c42);
}
});
The c3 is another combo box which contain from ground floor to fourth floor and c42 is combo box which contains room numbers for only First floor,i need something like when i clicked the search button then the c42 appear.
If c42 exists, and it's swing's ComboBox then You can simply use c42.setVisible(true).

Deactivate components until an element is selected from JComboBox

I am working on Java swing application using data base with MySQL
I need to know if I can deactivate components until select an element from JComboBox? I must know the choice of the 1st jcombobox to fill the 2nd JComboBox; the 1st choice is a foreign key on the 2nd, like that :
ResultSet res = st.executeQuery("SELECT NomF FROM famille_de_type");
while (res.next()) {
comboBox_Fam_innewT.addItem(res.getString(1));
}
this is my example :
Of course, you can. When you start work call setEnabled(false) to second comboBox. And add to 1st combobox ItemListener. It will be listen item selection.
firstComboBox.addItemListener(new ItemListener()
{
public void itemStateChanged(ItemEvent e)
{
if (e.getStateChange()==ItemEvent.SELECTED)
{
Object selectedItem = e.getItem(); // new item selected
// TODO select values for 2nd combobox
// TODO fill 2nd combobox
secondComboBox.setEnabled(true);
}
}
});
So in the ActionListenr of the JComboBox, simply call the setEnabled methods, passing false to disable them, or true to enable them
I need to know if i can deactivate components until select an element from jcombobox ?
YES. Why not Component.SetEnabled(false)?
Also you might want to look at ItemListener interface to achieve your goal. Here is more about Handling Events on a Combo Box.

how to control a combo box by using another combo box swing

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

Categories

Resources