Advertisement
Codex

Search a word

Jul 29th, 2011
184
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.53 KB | None | 0 0
  1. import java.awt.*;
  2. import java.awt.event.*;
  3. import javax.swing.*;
  4. import java.io.*;
  5. class Search extends JFrame implements ActionListener
  6. {
  7. JTextField word;
  8. JTextArea meaning;
  9. JLabel wordLabel,meaningLabel;
  10. JButton search,back;
  11. FileReader fr;
  12. BufferedReader bra;
  13. public Search()
  14. {
  15. super("Dictionary");
  16. word=new JTextField(20);
  17. meaning=new JTextArea(2,20);
  18. meaning.setEditable(false);
  19.  
  20. wordLabel=new JLabel("Word");
  21. meaningLabel=new JLabel("Meaning");
  22.  
  23. search=new JButton("Search");
  24. search.setActionCommand("Search");
  25. search.addActionListener(this);
  26.  
  27. back=new JButton("Back");
  28. back.setActionCommand("Back");
  29. back.addActionListener(this);
  30.  
  31. GridBagLayout gbag=new GridBagLayout();
  32. GridBagConstraints gbc = new GridBagConstraints();
  33. setLayout(gbag);
  34.  
  35. gbc.weightx = 0.0; // use a column weight of 1
  36. gbc.ipadx = 5; // pad by 5 units
  37. gbc.insets = new Insets(4, 4, 0, 0); //inset slightly from top left
  38. gbc.anchor = GridBagConstraints.CENTER;
  39. gbc.gridwidth = GridBagConstraints.RELATIVE;
  40. gbag.setConstraints(wordLabel, gbc);
  41.  
  42. gbc.gridwidth = GridBagConstraints.REMAINDER;
  43. gbag.setConstraints(word, gbc);
  44.  
  45. gbc.weighty = 0.0;
  46. gbc.weightx = 0.0;
  47. gbc.gridwidth = GridBagConstraints.RELATIVE;
  48. gbag.setConstraints(meaningLabel,gbc);
  49.  
  50. gbc.gridwidth = GridBagConstraints.REMAINDER;
  51. gbag.setConstraints(meaning,gbc);
  52.  
  53.  
  54. gbc.weightx = 0.0;
  55. gbc.weighty = 0.0;
  56. gbc.gridwidth = GridBagConstraints.REMAINDER;
  57. gbag.setConstraints(search,gbc);
  58.  
  59. gbc.gridwidth = GridBagConstraints.REMAINDER;
  60. gbag.setConstraints(back,gbc);
  61.  
  62.  
  63.  
  64. add(wordLabel);
  65. add(word);
  66. add(meaningLabel);
  67. add(meaning);
  68.  
  69. add(search);
  70. add(back);
  71.  
  72. setSize(400, 400);
  73. setVisible(true);
  74. setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  75. }
  76. public void actionPerformed(ActionEvent ae)
  77. {
  78. if((ae.getActionCommand()).equals("Back"))
  79. {
  80. setVisible(false);
  81. new Dictionary();
  82. }
  83. else if((ae.getActionCommand()).equals("Search"))
  84. {
  85. boolean b=search(word.getText());
  86. if(!(b))
  87. {
  88. JOptionPane.showMessageDialog(this,"Error reading from the file","Inane error",
  89. JOptionPane.ERROR_MESSAGE);
  90. }
  91.  
  92. }
  93. }
  94. public boolean search(String w)
  95. {
  96. try
  97. {
  98. fr=new FileReader("Data.txt");
  99. bra=new BufferedReader(fr);
  100. }
  101. catch(Exception e)
  102. {
  103. return false;
  104. }
  105. boolean founded=false;
  106. String s;
  107. String s2[]=new String[2];
  108. try
  109. {
  110. while((s=bra.readLine())!=null)
  111. {
  112. s2=s.split(":");
  113. if(w.equalsIgnoreCase(s2[0]))
  114. {
  115. meaning.append(s2[1]+"\n");
  116. founded=true;
  117. }
  118. }
  119. }
  120. catch(IOException e)
  121. {
  122. return false;
  123. }
  124. if(!(founded))
  125. meaning.setText("No result found");
  126. return true;
  127. }
  128. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement