First of all, i got two class which is Owner class and OwnerFrame.
Owner code:
package virtuPet; import java.io.File; import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; import java.io.PrintWriter; import java.util.HashMap; import java.util.Map; import java.util.Scanner; import java.util.Set; public class Owner { private String name, userName; private HashMap <String, String> owner = new HashMap <String, String>(); public void setOwnerName(String name) { this.name = name; } public String getOwnerName() { return name; } public void setUserName(String userName) { this.userName = userName; } public String getUserName() { return userName; } public void addOwner(String name, String userName) { owner.put ( name, userName); } public void saveOwner() throws IOException { File outFile = new File(userName+".txt"); // locating the data destination FileWriter aFileWriter = new FileWriter(outFile,true); PrintWriter aPrintWriter = new PrintWriter(aFileWriter); StringBuilder outputSB = new StringBuilder(); Set<Map.Entry<String, String>> setOwner = owner.entrySet(); // Set<Map.Entry<Integer,Pet >> setPet = pets.entrySet(); for (Map.Entry<String, String> me : setOwner) { outputSB.append (""+me.getKey()+ "; " + me.getValue()+""); } } }
OwnerFrame code:
package virtuPet; import java.io.*; import java.awt.*; import javax.swing.*; import java.awt.event.*; public class OwnerFrame extends JFrame { private static final int WIDTH = 350; private static final int HEIGHT = 150; private JLabel nameLabel, userNameLabel; private JTextField nameTextField, userNameTextField; private JButton createOwnerB, cancelB; private CreateOwnerButtonHandler cHandler; private CancelButtonHandler cbHandler; public OwnerFrame() { setTitle("Create New Owner"); setSize(WIDTH, HEIGHT); Container pane = getContentPane(); GridLayout aGrid = new GridLayout(3,2); pane.setLayout(aGrid); nameLabel = new JLabel("Name"); userNameLabel = new JLabel("User Name"); nameTextField = new JTextField(25); userNameTextField = new JTextField(15); createOwnerB = new JButton("Create Owner"); cHandler = new CreateOwnerButtonHandler(); createOwnerB.addActionListener(cHandler); cancelB = new JButton("Cancel"); cbHandler = new CancelButtonHandler(); cancelB.addActionListener(cbHandler); pane.add(nameLabel); pane.add(nameTextField); pane.add(userNameLabel); pane.add(userNameTextField); pane.add(createOwnerB); pane.add(cancelB); } private class CreateOwnerButtonHandler implements ActionListener { public void actionPerformed(ActionEvent e) { /*I am trying to solve how to put the OwnerName and Username value *in the HashMap in the onwer class...i did trough method but the file is created but no data *of the OwnerName and UserName in the text file*/ Owner a = new Owner(); a.setOwnerName(nameTextField.getText()); a.setUserName(userNameTextField.getText()); a.getOwnerName(); a.getUserName(); a.addOwner(a.getOwnerName(), a.getUserName()); try { a.saveOwner(); } catch (IOException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } setVisible(false); JFrame aPetFrame = new PetFrame(); aPetFrame.setVisible(true); } } private class CancelButtonHandler implements ActionListener { public void actionPerformed(ActionEvent e) { System.exit(0); } } public static void main(String[] args) { JFrame aOwnerFrame = new OwnerFrame(); aOwnerFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); aOwnerFrame.setVisible(true); } }
can someone help? i just need to solved on how to put input from textField to hashMap key and value.