?
Solved

Working with FocusListener in applet?

Posted on 2003-11-09
9
Medium Priority
?
819 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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 400 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

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!

Question has a verified solution.

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

Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
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 basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
Suggested Courses
Course of the Month15 days, 10 hours left to enroll

743 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