• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Problem about calling the component

 
Jess Ishasder
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi
I have created an applet with JTabbedPane on it. For each tab , I created a panel.
Suppose I have a JTextField object on a panel
1) How can I call that textfield?
2) How can I add actionListener to that textfield?
Part of my code is here:
================
ublic class Exercise3 extends JApplet{
JTabbedPane tabbedPane=null;
JPanel panel1=null;

public void init(){
//Create a tabbed pane
tabbedPane = new JTabbedPane();
createPanel1();

//Add the panels to the tabbed pane
tabbedPane.add("Add",panel1);
Container c = getContentPane();
c.add(tabbedPane);

}//End of init()
public void createPanel1(){

//Create panel1
panel1 = new JPanel();
JTextField txtEntry = new JTextField(20);
.....
.....
/*==============Add listener============*/
txtEntry.addActionListener(new TextFieldHandlerPanel1());<---"What should I put here to act as the argument of TextFieldHandlerPanel1?"
....
}//End of createPanel1()
class TextFieldHandlerPanel1 implements ActionListener{
....
....
//Constructor
public TextFieldHandlerPanel1(){
}
//Define method---actionPerformed()
public void actionPerformed(ActionEvent e){
String s = txtEntry.getText().trim();<--"How can I call txtEntry?"
.....
}//End of TextFieldHandlerPanel1
Thank you for your help!!! :roll:
 
Manfred Leonhardt
Ranch Hand
Posts: 1492
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
The ActionEvent gives you the component that the event was generated within:
getSource();
You can perform the following:

NOTE: Your user name chosen here is invalid. If you wish to continue posting please read the naming policy and re-register with a valid name (i.e., first name + space + last name).
Regards,
Manfred.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic