• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

ActionListeners

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
When I compile this code, I get an error - 'undefined variable this ' at the submitBtn.addActionListener(this).
Can anyone see what the problem is ?
import java.io.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class initial extends JInternalFrame implements ActionListener
{
// Declarations & Initialisations
private static JTabbedPane tabbedPane = new JTabbedPane();
private static JPanel topPanel = new JPanel();
private static JPanel panel1 = new JPanel();
private static JPanel panel2 = new JPanel();
private static JButton submitBtn = new JButton("Submit");
private static JLabel userNameL = new JLabel("Username :");
private static JLabel passwordL = new JLabel("Password :");
private static JTextField userNameTF = new JTextField();
private static JPasswordField passwordPF = new JPasswordField();
private static JDesktopPane desktop = new JDesktopPane();
public static String userName = new String();
public static String password = new String();
private static JScrollPane scrollPane1;

//System.out.println("in class initial");
public initial()
{
setClosable( true );
setMaximizable( true );
setIconifiable( true );
setResizable( true );
setTitle("Network Info Tool");
setSize(210, 250);

System.out.println("in method initial");

// Create a panel to hold all other components
topPanel = new JPanel();
topPanel.setLayout( new BorderLayout() );
getContentPane().add( topPanel );

//CreateTopPane( topPanel );

//desktop.add(initial);
moveToFront();
// Create the tab pages
createPage1();
createPage2();
// Create a tabbed pane
tabbedPane.addTab("Logon", panel1);
tabbedPane.addTab("About", panel2);
topPanel.add(tabbedPane, BorderLayout.CENTER);
}

private void CreateTopPane( JPanel topPanel )
{
// Create a text area
JTextArea area = new JTextArea();
// Load a file into the text area, catching any exceptions
try {
FileReader fileStream = new FileReader( "TestFrame.java" );
area.read( fileStream, "TestFrame.java" );
}
catch( FileNotFoundException e )
{
System.out.println( "File not found" );
}
catch( IOException e )
{
System.out.println( "IOException occurred" );
}
// Create the scrolling pane for the text area
scrollPane1 = new JScrollPane();
scrollPane1.getViewport().add( area );
topPanel.add( scrollPane1, BorderLayout.CENTER );
}

public static void createPage1()
{

panel1.setLayout(null);
userNameL.setBounds(10, 15, 150, 20);
panel1.add(userNameL);
userNameTF.setBounds(10, 35, 150, 20);
panel1.add(userNameTF);
passwordL.setBounds(10, 60, 150, 20);
panel1.add(passwordL);
passwordPF.setBounds(10, 80, 150, 20);
passwordPF.setEchoChar('*');
panel1.add(passwordPF);

submitBtn.setBounds(30, 120, 110, 30);
panel1.add(submitBtn);

submitBtn.addActionListener(this);
}

public void actionPerformed(ActionEvent e)
{
String logonError = "Incorrect User Name or Password";

System.out.println("USERNAME = " + userName);

if(e.getSource() == submitBtn)
{
System.out.println("Action: submitButton pressed");
userName = userNameTF.getText();
password = passwordPF.getText();
System.out.println("USERNAME = " + userName);
System.out.println("PASSWORD = " + password);


if ((daveframe.dave(userName, password)) == true)
{
System.out.println("NO ERROR");
}
else
{
JOptionPane.showMessageDialog(null,logonError, " ", JOptionPane.WARNING_MESSAGE);
System.out.println("ERROR");
}
}
}
public static void createPage2()
{
panel2.setLayout(new BorderLayout());
}
}
 
Sheriff
Posts: 7001
6
Eclipse IDE Python C++ Debian Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That's because your method createPage1 is declared as static. Staic methods exist for all object instances, and have no "this".
It should work if you remove the "static".
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic