Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: button action problem

  1. #1
    Member
    Join Date
    Mar 2011
    Posts
    198
    My Mood
    Daring
    Thanks
    7
    Thanked 4 Times in 4 Posts

    Default button action problem

    Hello, I have been working on this actionlistener for the past 3 hours. Cannot seem to get it working.

    I have set strSave to "false" in initialization, Further down in actionperformed i set it to true once the btSave (save button) is clicked, i then set an if strSave == true then execute the code which is my problem. It will not execute the code once the button is clicked.

    if it helps im using strings instead of integers..

    import java.applet.Applet;                     
    import java.awt.*;
    import java.awt.event.*;
    import java.util.*;
    import java.net.*;
    import java.io.*;
    import java.lang.*;
    import javax.swing.*;
    import java.awt.Color;
     
    public class IPAdress extends Applet implements ActionListener
     
     
    {
        //Create the Find frame
        Frame frFind = new Frame("Find");
     
     
        //Text file to read
        File mainFile = new File("ipaddress.txt");
     
        //Create box containers for layout
        private Box box1 = Box.createVerticalBox();
        private Box box2 = Box.createVerticalBox();
        private Box box3 = Box.createVerticalBox();
        private Box box4 = Box.createHorizontalBox();
        //This box just defines a horizontal strut to seperate buttons abit from text fields
        private Box NewLine = Box.createHorizontalBox();
     
        //Define Buttons    
        private Button btNew, btSave, btDelete, btFind, btExit, btLeft, btLeft2, btRight, btRight2, btCombined;
     
        //Define Labels
        private Label lblName, lblPCI, lblIP, lblDesc, lblDesc2, lblDesc3;
     
        //Define Strings
        private String clientIP, computername, strName, strPCI, strIPaddress, strNew, strSave, strDelete, strFind, strExit;
     
        //Define Text Fields
        private TextField txtName, txtPCI, txtIP;
     
     
        // define default values of arrays, variables
        private int k;
        String[] arrNames;
     
        //Declare a FileInputStream variable
        private FileInputStream FileinputStream;
     
        //Initialization method
        public void init()
        {
        strNew = "false";   
        strSave = "false";
        strDelete = "false";
     
        //Define array sizes and some values
        arrNames = new String[10];
     
        //Get the client IP and set it to strIP string    
        try 
        {
           clientIP = InetAddress.getLocalHost().getHostAddress ();
        }
            catch(Exception IP)
        { 
            // No return value needed
        } // End strIP TRY
     
        //Get the client Computer Name and set it to strName String
        try
        {
          computername = InetAddress.getLocalHost().getHostName ();
        }
            catch (Exception HN)    
        {
          // No return value needed
        }// End strName TRY
     
     
     
            //Set frame size
            setSize(320,250);
            frFind.setSize(400,380);
     
            //Convert to RGB code & set frame background color
            Color background = new Color(0, 51, 0);
            setBackground(background);
     
            //Set find frame values
            frFind.setVisible(false);
            frFind.setLocationRelativeTo(null);
     
            //Create new labels
            Label lblName = new Label ("Name:");
            Label lblPCI = new Label ("PC Identifier:");
            Label lblIP = new Label ("IP Address:");
     
            //Set label colors
            lblName.setForeground(Color.white);
            lblPCI.setForeground(Color.white);
            lblIP.setForeground(Color.white);
     
            //Create new textfields
            TextField txtName = new TextField ();
            TextField txtPCI = new TextField (computername);
            TextField txtIP = new TextField (clientIP);
     
            //Create new buttons
            Button btNew = new Button ("New Entry");
            Button btSave = new Button ("Save Entry");
            Button btDelete = new Button ("Delete Entry");
            Button btFind = new Button ("Find Entry");
            Button btExit = new Button ("Exit");
            Button btLeft = new Button ("|<");
            Button btLeft2 = new Button ("<");
            Button btRight = new Button (">|");
            Button btRight2 = new Button (">");
     
            //Add Labels to box 1
            box1.add(Box.createVerticalStrut(20));
            box1.add(lblName);
            box1.add(lblPCI);
            box1.add(lblIP);
            box1.add(Box.createVerticalStrut(10));
            box1.setVisible(true);
     
            //Add TextFields to box 2
            box2.add(Box.createVerticalStrut(10));
            box2.add(txtName);
            box2.add(txtPCI);
            box2.add(txtIP);
            box2.setVisible(true);
     
            //Create a greater space between textfields(box2) and buttons(box3)
            NewLine.add(Box.createHorizontalStrut(30));
     
            //Add menu buttons to box 3
            box3.add(Box.createVerticalStrut(20));
            box3.add(btNew);
            box3.add(Box.createVerticalStrut(1));
            box3.add(btSave);
            box3.add(Box.createVerticalStrut(1));
            box3.add(btDelete);
            box3.add(Box.createVerticalStrut(1));
            box3.add(btFind);
            box3.add(Box.createVerticalStrut(20));
            box3.add(btExit);
            box3.setVisible(true);
     
            //Add bottom buttons to box 4
            box4.add(btLeft);
            box4.add(Box.createHorizontalStrut(3));
            box4.add(btLeft2);
            box4.add(Box.createHorizontalStrut(3));
            box4.add(btRight2);
            box4.add(Box.createHorizontalStrut(3));
            box4.add(btRight);
            box4.add(Box.createHorizontalStrut(10));
     
            //Set box borders
            box1.setBorder(new javax.swing.border.LineBorder(Color.red));
            box2.setBorder(new javax.swing.border.LineBorder(Color.black));
     
            //Set box layouts 
            add(box1, BorderLayout.CENTER);
            add(box2, BorderLayout.CENTER);
            add(NewLine, BorderLayout.CENTER);
            add(box3, BorderLayout.EAST);
            add(box4, BorderLayout.CENTER);
     
            //Add an action listener to buttons & text fields
            btNew.addActionListener(this);
            btSave.addActionListener(this);
            btDelete.addActionListener(this);
            btFind.addActionListener(this);
            btExit.addActionListener(this);
            //close program
                   // System.exit(0);
       }
     
     
     
       public void paint(Graphics g)
       {
            g.setColor(Color.white);
            g.drawString("This program will allow the user to add", 10, 20);
            g.drawString("edit or delete Users.their PC Identifiers", 10, 32);
            g.drawString("and their PC's IP Address.", 50, 44);
     
     
     
       }    
     
        public void actionPerformed(ActionEvent evt)
        {  
            if (evt.getSource() == btSave)  { strSave = "true"; }
     
            //execute following code if strSave is set to true
            if(strSave == "true")
            {
                //Get text entered from textfields and set them as string vars set below
              strName = txtName.getText();
              strPCI = txtPCI.getText();
              strIPaddress = txtIP.getText();
     
              //set array values to text entered
              arrNames[0] = strName;
              arrNames[1] = strPCI;
              arrNames[2] = strIPaddress;
     
              try 
                        {
                     Writer output = null;                   
                        output = new BufferedWriter(new FileWriter(mainFile));
                        //Write array 0,1 & 2 data to file
                        output.write(arrNames[0]);
                        output.write(arrNames[1]);
                        output.write(arrNames[2]);
                        output.close();
                        }
                        catch(Exception sfo)
                        {
                        System.out.println("Your file has been written"); 
                        }  
            }
     
            repaint();
        }
      }

    hope somebody can help with a solution


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: button action problem


  3. #3
    Member
    Join Date
    Mar 2011
    Posts
    198
    My Mood
    Daring
    Thanks
    7
    Thanked 4 Times in 4 Posts

    Default Re: button action problem

    Quote Originally Posted by copeg View Post
    thanks for fast reply, Erm under which section. Dont feel like going through it all right now i have a perliminary report to complete and a c# prototype to start

  4. #4
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: button action problem

    That link should scroll you to the correct section. If not, see the section regarding == versus equals()

  5. The Following User Says Thank You to copeg For This Useful Post:

    macko (March 29th, 2011)

  6. #5
    Member
    Join Date
    Mar 2011
    Posts
    198
    My Mood
    Daring
    Thanks
    7
    Thanked 4 Times in 4 Posts

    Default Re: button action problem

    thanks mate

Similar Threads

  1. Problem with JText Fields and using action listener
    By toble in forum AWT / Java Swing
    Replies: 2
    Last Post: October 27th, 2010, 04:44 PM
  2. converter program, problem with action listener
    By robertson.basil in forum What's Wrong With My Code?
    Replies: 2
    Last Post: October 2nd, 2010, 05:44 AM
  3. Action button HELP?
    By utoptas in forum Java Theory & Questions
    Replies: 8
    Last Post: August 27th, 2010, 03:32 PM
  4. Action from Radio Button
    By halfwaygone in forum What's Wrong With My Code?
    Replies: 0
    Last Post: April 25th, 2010, 10:52 AM
  5. Problem with Action Listener
    By JonoScho in forum AWT / Java Swing
    Replies: 4
    Last Post: March 19th, 2010, 01:03 AM