Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- Add class of Dictionary.java
- */
- import java.io.*;
- import java.awt.*;
- import java.awt.event.*;
- import javax.swing.*;
- class Add extends JFrame implements ActionListener
- {
- JTextField word,meaning;
- JLabel wordLabel,meaningLabel;
- JButton add,back;
- FileWriter fw;
- BufferedWriter bw;
- PrintWriter pw;
- public Add()
- {
- super("Dictionary");
- word=new JTextField(20);
- meaning=new JTextField(20);
- /*meaning.setLineWrap(true);
- meaning.setWrapStyleWord(true);
- JScrollPane meaningScrollPane = new JScrollPane(meaning);
- meaningScrollPane.setVerticalScrollBarPolicy(
- JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
- meaningScrollPane.setPreferredSize(new Dimension(250, 250));*/
- wordLabel=new JLabel("Word");
- meaningLabel=new JLabel("Meaning");
- add=new JButton("Add");
- add.setActionCommand("Add");
- add.addActionListener(this);
- back=new JButton("Back");
- back.setActionCommand("Back");
- back.addActionListener(this);
- GridBagLayout gbag=new GridBagLayout();
- GridBagConstraints gbc = new GridBagConstraints();
- setLayout(gbag);
- gbc.weightx = 0.0; // use a column weight of 1
- gbc.ipadx = 5; // pad by 5 units
- gbc.insets = new Insets(4, 4, 0, 0); //inset slightly from top left
- gbc.anchor = GridBagConstraints.CENTER;
- gbc.gridwidth = GridBagConstraints.RELATIVE;
- gbag.setConstraints(wordLabel, gbc);
- gbc.gridwidth = GridBagConstraints.REMAINDER;
- gbag.setConstraints(word, gbc);
- gbc.weighty = 0.0;
- gbc.weightx = 0.0;
- gbc.gridwidth = GridBagConstraints.RELATIVE;
- gbag.setConstraints(meaningLabel,gbc);
- gbc.gridwidth = GridBagConstraints.REMAINDER;
- gbag.setConstraints(meaning,gbc);
- gbc.weightx = 0.0;
- gbc.weighty = 0.0;
- gbc.gridwidth = GridBagConstraints.REMAINDER;
- gbag.setConstraints(add,gbc);
- gbc.gridwidth = GridBagConstraints.REMAINDER;
- gbag.setConstraints(back,gbc);
- add(wordLabel);
- add(word);
- add(meaningLabel);
- add(meaning);
- add(add);
- add(back);
- setSize(400, 400);
- setVisible(true);
- setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- }
- public void actionPerformed(ActionEvent ae)
- {
- if((ae.getActionCommand()).equals("Back"))
- {
- setVisible(false);
- new Dictionary();
- }
- else if((ae.getActionCommand()).equals("Add"))
- {
- boolean b=write(word.getText(),meaning.getText());
- if(!(b))
- {
- JOptionPane.showMessageDialog(this,"Error writing to the file","Inane error",
- JOptionPane.ERROR_MESSAGE);
- }
- word.setText("");
- meaning.setText("");
- }
- }
- public boolean write(String w,String m)
- {
- if(!(isExisting(w,m)))
- {
- try
- {
- fw=new FileWriter("Data.txt",true);
- bw=new BufferedWriter(fw);
- pw=new PrintWriter(bw);
- pw.println(w+":"+m);
- pw.close();
- bw.close();
- fw.close();
- }
- catch(IOException e)
- {
- return false;
- }
- }
- return true;
- }
- boolean isExisting(String wo,String me)
- {
- FileReader fre;
- BufferedReader bre;
- try
- {
- fre=new FileReader("Data.txt");
- bre=new BufferedReader(fre);
- }
- catch(Exception e)
- {
- return false;
- }
- boolean existing=false;
- String s;
- String s2[]=new String[2];
- try
- {
- while((s=bre.readLine())!=null)
- {
- s2=s.split(":");
- if(wo.equalsIgnoreCase(s2[0]) && me.equalsIgnoreCase(s2[1]))
- {
- existing=true;
- }
- }
- }
- catch(IOException e)
- {
- return false;
- }
- return existing;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement