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..
Code Java: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

