Solved

Working with FocusListener in applet?

Posted on 2003-11-09
9
814 Views
Last Modified: 2013-11-23
On execution of the applet ,I want a list of dates to be displayed in a list box and an empty destination combo box should be displayed.As soon as I select the date of travel and tabs out of the list box,the flights available on that particular date should be updated in the combo box.
  Below is the code I wrote as you can understand the logic but I'am getting errors.The code is.....

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class AirLineReservation extends JApplet  implements FocusListener          
{
String date[]={"12/21/2001","01/12/2002","12/25/2001"};

String dest[]={"Chicago","Boston","New York","California","Atlanta"};

JPanel panelobj;
JComboBox destcombo;
JList datelist;
JButton submit;
public void init()
{
datelist=new JList(date);
datelist.addFocusListener(this);
destcombo=new JComboBox();
submit=new JButton("Submit");
panelobj=new JPanel();
getContentPane().add(panelobj);
panelobj.add(datelist);
panelobj.add(destcombo);
panelobj.add(submit);
}

class this implements FocusListener
{
public void focusGained(FocusEvent e)
{
JList source=(JList)e.getSource();
if (source.getSelectedValue()=="12/21/2001")
{
destcombo.removeAllItems();
String dest[]={"Chicago","Boston"};
destcombo=new JComboBox(dest);
return;
}

if(source.getSelectedValue()=="01/12/2002")
{
destcombo.removeAllItems();
String dest[]={"New York","California","Atlanta"};
destcombo=new JComboBox(dest);
return;
}
}
}
}

Since I'm learning java I doubt that "class this implements FocusListener" is wrong.I tried doing this...
ListListener Llisten=new ListListener();
datelist.addFocusListener(Llisten)
...
class ListListener implements FocusListener
{
...
}
But I got the following errors...
AirLineReservation.java:55: AirLineReservation is not abstract and does not overide abstract method focusLost(java.awt.event.FocusEvent) in java.awt.event.FocusListener
public class AirLineReservation
       ^
And similarly for AirLineReservation.ListListener I got the same error.
So please help me how to proceed? And btw the above one is just the practise I'm doing from a book and its not any homework.Thanks.
0
Comment
Question by:tony10
  • 5
  • 4
9 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 9710909
Just put the method

>>public void focusGained(FocusEvent e)

alone into your applet class. Remove anything other than these two things.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 9710916
i.e. you have declared that the applet implements FocusListener, so that method should be inside your applet.
0
 

Author Comment

by:tony10
ID: 9711020
Well this is correct?

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class AirLineReservation extends JApplet implements FocusListener          
{
String date[]={"12/21/2001","01/12/2002","12/25/2001"};

String dest[]={"Chicago","Boston","New York","California","Atlanta"};

JPanel panelobj;
JComboBox destcombo;
JList datelist;
JButton submit;
public void init()
{
datelist=new JList(date);
datelist.addFocusListener(this);
destcombo=new JComboBox();
submit=new JButton("Submit");
panelobj=new JPanel();
getContentPane().add(panelobj);
panelobj.add(datelist);
panelobj.add(destcombo);
panelobj.add(submit);
}

public void focusGained(FocusEvent e)
{
JList source=(JList)e.getSource();
if (source.getSelectedValue()=="12/21/2001")
{
destcombo.removeAllItems();
String dest[]={"Chicago","Boston"};
destcombo=new JComboBox(dest);
return;
}

if(source.getSelectedValue()=="01/12/2002")
{
destcombo.removeAllItems();
String dest[]={"New York","California","Atlanta"};
destcombo=new JComboBox(dest);
return;
}
}
}

But after compiling I'm getting ...AirLineReservation.java:4: AirLineReservation is not abstract and does not override abstract method focusLost(java.awt.event.FocusEvent) in java.awt.event.FocusListener
public class AirLineReservation extends JApplet implements FocusListener

       ^

0
Simplifying Server Workload Migrations

This use case outlines the migration challenges that organizations face and how the Acronis AnyData Engine supports physical-to-physical (P2P), physical-to-virtual (P2V), virtual to physical (V2P), and cross-virtual (V2V) migration scenarios to address these challenges.

 
LVL 86

Expert Comment

by:CEHJ
ID: 9711067
Yes, you'll have to override that method too.
0
 

Author Comment

by:tony10
ID: 9713399
The above code is compiling without any errors.But when I run it using Appletviewer I'm getting
 load: AirLineReservation.class can't be instantiated.
java.lang.InstantiationException
        at sun.reflect.InstantiationExceptionConstructorAccessorImpl.newInstance
 So how to rectify it please?
0
 

Author Comment

by:tony10
ID: 9713519
Sorry when I added just declaration of focusLost method its working, ie I added public void focusLost(FocusEvent e) { };
I'am also able to run the applet.But the problem is the combo box is not getting updated whenever I select a value from the list item? So please can anyone run the above to see what the problem is? Thanks.
0
 
LVL 86

Accepted Solution

by:
CEHJ earned 100 total points
ID: 9714399
It's not actually a FocusListener you need anyway. Try this:


import javax.swing.*;
import javax.swing.event.*;
import java.awt.*;
import java.awt.event.*;
public class AirLineReservation extends JApplet  implements ListSelectionListener
{
String date[]={"12/21/2001","01/12/2002","12/25/2001"};

String dest[]={"Chicago","Boston","New York","California","Atlanta"};

JPanel panelobj;
JComboBox destcombo;
JList datelist;
JButton submit;
public void init()
{
datelist=new JList(date);
datelist.getSelectionModel().addListSelectionListener(this);
destcombo=new JComboBox();
submit=new JButton("Submit");
panelobj=new JPanel();
getContentPane().add(panelobj);
panelobj.add(datelist);
panelobj.add(destcombo);
panelobj.add(submit);
}


public void valueChanged(ListSelectionEvent e)
{
    if (e.getValueIsAdjusting()) return;
    Object source = null;
    String newDest[] = null;
    if((source = e.getSource()) instanceof ListSelectionModel)
    {
      ListSelectionModel model = (ListSelectionModel)source;
      int firstIndex = model.getMinSelectionIndex();
      switch(firstIndex)
      {
        case 0: // ("12/21/2001")
          destcombo.removeAllItems();
          destcombo.setModel(new DefaultComboBoxModel(new String[] { "Chicago","Boston" }));
          return;

        case 1: // ("01/12/2002")
          destcombo.removeAllItems();
          destcombo.setModel(new DefaultComboBoxModel(new String[] { "New York","California","Atlanta" }));
          return;
      }

    }
    else
    {
      return;
    }
}


}
0
 

Author Comment

by:tony10
ID: 9714923
Now it worked ! Thanks a lot CEHJ :-)
But I guess similar to ListSelectionListener it works also with FocusListener?
I cant understand fully the following statement,
datelist.getSelectionModel().addListSelectionListener(this);
Instead cant we not use datelist.addListSelectionListener(this);?
Also what does that "this" do?
 Thank You.

0
 
LVL 86

Expert Comment

by:CEHJ
ID: 9715012
Focus is not really an appropriate concept - you want something to happen when you click on an item. With focus, this would happen too when you tabbed over it - not appropriate.

It's the *model* that you add the listener to, not the component. Swing uses a Model View Controller concept.

The 'this' reference (a reference to the main class) is passed so that a reference to the ListSelectionListener interface can be used to receive events.
0

Featured Post

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

For customizing the look of your lightweight component and making it look opaque like it was made of plastic.  This tip assumes your component to be of rectangular shape and completely opaque.   (CODE)
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question