Advertisement
Codex

Add a word

Jul 28th, 2011
126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.12 KB | None | 0 0
  1. /*
  2. Add class of Dictionary.java
  3. */
  4. import java.io.*;
  5. import java.awt.*;
  6. import java.awt.event.*;
  7. import javax.swing.*;
  8. class Add extends JFrame implements ActionListener
  9. {
  10. JTextField word,meaning;
  11. JLabel wordLabel,meaningLabel;
  12. JButton add,back;
  13. FileWriter fw;
  14. BufferedWriter bw;
  15. PrintWriter pw;
  16. public Add()
  17. {
  18. super("Dictionary");
  19. word=new JTextField(20);
  20. meaning=new JTextField(20);
  21. /*meaning.setLineWrap(true);
  22. meaning.setWrapStyleWord(true);
  23. JScrollPane meaningScrollPane = new JScrollPane(meaning);
  24. meaningScrollPane.setVerticalScrollBarPolicy(
  25.         JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
  26. meaningScrollPane.setPreferredSize(new Dimension(250, 250));*/
  27. wordLabel=new JLabel("Word");
  28. meaningLabel=new JLabel("Meaning");
  29. add=new JButton("Add");
  30. add.setActionCommand("Add");
  31. add.addActionListener(this);
  32. back=new JButton("Back");
  33. back.setActionCommand("Back");
  34. back.addActionListener(this);
  35.  
  36. GridBagLayout gbag=new GridBagLayout();
  37. GridBagConstraints gbc = new GridBagConstraints();
  38. setLayout(gbag);
  39.  
  40. gbc.weightx = 0.0; // use a column weight of 1
  41. gbc.ipadx = 5; // pad by 5 units
  42. gbc.insets = new Insets(4, 4, 0, 0); //inset slightly from top left
  43. gbc.anchor = GridBagConstraints.CENTER;
  44. gbc.gridwidth = GridBagConstraints.RELATIVE;
  45. gbag.setConstraints(wordLabel, gbc);
  46.  
  47. gbc.gridwidth = GridBagConstraints.REMAINDER;
  48. gbag.setConstraints(word, gbc);
  49.  
  50. gbc.weighty = 0.0;
  51. gbc.weightx = 0.0;
  52. gbc.gridwidth = GridBagConstraints.RELATIVE;
  53. gbag.setConstraints(meaningLabel,gbc);
  54.  
  55. gbc.gridwidth = GridBagConstraints.REMAINDER;
  56. gbag.setConstraints(meaning,gbc);
  57.  
  58.  
  59. gbc.weightx = 0.0;
  60. gbc.weighty = 0.0;
  61. gbc.gridwidth = GridBagConstraints.REMAINDER;
  62. gbag.setConstraints(add,gbc);
  63.  
  64. gbc.gridwidth = GridBagConstraints.REMAINDER;
  65. gbag.setConstraints(back,gbc);
  66.  
  67.  
  68.  
  69. add(wordLabel);
  70. add(word);
  71. add(meaningLabel);
  72. add(meaning);
  73.  
  74. add(add);
  75. add(back);
  76.  
  77. setSize(400, 400);
  78. setVisible(true);
  79. setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  80. }
  81. public void actionPerformed(ActionEvent ae)
  82. {
  83. if((ae.getActionCommand()).equals("Back"))
  84. {
  85. setVisible(false);
  86. new Dictionary();
  87. }
  88. else if((ae.getActionCommand()).equals("Add"))
  89. {
  90.  
  91. boolean b=write(word.getText(),meaning.getText());
  92. if(!(b))
  93. {
  94. JOptionPane.showMessageDialog(this,"Error writing to the file","Inane error",
  95. JOptionPane.ERROR_MESSAGE);
  96. }
  97. word.setText("");
  98. meaning.setText("");
  99.  
  100. }
  101. }
  102. public boolean write(String w,String m)
  103. {
  104. if(!(isExisting(w,m)))
  105. {
  106. try
  107. {
  108. fw=new FileWriter("Data.txt",true);
  109. bw=new BufferedWriter(fw);
  110. pw=new PrintWriter(bw);
  111. pw.println(w+":"+m);
  112. pw.close();
  113. bw.close();
  114. fw.close();
  115. }
  116. catch(IOException e)
  117. {
  118. return false;
  119. }
  120. }
  121.  
  122. return true;
  123. }
  124. boolean isExisting(String wo,String me)
  125. {
  126. FileReader fre;
  127. BufferedReader bre;
  128. try
  129. {
  130. fre=new FileReader("Data.txt");
  131. bre=new BufferedReader(fre);
  132. }
  133. catch(Exception e)
  134. {
  135. return false;
  136. }
  137. boolean existing=false;
  138. String s;
  139. String s2[]=new String[2];
  140. try
  141. {
  142. while((s=bre.readLine())!=null)
  143. {
  144. s2=s.split(":");
  145. if(wo.equalsIgnoreCase(s2[0]) && me.equalsIgnoreCase(s2[1]))
  146. {
  147. existing=true;
  148. }
  149. }
  150. }
  151. catch(IOException e)
  152. {
  153. return false;
  154. }
  155. return existing;
  156. }
  157. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement