Reputation: 41
Here is all of the code, you can Copy & Paste it into your Java programming software. I developed it in Eclipse. Paste the line you modified/added.
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.text.DefaultCaret;
import javax.swing.border.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.geom.*;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JScrollBar;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
public class Notepad extends JFrame implements ActionListener {
/**
*
*/
private static final long serialVersionUID = 1L;
private JMenuBar menuBar;
private JMenu menuFile;
private JMenu menuEdit;
private JMenuItem open;
private JMenuItem save;
private JMenuItem exit;
private JMenuItem copy;
private JMenuItem paste;
private JTextArea page;
Notepad(){
super("Notepad");
init();
this.setSize(1024, 768);
this.setVisible(true);
}//end Notepad
private void init(){
//create text area
page = new JTextArea();
page.setCaretPosition(page.getDocument().getLength());page.setCaretPosition(page.getDocument().getLength());page.setCaretPosition(page.getDocument().getLength());page.setCaretPosition(page.getDocument().getLength());page.setCaretPosition(page.getDocument().getLength());page.setCaretPosition(page.getDocument().getLength());page.setCaretPosition(page.getDocument().getLength());page.setCaretPosition(page.getDocument().getLength());page.setCaretPosition(page.getDocument().getLength());
page.setEditable(true);
page.setFont(new Font("Comic Sans MS", Font.BOLD, 20));
page.setLineWrap(true);
//create menus
menuFile = new JMenu("File");
menuEdit = new JMenu("Edit");
//create menu items
open = new JMenuItem("Open");
save = new JMenuItem("Save");
exit = new JMenuItem("Exit");
copy = new JMenuItem("Copy");
paste = new JMenuItem("Paste");
//add listener to menu items
exit.addActionListener(this);
copy.addActionListener(this);
paste.addActionListener(this);
save.addActionListener(this);
open.addActionListener(this);
//add items to menus
menuFile.add(open);
menuFile.add(save);
menuFile.add(exit);
menuEdit.add(copy);
menuEdit.add(paste);
//create menu bar
menuBar = new JMenuBar();
//adds menus to the bar
menuBar.add(menuFile);
menuBar.add(menuEdit);
//add stuff to the window
this.setJMenuBar(menuBar);
this.add(page);
}//end init
//this method opens a file
private void openFile(){
JFileChooser fileChooser = new JFileChooser();
fileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
int result = fileChooser.showOpenDialog(this);
if(result == JFileChooser.CANCEL_OPTION)
return;
File fileName = fileChooser.getSelectedFile();
if (fileName == null || fileName.getName().equals("")){
JOptionPane.showMessageDialog(this, "Invalid File Name", "This is an" + " invalid file name.", JOptionPane.ERROR_MESSAGE);
}//end if
else{
JOptionPane.showMessageDialog(this, fileName.getName());
BufferedReader input;
try{
input = new BufferedReader(new FileReader(fileName.getAbsolutePath()));
String line;
while((line = input.readLine()) != null){
page.append(line+"\n");
}//end while
input.close();
}//end try
catch(Exception e){
JOptionPane.showMessageDialog(this, "Error", e.toString(), JOptionPane.ERROR_MESSAGE);
}//end catch
}//end else
}//end openFile
//this method saves our data to a file
private void saveFile(){
JFileChooser fileChooser = new JFileChooser();
fileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
int result = fileChooser.showSaveDialog(this);
if(result == JFileChooser.CANCEL_OPTION)
return;
File fileName = fileChooser.getSelectedFile();
if (fileName == null || fileName.getName().equals("")){
JOptionPane.showMessageDialog(this, "Invalid File Name", "This is an" + " invalid file name.", JOptionPane.ERROR_MESSAGE);
}//end if
else{
BufferedWriter output;
try{
output = new BufferedWriter(new FileWriter(fileName.getAbsolutePath()));
output.write(page.getText(), 0, page.getText().length());
output.close();
}//end try
catch(Exception e){
JOptionPane.showMessageDialog(this, "Error", e.toString(), JOptionPane.ERROR_MESSAGE);
}//end catch
}//end else
}//end saveFile
@Override
public void actionPerformed(ActionEvent e) {
//TODO Auto-generated method stub
if (e.getSource() == exit){
this.dispose();
}//end if
else if (e.getSource() == copy){
page.copy();
}//end else if
else if (e.getSource() == paste){
page.paste();
}//end else if
else if (e.getSource() == save){
this.saveFile();
}//end else if
else{
this.openFile();
}//end else
}//end override
}//end class
I have looked at other stack overflow pages, they didn't work. nothing I tried did.
Upvotes: 1
Views: 212
Reputation: 1206
The page.setCaretPosition(page.getDocument().getLength());
is aplied at the time you call it. That means that I'll set the caret position when you call it. So that also mean that I should be called after setting the tekst like:
page.append("Hi");
page.setCaretPosition(page.getDocument().getLength());
And now It will scroll. But you'll always have to call it if you want to let the area auto-scroll in this way. And if you want to make it a little bit easier you could write a method for it.
public void scroll(){
page.setCaretPosition(page.getDocument().getLength());
}
I hope that this helps you and if you need somemore info or clarification feel free to ask.
Upvotes: 0
Reputation: 757
Wrapping the JTextArea
in a JScrollPane
like this makes it work:
this.add(new JScrollPane(page));
It then adds/removes horizontal and vertical scrollbars as needed, and scrolls the scrollpane along with the caret.
Upvotes: 1