Java

Moderators: zibadian
Number of threads: 7832
Number of posts: 18231

This Forum Only
Post New Thread
Single Post View       Linear View       Threaded View      f

Report
action listener problem Posted by mack1320 on 11 May 2011 at 7:25 AM
ok so im coding a program trying to add an action listener, Giving me this error:

Program.java:185: non-static method init() cannot be referenced from a static context init();

here is my code:

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

/**
 * This program will recieve the IP, PCI and client name of client computers and save them to a text file.
 * 
 * @author Marko Klesnik 
 * @version 1.0
 */

public class Project implements ActionListener
{
	// Variable Declarations
    // Declare JButtons
    public JButton btnNew, btnSave, btnFind, btnDelete, btnExit;
    
    // Declare JLabels
    public JLabel lblName, lblIP, lblPCI, lblDesc, lblDesc2, lblDesc3, lblDesc4;
    
    // Declare JTextFields
    public JTextField txtName, txtIP, txtPCI;
    
    public static String [] clientList;

	
	
	public void init ()
	{
		
		// Create a string array
	 	clientList = new String [9];
	 	clientList [0] = "Please click Find Entry..";
 
    
    	// Declare JList
    	JList Selector = new JList ();
    	Selector.setSize(90, 90);
    	Selector.setBackground(Color.BLACK);
    	Selector.setListData (clientList);
    
   		 // Scrollpane for JList
    	JScrollPane ListScrollPane = new JScrollPane(Selector);
    	ListScrollPane.setPreferredSize(new Dimension(550,200));
    
    	// Define default JButton data
        JButton btnNew = new JButton("New Entry ");
        btnNew.setBackground(Color.BLACK);
        btnNew.setForeground(Color.GREEN);
        JButton btnSave = new JButton("Save Entry");
        btnSave.setBackground(Color.BLACK);
        btnSave.setForeground(Color.GREEN);
        JButton btnFind = new JButton("Find Entry");
        btnFind.setBackground(Color.BLACK);
        btnFind.setForeground(Color.GREEN);
        JButton btnDelete = new JButton("Delete Entry");
        btnDelete.setBackground(Color.BLACK);
        btnDelete.setForeground(Color.GREEN);
        JButton btnExit = new JButton("Exit System");
        btnExit.setBackground(Color.BLACK);
        btnExit.setForeground(Color.RED);
        
        
               

        
        // Define default JLabel data
        JLabel lblName = new JLabel("Client Name:");
        lblName.setForeground(Color.WHITE);
        JLabel lblIP = new JLabel("IP Address:");
        lblIP.setForeground(Color.WHITE);
        JLabel lblPCI = new JLabel("PC Identifier:");
        lblPCI.setForeground(Color.WHITE);
        JLabel lblDesc = new JLabel(" This program will allow the user to add ");
        lblDesc.setForeground(Color.WHITE);
    	JLabel lblDesc2 = new JLabel(" edit or delete Users.their PC Identifiers ");
    	lblDesc2.setForeground(Color.WHITE);
    	JLabel lblDesc3 = new JLabel(" and their PC's IP Address. ");
    	lblDesc3.setForeground(Color.WHITE);
    	JLabel lblDesc4 = new JLabel(" Please click on the 'Find Entry' button to generate a client list... ");
    	lblDesc4.setForeground(Color.WHITE);
    	
    	
    	
        //Define default JTextField data
  

        JTextField txtName = new JTextField("Client1", 10);
        txtName.setBackground(Color.BLACK);
        txtName.setForeground(Color.GREEN);
        JTextField txtIP = new JTextField("", 10);
        txtIP.setBackground(Color.BLACK);
        txtIP.setForeground(Color.GREEN);
        JTextField txtPCI = new JTextField("", 10);
        txtPCI.setBackground(Color.BLACK);
        txtPCI.setForeground(Color.GREEN);
        
        //Create container + Layout
        SpringLayout scnLayout = new SpringLayout();
    
        
        // Create a frame and apply settings
        JFrame progFrame = new JFrame("[COMPANY NAME] Client Recording Software");
        progFrame.setVisible(true);
        progFrame.setSize(900, 800);
        progFrame.setResizable(false);
                
        // Create a JPanel and apply settings
        JPanel panel1 = new JPanel();
        panel1.setVisible(true);
        panel1.setSize(200, 300);
        panel1.setBackground(Color.black);
        panel1.setLayout(scnLayout);
        
        // Add panels to frame
        progFrame.add(panel1);
        
        // Add components to panels
        panel1.add(lblName);
        panel1.add(txtName);
        panel1.add(btnNew);
        btnNew.addActionListener(this);
        panel1.add(lblPCI);
        panel1.add(txtPCI);
        panel1.add(lblIP);
        panel1.add(txtIP);
        panel1.add(btnSave);
        panel1.add(btnFind);
        panel1.add(btnDelete);
        panel1.add(btnExit);
        panel1.add(lblDesc);
        panel1.add(lblDesc2);
        panel1.add(lblDesc3);
        panel1.add(lblDesc4);
        panel1.add(ListScrollPane);

        // Set locations of panel1 components
        scnLayout.putConstraint(SpringLayout.WEST, lblName, 350, SpringLayout.WEST, panel1);
    	scnLayout.putConstraint(SpringLayout.NORTH, lblName, 200, SpringLayout.NORTH, panel1);
    	scnLayout.putConstraint(SpringLayout.NORTH, txtName, 200, SpringLayout.NORTH, panel1);
    	scnLayout.putConstraint(SpringLayout.WEST, txtName, 35, SpringLayout.EAST, lblName);
    	scnLayout.putConstraint(SpringLayout.NORTH, btnNew, 10, SpringLayout.NORTH, panel1);
    	scnLayout.putConstraint(SpringLayout.WEST, btnNew, 25, SpringLayout.WEST, panel1);
    	scnLayout.putConstraint(SpringLayout.NORTH, lblPCI, 250, SpringLayout.NORTH, panel1);
    	scnLayout.putConstraint(SpringLayout.WEST, lblPCI, 350, SpringLayout.WEST, panel1);
    	scnLayout.putConstraint(SpringLayout.NORTH, txtPCI, 250, SpringLayout.NORTH, panel1);
    	scnLayout.putConstraint(SpringLayout.WEST, txtPCI, 35, SpringLayout.EAST, lblPCI);
    	scnLayout.putConstraint(SpringLayout.NORTH, lblIP, 300, SpringLayout.NORTH, panel1);
    	scnLayout.putConstraint(SpringLayout.WEST, lblIP, 350, SpringLayout.WEST, panel1);
    	scnLayout.putConstraint(SpringLayout.NORTH, txtIP, 300, SpringLayout.NORTH, panel1);
    	scnLayout.putConstraint(SpringLayout.WEST, txtIP, 45, SpringLayout.EAST, lblIP);
    	scnLayout.putConstraint(SpringLayout.NORTH, btnSave, 10, SpringLayout.NORTH, panel1);
    	scnLayout.putConstraint(SpringLayout.WEST, btnSave, 150, SpringLayout.EAST, btnNew);
    	scnLayout.putConstraint(SpringLayout.NORTH, btnFind, 10, SpringLayout.NORTH, panel1);
    	scnLayout.putConstraint(SpringLayout.EAST, btnFind, -150, SpringLayout.WEST, btnDelete);
    	scnLayout.putConstraint(SpringLayout.NORTH, btnDelete, 10, SpringLayout.NORTH, panel1);
    	scnLayout.putConstraint(SpringLayout.EAST, btnDelete, -25, SpringLayout.EAST, panel1);
    	scnLayout.putConstraint(SpringLayout.EAST, btnExit, -25, SpringLayout.EAST, panel1);
    	scnLayout.putConstraint(SpringLayout.SOUTH, btnExit, -20, SpringLayout.SOUTH, panel1);
    	scnLayout.putConstraint(SpringLayout.WEST, ListScrollPane, 200, SpringLayout.WEST, panel1);
    	scnLayout.putConstraint(SpringLayout.NORTH, ListScrollPane, 150, SpringLayout.SOUTH, txtIP);
    	scnLayout.putConstraint(SpringLayout.WEST, lblDesc, 350, SpringLayout.WEST, panel1);
    	scnLayout.putConstraint(SpringLayout.NORTH, lblDesc, 80, SpringLayout.NORTH, panel1);
    	scnLayout.putConstraint(SpringLayout.WEST, lblDesc2, 350, SpringLayout.WEST, panel1);
    	scnLayout.putConstraint(SpringLayout.NORTH, lblDesc2, 10, SpringLayout.SOUTH, lblDesc);
    	scnLayout.putConstraint(SpringLayout.WEST, lblDesc3, 385, SpringLayout.WEST, panel1);
    	scnLayout.putConstraint(SpringLayout.NORTH, lblDesc3, 10, SpringLayout.SOUTH, lblDesc2);
    	scnLayout.putConstraint(SpringLayout.WEST, lblDesc4, 300, SpringLayout.WEST, panel1);
    	scnLayout.putConstraint(SpringLayout.NORTH, lblDesc4, -16, SpringLayout.NORTH, ListScrollPane);
    		
	}
   
   public void actionPerformed(ActionEvent e)
    {

            //Set if statements to true by clicking calculate button
        if (e.getSource() == btnNew)  {  }
   
    }
   
    public static void main(String[] args)
	 	{  
	 	init();
	 	
	 	}   	
}


Ive had it working before but just dont know how to fix this error, Cheers.
Report
Re: action listener problem Posted by totha on 16 May 2011 at 12:36 AM
Change the line in the main method to:
new Project().init();
Report
Re: action listener problem Posted by bad_coder on 8 Sept 2011 at 10:10 AM
I know this old but the problem is that JButton btnNew is declared twice. Delete the second and it will work with the prior change suggested to do a new Project ().init();
Report
This post has been deleted. Posted by bad_coder on 8 Sept 2011 at 10:12 AM
This post has been deleted.



 

Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic
© Copyright 2011 Programmersheaven.com - All rights reserved.
Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
Operated by CommunityHeaven, a BootstrapLabs company.