• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Bear Bibeault
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Jj Roberts
  • Carey Brown
Bartenders:
  • salvin francis
  • Frits Walraven
  • Piet Souris

ActionListeners

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • 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: 6920
  • Mark post as helpful
  • send pies
  • 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".
 
Being a smart alec beats the alternative. This tiny ad knows what I'm talking about:
the value of filler advertising in 2020
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic