Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

trouble with actionlistener in a panel in a tabbed pane.

Status
Not open for further replies.

KishMish

Programmer
May 11, 2002
3
IN
Hi, please help me! this is the code i wrote, with the error given below:
CODE:

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class tabslisten extends JApplet
{
JPanel RecPanel;

GridBagLayout gbl;
GridBagConstraints gbc;

public void init ()
{
FlowLayout flow;
flow = new FlowLayout();
Container content = getContentPane();
content.setLayout(new GridLayout());

JTabbedPane tabpane = new JTabbedPane();
content.add(tabpane, flow);

RecPanel = new JPanel();
recipientDetails();

tabpane.addTab("Recipient Details", null, RecPanel, "Recipient Details");
}
public void recipientDetails()
{
JLabel lblRecFName;
JLabel lblRecLName;
JTextField txtRecFName;
JTextField txtRecLName;

JButton btnValidate;

gbl = new GridBagLayout();
gbc = new GridBagConstraints();
RecPanel.setLayout(gbl);

lblRecFName = new JLabel("First Name");
lblRecLName = new JLabel("Last Name");
txtRecFName = new JTextField(8);
txtRecLName = new JTextField(5);

btnValidate= new JButton("Validate");

gbc.anchor = GridBagConstraints.NORTHWEST;
gbc.gridx = 1;
gbc.gridy = 8;
gbl.setConstraints(lblRecFName, gbc);
RecPanel.add(lblRecFName);

gbc.anchor = GridBagConstraints.NORTHWEST;
gbc.gridx = 4;
gbc.gridy = 8;
gbl.setConstraints(txtRecFName, gbc);
RecPanel.add(txtRecFName);

gbc.anchor = GridBagConstraints.NORTHWEST;
gbc.gridx = 1;
gbc.gridy = 11;
gbl.setConstraints(lblRecLName, gbc);
RecPanel.add(lblRecLName);

gbc.anchor = GridBagConstraints.NORTHWEST;
gbc.gridx = 4;
gbc.gridy = 11;
gbl.setConstraints(txtRecLName, gbc);
RecPanel.add(txtRecLName);


gbc.anchor = GridBagConstraints.NORTHWEST;
gbc.gridx = 4;
gbc.gridy = 32;
gbl.setConstraints(btnValidate, gbc);
RecPanel.add(btnValidate);


validateAction validateButton = new validateAction();
btnValidate.addActionListener(validateButton);

}
class validateAction implements ActionListener
{
public void actionPerformed(ActionEvent evt)
{
Object obj = evt.getSource();

if (obj == btnValidate)
{
String fName = txtRecFName.getText();
String lName = txtRecLName.getText();

if (fName.length()==0)
{
getAppletContext().showStatus("First Name not entered.");
return;
}
else if (lName.length()==0)
{
getAppletContext().showStatus("Last Name not entered.");
return;
}
else
{
getAppletContext().showStatus("Successful.");
}
}
}
}
}

ERROR:

tabslisten.java:90: Undefined variable: btnValidate
if (obj == btnValidate)
^
tabslisten.java:92: Undefined variable or class name: txtRecFName
String fName = txtRecFName.getText();
^
tabslisten.java:93: Undefined variable or class name: txtRecLName
String lName = txtRecLName.getText();
^
3 errors

Press any key to continue...

I'm sure I'm doing something really stupid, but I don't know what! ANY and ALL help will be appreciated! I've simplified the code as much as possible, but i HAVE to be able to validate, and i HAVE to use the panels in the tabbed pane! Thanks!
 
Your btnValidate is declared as a instance of JButton for the recipientDetails() method, access to this instance does not exist for any code outside of that method. Same thing with your JTextFields. Why are you checking that the obj equals btnValidate anyways? It does not look like you are using that listener for anything else so I would omit the test and just perform the rest of the code. Hopefully that be will be helpful. If you want access to them just declare the variables outside of any methods in the main class area. If you get a static error, you could try this.(variableName) to see if that will work but try just declaring the variables as variables that belong to the object first. Hopefully that will be helpful.

JavaDude32
 
Hey,
thanks for the quick reply. I'm a Java newbie, so although what you said makes sense, i can't implement it!! the reason for the if condition is that there are actually 2 btns on that panel, (like i said, i simplified the code as much as possible!) so i need that line!! could you PLEASE give me the code for what i need if it isn't too much trouble? thanks a ton!
 
actually nevermind - i figured out what you were saying, but now the problem is that although it executes upto the if condition, it doesnt go anyfurther - i put showStatus("click") after the Object obj = evt.getSource() line, and it executes, so it's catching the click but it isnt entering the if statement. any ideas? thanks a ton!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top