1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86
| import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.*;
public class ComboBox1 extends JPanel implements ActionListener
{
private String unit;
private String subUnit;
private JComboBox<String> mainComboBox;
private JComboBox<String> subComboBox;
private Hashtable<String, String[]> subItems = new Hashtable<String, String[]>();
public ComboBox1(String unit,String subUnit)
{setUnit(unit);
setSubUnit(subUnit);
}
//accesseurs
public void setUnit(String unit)
{this.unit=unit;}
public void getUnit(String unit)
{this.unit=unit;}
public void setSubUnit(String subUnit)
{this.subUnit=subUnit;}
public void getSubUnit(String subUnit)
{this.subUnit=subUnit;}
public ComboBox1()
{
String[] items = { "type d'unité", "volume", "masse", "distance" };
mainComboBox = new JComboBox<String>( items );
mainComboBox.addActionListener( this );
// prevent action events from being fired when the up/down arrow keys are used
mainComboBox.putClientProperty("JComboBox.isTableCellEditor", Boolean.TRUE);
add( mainComboBox );
// Create sub combo box with multiple models
subComboBox = new JComboBox<String>();
subComboBox.setPrototypeDisplayValue("XXXXXXXXXX");
add( subComboBox );
String[] subItems1 = { "l", "dl", " cl","ml" };
subItems.put(items[1], subItems1);
String[] subItems2 = { "g", "dg", "cg","mg" };
subItems.put(items[2], subItems2);
String[] subItems3 = { "m", "dm", "cm","mm" };
subItems.put(items[3], subItems3);
//JComboBox cb = new JComboBox(items);
//Object obj = cb.getSelectedItem();
}
public String getUn(){
return unit;}
public String getSubUn(){
return subUnit;}
public void actionPerformed(ActionEvent e)
{
unit = (String)mainComboBox.getSelectedItem();
Object o = subItems.get( unit );
if (o == null)
{
subComboBox.setModel( new DefaultComboBoxModel() );
}
else
{
subComboBox.setModel( new DefaultComboBoxModel( (String[])o ) );
}
}
} |
Partager