hi
I want to know that i there any leave function in java like the one in c#.
what i am trying to do that there is 2 combobox and the list of second combobox depends on the data selected in first combobox.
so as soon as the user select the first combobox and enters the second combobox the list should be prepared and loaded in the second combobox.

Thanx in advance

Recommended Answers

All 5 Replies

Uhm, a FocusListener?

I tried to use FocusListener in many ways but its not working. Plz help me out

import javax.swing.*;

import java.io.*;
import java.util.*;
import java.awt.*;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.event.*;
import java.awt.event.FocusListener.*;
import java.lang.*;
class Reserve_Ticket 
{
  String Source_List[]={"Chennai","Hyderabad","Kolkata","Delhi"};
 String[] Dest_List1= new String[3];


  Reserve_Ticket(){
  final JFrame Res_Tic = new JFrame("XYZ AIRLINE");
  Res_Tic.setBounds(50,50,650,550);
  Res_Tic.setVisible(true);
  Res_Tic.setLayout(null);
  JLabel Src1 = new JLabel("Source");
  Src1.setBounds(50,50,150,20);
  Res_Tic.add(Src1);
  
  JLabel Dest1 = new JLabel("Destiny");
  Dest1.setBounds(50,100,150,20);
  Res_Tic.add(Dest1);
  
  
   final JComboBox src = new JComboBox(Source_List);
  src.setBounds(150,50,150,20);
  Res_Tic.add(src);
 String[] Dest_List2= new String[3];
 
  src.addFocusListener(new FocusAdapter() {
    public void focusLost(FocusEvent fe){
     
      String source;
      System.out.println("lost");
  source=(String)src.getSelectedItem();
  Dest_List1=Dest_List(source);
  
 
   
  
 
  
    }
  });
  JComboBox Dest = new JComboBox(Dest_List1);
  Dest.setBounds(150,100,150,20);
  Res_Tic.add(Dest);
 
 /* src.addFocusListener(new FocusAdapter() {
    public void focusLost(FocusEvent fe){
      String source;
      System.out.println("gain");
  source=(String)src.getSelectedItem();
  Dest_List1=Dest_List(source);
  System.out.println(Dest_List1[0]+Dest_List1[1]+Dest_List1[2]);
    }
  });
        //   ((Component)Res_Tic).addFocusListener( this );*/

  
  
  
  }
  String[] Dest_List(String Source)
  {
    int i=0,j=0;
    
    for(;i<4;i++)
    {
      if(!(Source.equals(Source_List[i])))
           Dest_List1[j++]=Source_List[i];
    }
    return Dest_List1;
  }
  

          
          
}
class main1
{
  public static void main(String[] args)
  {
    Reserve_Ticket r = new Reserve_Ticket();
  }
}

As soon as the first combobox loses focus (and the selected/entered value has changed) repopulate the second combobox (don't figure to disable it while doing so).

can you please give some more details. I tried many stuffs but unable to get the result.

package test;

import java.awt.BorderLayout;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;

import javax.swing.JFrame;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;

public class FocusTest {

	public static void main(String[] args) throws Exception {
		SwingUtilities.invokeLater(
			new Runnable() {
				public void run() { createGui(); }
			});
	}

	public static void createGui() {
		JFrame f = new JFrame();
		f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		final JTextField jtf1 = new JTextField(20);
		final JTextField jtf2 = new JTextField(20);
		jtf1.addFocusListener(
			new FocusListener() {
				public void focusGained(FocusEvent e) {}
				public void focusLost(FocusEvent e) { jtf2.setText(jtf1.getText()); }
			});
		f.getContentPane().add(jtf1, BorderLayout.NORTH);
		f.getContentPane().add(jtf2, BorderLayout.SOUTH);
		f.pack();
		f.setVisible(true);
	}
}

The Tutorials obviously give better information as does this.

Edit: P.S. In earlier post I meant to say don't forget to disable the combobox while you repopulate it.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.