Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Working with FocusListener in applet?

Posted on 2003-11-09
9
Medium Priority
?
821 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
Independent Software Vendors: 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 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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering 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

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
In this post we will learn different types of Android Layout and some basics of an Android App.
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
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.
Suggested Courses

618 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