Hey all
Basically i've got a question about how to put two classes together if that makes any sense
The code below is for a GUI that uses a drop down menu.
The menu allows the user to open a file and also gives the user the option of saving a file.
This i shown in the code below:
import java.awt.*; import java.awt.event.*; import java.io.*; import java.util.regex.*; import javax.swing.*; public class Editor extends JFrame implements ActionListener { public static void main(String[] s) { new Editor( ); } private JEditorPane textPane = new JEditorPane( ); public Editor( ) { super("Editor v1.0"); addWindowListener(new WindowAdapter( ) { public void windowClosing(WindowEvent e) { System.exit(0); } }); Container content = getContentPane( ); content.add(new JScrollPane(textPane), BorderLayout.CENTER); JMenu menu = new JMenu("File"); menu.add(makeMenuItem("Open")); menu.add(makeMenuItem("Clean")); menu.add(makeMenuItem("Extract")); menu.add(makeMenuItem("Save")); menu.add(makeMenuItem("Quit")); JMenuBar menuBar = new JMenuBar( ); menuBar.add(menu); setJMenuBar(menuBar); setSize(300, 300); setLocation(200, 200); setVisible(true); } public void actionPerformed(ActionEvent e) { String command = e.getActionCommand( ); if (command.equals("Quit")) System.exit(0); else if (command.equals("Open")) loadFile( ); else if (command.equals("Save")) saveFile( ); } private void loadFile ( ) { JFileChooser chooser = new JFileChooser( ); int result = chooser.showOpenDialog(this); if (result == JFileChooser.CANCEL_OPTION) return; try { File file = chooser.getSelectedFile( ); java.net.URL url = file.toURL( ); textPane.setPage(url); } catch (Exception e) { textPane.setText("Could not load file: " + e); } } private void saveFile( ) { JFileChooser chooser = new JFileChooser( ); int result = chooser.showSaveDialog(this); // Save file data... if (result == JFileChooser.CANCEL_OPTION) return; // if user click on Save button File file = chooser.getSelectedFile(); try { BufferedWriter bw = new BufferedWriter(new FileWriter(file)); bw.write(textPane.getText()); bw.close(); } catch (Exception e) { JOptionPane.showMessageDialog( this, e.getMessage(), "File Error", JOptionPane.ERROR_MESSAGE ); } } private JMenuItem makeMenuItem( String name ) { JMenuItem m = new JMenuItem( name ); m.addActionListener( this ); return m; } }
However i've also got another program that removes any whitespace from the xml tag headers contained within a file.
The code for this is below:
import java.util.regex.*; import java.io.*; public class regularexpressions{ public static void main(String[] args) throws IOException{ BufferedReader bf = new BufferedReader(new InputStreamReader(System.in)); System.out.print("Enter file name: "); String filename = bf.readLine(); File file = new File(filename); if(!filename.endsWith(".txt")){ System.out.println("Usage: This is not a text file!"); System.exit(0); } else if(!file.exists()){ System.out.println("File not found!"); System.exit(0); } FileInputStream fstream = new FileInputStream(filename); DataInputStream in = new DataInputStream(fstream); BufferedReader br = new BufferedReader(new InputStreamReader(in)); Pattern p; Matcher m; String afterReplace = ""; String strLine; String inputText = ""; while ((strLine = br.readLine()) != null) if (strLine.contains("< d v d T i t l e >")) { System.out.println (strLine); inputText = strLine; p = Pattern.compile("\\s+"); m = p.matcher(inputText); System.out.println(afterReplace); afterReplace = afterReplace + m.replaceAll("") + "\r\n"; } else if (strLine.contains("< d v _ i d >")) { System.out.println (strLine); inputText = strLine; p = Pattern.compile("\\s+"); m = p.matcher(inputText); System.out.println(afterReplace); afterReplace = afterReplace + m.replaceAll("") + "\r\n"; } else if (strLine.contains("< / M e t a d a t a P r o v i d e r T i m e >")) { System.out.println (strLine); inputText = strLine; p = Pattern.compile("\\s+"); m = p.matcher(inputText); System.out.println(afterReplace); afterReplace = afterReplace + m.replaceAll("") + "\r\n"; } else if (strLine.contains("< a l b u m A r t i s t >")) { System.out.println (strLine); inputText = strLine; p = Pattern.compile("\\s+"); m = p.matcher(inputText); System.out.println(afterReplace); afterReplace = afterReplace + m.replaceAll("") + "\r\n"; } else if (strLine.contains("< a l b u m T i t l e >")) { System.out.println (strLine); inputText = strLine; p = Pattern.compile("\\s+"); m = p.matcher(inputText); System.out.println(afterReplace); afterReplace = afterReplace + m.replaceAll("") + "\r\n"; } FileWriter fstream1 = new FileWriter(filename); BufferedWriter out = new BufferedWriter(fstream1); out.write(afterReplace); in.close(); out.close(); } }
In the gui there is also some other options for the user to choose, one of which is 'clean'.
Basically what i want to do is link this program with the command 'clean' from the drop down menu in the gui if that makes sense.
At the moment this program just allows the user to input and output to the console.
Hoever i'm just curious how can i enable the Editor class to call on the regularexpressions class whenever the user selects clean form the drop down menu.
I hope i've made some sense lol
Any help is much appreciated.
Thanks
John