I`ve got a question about comboboxes,
i want that the list of a JCB is changed when i choose between 2 options from another JCB.
I thougt it word work with only changeing the string- arrays, and the interface
changes automatically --> nope..
// aangeven wat in het combobox van Rotterdam mag staan
private String raPlaatsen[] = { "Alicante", "Faro" };
// aangeven wat in het combobox van Amsterdam
private String aaPlaatsen[] = { "Bacelona", "Antalya", "Arrecife", "Alicante", "Bodrun" };
private String[] plaats = aaPlaatsen;
//thread-safe gui changing with swingUtilities.invokeLater( Runnable )
private Runnable run = new Runnable(){
public void run()
{
getContentPane().invalidate();
getContentPane().removeAll();
getContentPane().add(vertrek);
getContentPane().add(naar);// = new JComboBox( plaats ));
getContentPane().add(vdag);
getContentPane().add(vmaand);
getContentPane().add(volwassenen);
getContentPane().add(kinderen);
getContentPane().add(babys);
getContentPane().add(zoek);
getContentPane().add(vDag);
getContentPane().add(aDatum);
getContentPane().add(vTijd);
getContentPane().add(aTijd);
getContentPane().add(vlucht);
getContentPane().validate();
}
};
public GridLayoutDemo()
{
super( "GridLayout Demo" );
// The URL specifying the Books database to which
// this program connects using JDBC to connect to a
// Microsoft ODBC database.
String url = "jdbc:odbc:vluchtinformatie";
// Load the driver to allow connection to the database
try {
Class.forName( "sun.jdbc.odbc.JdbcOdbcDriver" );
Container c = getContentPane();
c.setLayout( grid1 );
setSize(500,200);
vertrek = new JComboBox(vPlaatsen);
vertrek.setName("vertrek");
vertrek.setMaximumRowCount(2);
vertrek.addActionListener( this );
c.add(vertrek);
show();
//als de comment code bij run weggehaald zou worden, moet deze ook weg,
//dit omdat je 2 verschillende arrays moet kunnen tonen bij aankomstplaatsen
naar = new JComboBox(plaats);
naar.setName("naar");
naar.setMaximumRowCount(6);
naar.addActionListener( this );
c.add(naar);
show();
vdag = new JComboBox(dag);
vdag.setName("dag");
vdag.setMaximumRowCount(5);
vdag.addActionListener( this );
c.add(vdag);
show();
vmaand = new JComboBox(maand);
vmaand.setName("maand");
vmaand.setMaximumRowCount(5);
vmaand.addActionListener( this );
c.add(vmaand);
show();
volwassenen = new JComboBox(volwassen);
volwassenen.setName("volwassen");
volwassenen.setMaximumRowCount(5);
volwassenen.addActionListener( this );
c.add(volwassenen);
show();
kinderen = new JComboBox(kind);
kinderen.setName("kind");
kinderen.setMaximumRowCount(5);
kinderen.addActionListener( this );
c.add(kinderen);
show();
babys = new JComboBox(baby);
babys.setName("baby");
babys.setMaximumRowCount(5);
babys.addActionListener( this );
c.add(babys);
show();
zoek = new JButton( "Zoek vlucht" );
zoek.addActionListener( this );
c.add(zoek);
show();
vDag = new JTextField( "Vertrekdatum" );
c.add(vDag);
show();
aDatum = new JTextField( "Aankomstdatum" );
c.add(aDatum);
show();
vTijd = new JTextField( "Vertrektijd" );
c.add(vTijd);
show();
aTijd = new JTextField( "Aankomsttijd" );
c.add(aTijd);
show();
vlucht = new JTextField("Vluchtnummer" );
c.add(vlucht);
show();
}
public void actionPerformed(ActionEvent e)
{
Object source4 = e.getSource();
if (source4 instanceof JComboBox)
{
JComboBox combo = (JComboBox) source4;
if (combo.getName().equals("dag"))
{
JComboBox avrida = (JComboBox)e.getSource();
String dag = (String)avrida.getSelectedItem();
System.out.println("dag is "+dag);
}
if (combo.getName().equals("naar"))
{
JComboBox avrida = (JComboBox)e.getSource();
String naar = (String)avrida.getSelectedItem();
System.out.println("naar is "+naar);
}
if (combo.getName().equals("maand"))
{
JComboBox avrida = (JComboBox)e.getSource();
String maand = (String)avrida.getSelectedItem();
System.out.println("maand is "+maand);
}
if (combo.getName().equals("vertrek"))
{
if (((String) combo.getSelectedItem()).equals(vPlaatsen[0]))
{
plaats = aaPlaatsen;
SwingUtilities.invokeLater( run );
System.out.println("aaplaatseb? "+plaats);
}
else if (((String) combo.getSelectedItem()).equals(vPlaatsen[1]))
{
plaats = raPlaatsen;
SwingUtilities.invokeLater( run );
System.out.println("raplaatsen? "+plaats);
}
JComboBox avrida = (JComboBox)e.getSource();
String vertrek = (String)avrida.getSelectedItem();
System.out.println("vertrek is "+vertrek);
}
if (combo.getName().equals("volwassen"))
{
JComboBox avrida = (JComboBox)e.getSource();
String volwassen = (String)avrida.getSelectedItem();
System.out.println("volwassen is "+volwassen);
}
if (combo.getName().equals("kind"))
{
JComboBox avrida = (JComboBox)e.getSource();
String kind = (String)avrida.getSelectedItem();
System.out.println("kind is "+kind);
}
if (combo.getName().equals("baby"))
{
JComboBox avrida = (JComboBox)e.getSource();
String baby = (String)avrida.getSelectedItem();
System.out.println("baby is "+baby);
}
}
}
public static void main( String args[] )
{
try
{
UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel" );
}
catch (Exception e) { }
GridLayoutDemo app = new GridLayoutDemo();
app.addWindowListener(
new WindowAdapter()
{
public void windowClosing( WindowEvent e )
{
System.exit( 0 );
}
}
);
}
}
The purpose is that if you change something with "vertrek" (Amsterdam or Rotterdan) Then the index is changed bij "naar" (plaatsen, raplaatsen, aaplaatsen)
Bookmarks