import java.io.*; import java.awt.*; import java.awt.event.*; import java.applet.Applet; import java.lang.*; import javax.swing.filechooser.FileFilter; import javax.swing.*; public class uusredaktor extends JApplet implements ActionListener,TextListener{ String failinimi; int akendeArv=1; TextArea tekstivali1=new TextArea(); TextArea tekstivali2=new TextArea(); TextArea tekstivali3=new TextArea(); TextArea tekstivali4=new TextArea(); TextArea tekstivali5=new TextArea(); TextArea tekstivali6=new TextArea(); JButton uus=new JButton("Uus"); JButton ava=new JButton("Ava"); JButton sulge=new JButton("Sulge"); JButton salvesta=new JButton("Salvesta"); JTabbedPane paneel2=new JTabbedPane(); JPanel p1=new JPanel(new GridLayout(1,3)); JSplitPane paneel=new JSplitPane(JSplitPane.VERTICAL_SPLIT,p1,paneel2); TextArea valjad[]=new TextArea[]{tekstivali1, tekstivali2,tekstivali3,tekstivali4,tekstivali5,tekstivali6}; public uusredaktor(){ ava.addActionListener(this); salvesta.addActionListener(this); sulge.addActionListener(this); uus.addActionListener(this); tekstivali1.addTextListener(this); tekstivali2.addTextListener(this); tekstivali3.addTextListener(this); tekstivali4.addTextListener(this); tekstivali5.addTextListener(this); tekstivali6.addTextListener(this); uus.setEnabled(true); ava.setEnabled(true); sulge.setEnabled(false); salvesta.setEnabled(false); p1.add(uus); p1.add(ava); p1.add(sulge); p1.add(salvesta); getContentPane().add(paneel); } public void avaAken(String failinimi, int number, String rida) { if (failinimi.length()>0) { paneel2.add(failinimi,valjad[number]); valjad[number].setText(rida+"\n"); } } public void sulgeAken(int number) { paneel2.remove(number); } public void textValueChanged(TextEvent m){ if(paneel2.getTabCount()>0){ sulge.setEnabled(true); salvesta.setEnabled(true); } if(paneel2.getTabCount()==6){ uus.setEnabled(false); ava.setEnabled(false);} } public void actionPerformed (ActionEvent e) { if(e.getSource()==ava) { JFileChooser valija=new JFileChooser(new File(".")); valija.addChoosableFileFilter(new Pildifilter()); valija.showDialog(new JFrame(), "Ava"); System.out.println("Valiti "+valija.getSelectedFile()); try{ BufferedReader sisse=new BufferedReader( new FileReader(valija.getSelectedFile())); String tulemus=" "; for(String rida=sisse.readLine(); rida!=null; rida=sisse.readLine()) { tulemus=tulemus+rida+"\n"; } System.out.println(tulemus); avaAken(valija.getSelectedFile().getName(), paneel2.getTabCount(),tulemus); sisse.close(); }catch(IOException viga){viga.printStackTrace();} } if(e.getSource()==salvesta){ JFileChooser valija3=new JFileChooser(new File(".")); valija3.addChoosableFileFilter(new Pildifilter()); valija3.showDialog(new JFrame(), "Salvesta"); System.out.println("Valiti "+valija3.getSelectedFile()); try{ PrintWriter faili=new PrintWriter( new FileWriter(valija3.getSelectedFile())); faili.println(valjad[paneel2.getSelectedIndex()].getText()); faili.close(); }catch(IOException viga){viga.printStackTrace();} } if (e.getSource()==sulge) { if (paneel2.getTabCount()>0) { sulgeAken(paneel2.getSelectedIndex()); } if(paneel2.getTabCount()<6){ uus.setEnabled(true); ava.setEnabled(true);} if(paneel2.getTabCount()==0) {sulge.setEnabled(false); salvesta.setEnabled(false); } } if (e.getSource()==uus) { avaAken("Nimetu", (paneel2.getTabCount()),""); } } class Pildifilter extends FileFilter { public boolean accept(File f) { String failinimi=f.getName(); if(failinimi.endsWith(".txt")) return true; else return false; } public String getDescription() { return ".txt "; } } public static void main(String argumendid[]) { JFrame f=new JFrame("Redaktor"); f.getContentPane().add(new uusredaktor()); f.setSize(500, 300); f.setVisible(true); } }