Originally posted by tamara omar
Code:
import javax.swing.*; import javax.swing.text.*; import java.awt.event.*; import java.awt.Container; import java.io.*; import java.awt.*; //import java.util.Scanner; public class Tamara2 extends JFrame implements ActionListener { //is allowed to listen for actions private Container pane; JTextArea area; JFileChooser fc; JPanel southPanel; JPanel northPanel; JScrollPane scrollPane; static private final String newline = "\n"; public Tamara2() { pane = getContentPane(); //pane will now replace getContentPane() from now onwards setTitle("Tamara Omar"); setSize(400, 400); // fc = new JFileChooser(); area = new JTextArea(10,20); scrollPane = new JScrollPane(area); area.setMargin(new Insets(5,5,5,5)); area.setEditable(true); //make buttons JButton open = makeButton("Open"); JButton save = makeButton("Save"); JButton edit = makeButton("Find"); JButton edit1= makeButton("find all"); JButton replace1=makeButton("replace all"); JButton replace = makeButton("Replace"); southPanel = new JPanel(); northPanel = new JPanel(); //add the buttons to the panel southPanel.add(edit1); southPanel.add(replace1); southPanel.add(edit); southPanel.add(replace); //add the panel to the frame at the bottom northPanel.add(open); northPanel.add(save); pane.add(northPanel, "North"); pane.add(scrollPane, "Center"); pane.add(southPanel, "South"); setVisible(true); } public JButton makeButton(String label) { //creates a button JButton jb = new JButton(label); jb.setActionCommand(label); //This message will be set in the action for the button jb.addActionListener(this); //this (JFrame) will respond to this button's press return jb; } public void actionPerformed(ActionEvent action) { String command = action.getActionCommand(); if(command.equals("Find")) { JOptionPane.showMessageDialog(this, "Find"); } else if(command.equals("Replace")) { JOptionPane.showMessageDialog(this, "Replace"); } else if(command.equals("replace all")) { JOptionPane.showMessageDialog(this,"replace all"); } else if(command.equals("find all")) { JOptionPane.showMessageDialog(this,"replace all"); } else if(command.equals("Open")) { int returnVal = fc.showOpenDialog(Tamara2.this); if (returnVal == JFileChooser.APPROVE_OPTION) { File file = fc.getSelectedFile(); //This is where a real application would open the file. //Scanner inFile = null; BufferedReader inFile = null; try { inFile = new BufferedReader(new FileReader(file)); String line = ""; while ((line = inFile.readLine()) != null) { //String line = inFile.readLine(); area.append(line + newline); } inFile.close(); } catch(IOException iOE) { iOE.printStackTrace(); } } else { area.append("Open command cancelled by user." + newline); } area.setCaretPosition(area.getDocument().getLength()); } else if(command.equals("Save")) { try { int returnVal = fc.showSaveDialog(Tamara2.this); if(returnVal == JFileChooser.APPROVE_OPTION) { File file = fc.getSelectedFile(); //FileOutputStream fos = new FileOutputStream(file); //DataOutputStream dos = new DataOutputStream(fos); BufferedWriter out = new BufferedWriter(new FileWriter(file)); //This is where a real application would save the file. int rows = area.getLineCount(); for(int i = 0; i < rows; i++) { int end = area.getLineEndOffset(i); int start = area.getLineStartOffset(i); int length = end - start; if(length <= 0) { length = 1; } String s = area.getText(start, (length-1)); System.out.println(s); out.write(s); out.newLine(); out.flush(); } out.close(); area.append("Saving: " + file.getName() + "." + newline); } else { area.append("Save command cancelled by user." + newline); } } catch(Exception e) { e.printStackTrace(); } area.setCaretPosition(area.getDocument().getLength()); } } public static void main(String[] args) { new Tamara2(); } }
Comment