Solved

Working with FocusListener in applet?

Posted on 2003-11-09
9
812 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
 
LVL 86

Expert Comment

by:CEHJ
ID: 9711067
Yes, you'll have to override that method too.
0
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 

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

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Java 1603 Error 2 42
sumHeights  challenge 17 61
Unable to start eclipse ? 17 86
Java and GPO 11 47
After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
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 learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

758 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now