Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Swing!  RSS feed

 
Angela Jessi
Ranch Hand
Posts: 428
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I just copied following code from one book. I want to understand that in KeyListener class we used extends KeyAdapter and in Button we used implements ActionListener.Why can't we use implement(instead of extends in KeyAdapter)
import javax.swing.*;
import javax.awt.event.*;
import javac.awt.*;
public class TKeyEvent extends JApplet{
Container contentPane;
JLabel label;
JTextField txt;
public void init(){
contentPane = this.getContentPane();
txt = new JTextField(25);
txt.addKeyListener(new MyKeyListener());

Button button = new Button("Clear");
button.addActionListener(new ButtonListener());
label = new JLabel("Key Typed: Nill");
label.setBorder(BorderFactory.createTitledBorder("You Pressed the Following key"));
contentPane.setLayout(new BorderLayout());
contentPane.add("North",txt);
contentPane.add(label);
contentPane.add("South",button);
txt.requestFocus();
}
// create MyKeyListener class

class MyKeyListener extends KeyAdapter{

public void keyTyped(KeyEvent e){
char c = e.getKeyChar();
label.setText("Key Typed: " + c);
}
}
//Create ButtonListener class
class ButtonListener implements ActionListener{

public void actionPerformed(ActionEvent e)
{
txt.setText("");
txt.requestFocus();
}
}
}
Thanks in advance,
Angela
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Interfaces define behavior that is expected of any class that implements that interface. When you implement an interface, you are required to override all of its methods (even the ones you aren't interested in).
To save time there are classes called Adapters that implement the various interfaces. By extending an adapter you can avoid having to deal with methods you don't care about, because you won't be required to override them all. You can just override the ones you care about and move on.
You can't implement an adapter class because they aren't interfaces, they're abstract classes.
Does that help at all?
 
Angela Jessi
Ranch Hand
Posts: 428
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Bodie,
I got it!Thanks a lot! Is there anyother classes like Adapters which behaves as abstract class and for that we need to call only Extend not implement. And also we can make our own Methods in Adapter class(means we can make our own name methods)
Thanks again
angela
Originally posted by Angela Jessi:
I just copied following code from one book. I want to understand that in KeyListener class we used extends KeyAdapter and in Button we used implements ActionListener.Why can't we use implement(instead of extends in KeyAdapter)
import javax.swing.*;
import javax.awt.event.*;
import javac.awt.*;
public class TKeyEvent extends JApplet{
Container contentPane;
JLabel label;
JTextField txt;
public void init(){
contentPane = this.getContentPane();
txt = new JTextField(25);
txt.addKeyListener(new MyKeyListener());

Button button = new Button("Clear");
button.addActionListener(new ButtonListener());
label = new JLabel("Key Typed: Nill");
label.setBorder(BorderFactory.createTitledBorder("You Pressed the Following key"));
contentPane.setLayout(new BorderLayout());
contentPane.add("North",txt);
contentPane.add(label);
contentPane.add("South",button);
txt.requestFocus();
}
// create MyKeyListener class

class MyKeyListener extends KeyAdapter{

public void keyTyped(KeyEvent e){
char c = e.getKeyChar();
label.setText("Key Typed: " + c);
}
}
//Create ButtonListener class
class ButtonListener implements ActionListener{

public void actionPerformed(ActionEvent e)
{
txt.setText("");
txt.requestFocus();
}
}
}
Thanks in advance,
Angela

 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The event adapter classes are the only ones that I can think of that are used this way. Yes, you can add your own methods of your own creation to your adapter class.
 
Angela Jessi
Ranch Hand
Posts: 428
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks bodie,
Angela
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!