Solved

Working with FocusListener in applet?

Posted on 2003-11-09
9
816 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
web services creation SOAP vs REST 5 80
maven module vs maven project 3 69
Website checklist for browser compatibility? 2 37
Coding for the first time 9 62
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:

685 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