Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.awt.GridBagConstraints;
- import java.awt.GridBagLayout;
- import java.awt.event.ActionEvent;
- import java.awt.event.ActionListener;
- import java.io.BufferedInputStream;
- import java.io.BufferedReader;
- import java.io.File;
- import java.io.FileInputStream;
- import java.io.FileNotFoundException;
- import java.io.IOException;
- import java.io.InputStream;
- import java.io.InputStreamReader;
- import java.io.PrintWriter;
- import java.text.SimpleDateFormat;
- import java.util.Calendar;
- import javax.swing.JFrame;
- import javax.swing.JPanel;
- import javax.swing.JScrollPane;
- import javax.swing.JTextArea;
- import javax.swing.JTextField;
- import javax.swing.UIManager;
- import javax.swing.UnsupportedLookAndFeelException;
- public class display extends JPanel implements ActionListener
- {
- protected static JTextField textField;
- protected JTextArea textArea;
- private final static String newline = "\n";
- String dir = System.getProperty("user.dir");
- static File file = new File("chat.html");
- static boolean exists = (file).exists();
- public static final String DATE_FORMAT_NOW = "HH:mm:ss";
- public static String now()
- {
- Calendar cal = Calendar.getInstance();
- SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT_NOW);
- return sdf.format(cal.getTime());
- }
- private static void createAndShowGUI() throws ClassNotFoundException, InstantiationException, IllegalAccessException, UnsupportedLookAndFeelException
- {
- //Create and set up the window.
- JFrame.setDefaultLookAndFeelDecorated(true);
- UIManager.setLookAndFeel(UIManager.getCrossPlatformLookAndFeelClassName());
- JFrame frame = new JFrame("Chat");
- frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- //Add contents to the window.
- frame.add(new TestHelp());
- //TestHelp the window.
- frame.pack();
- frame.setVisible(true);
- }
- public void actionPerformed(ActionEvent event)
- {
- //the code it executes when the button is pressed
- boolean exists = (file).exists();
- byte[] c = new byte[1024];
- int count = 0;
- int readChars = 0;
- if(exists)
- {
- try
- {
- String input = textField.getText();
- if(input.length() != 0)
- {
- InputStream is = new BufferedInputStream(new FileInputStream(file));
- FileInputStream in = new FileInputStream("chat.html");
- BufferedReader br = new BufferedReader(new InputStreamReader(in));
- while ((readChars = is.read(c)) != -1)
- {
- for (int i = 0; i < readChars; ++i)
- {
- if (c[i] == '\n')
- ++count;
- }
- }
- String[] read;
- read = new String[count];
- for (int counter = 0; counter < read.length; counter++)
- {
- read[counter] = br.readLine();
- }
- in.close();
- PrintWriter prnt = new PrintWriter(file);
- for(int index=0;index<read.length; index++)
- {
- //Print the old file contents
- prnt.println(read[index]);
- }
- prnt.println("<" +now() +">" +input);
- prnt.close();
- }
- }
- catch(NullPointerException e)
- {
- e.printStackTrace();
- }
- catch(FileNotFoundException e)
- {
- e.printStackTrace();
- }
- catch(IOException e)
- {
- e.printStackTrace();
- }
- }
- else//if it does not exist
- {
- }
- }
- public static void main(String[] args) throws FileNotFoundException{
- //if(!exists){//if chat.html doesn't exists,
- //PrintWriter prnt = new PrintWriter(file);
- //prnt.println("");
- //prnt.close();
- //}
- //Schedule a job for the event dispatch thread:
- //creating and showing this application's GUI.
- javax.swing.SwingUtilities.invokeLater(new Runnable()
- {
- public void run()
- {
- try
- {
- createAndShowGUI();
- }
- catch (ClassNotFoundException e)
- {
- e.printStackTrace();
- }
- catch (InstantiationException e)
- {
- e.printStackTrace();
- }
- catch (IllegalAccessException e)
- {
- e.printStackTrace();
- }
- catch (UnsupportedLookAndFeelException e)
- {
- e.printStackTrace();
- }
- }
- });
- //else{}//if it exists, no use yet.
- }
- public display()
- {
- super(new GridBagLayout());
- textField = new JTextField(20);
- textField.addActionListener(this);
- textArea = new JTextArea(5, 20);
- textArea.setEditable(false);
- JScrollPane scrollPane = new JScrollPane(textArea);
- //Add Components to this panel.
- GridBagConstraints c = new GridBagConstraints();
- c.gridwidth = GridBagConstraints.REMAINDER;
- c.fill = GridBagConstraints.HORIZONTAL;
- add(textField, c);
- c.fill = GridBagConstraints.BOTH;
- c.weightx = 1.0;
- c.weighty = 1.0;
- add(scrollPane, c);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement