Question: Add a JScrollPane control to the frame 1. Add the JTextArea to a JScrollPane and then add the JScrollPane to the center area of the
Add a JScrollPane control to the frame
1. Add the JTextArea to a JScrollPane and then add the JScrollPane to the center area of the BorderLayout for the frame.
2. Run the application to make sure it works correctly. Now, if you open a large text file, a scrollbar should appear that allows you to scroll through the file.
Here is the java code:
* Main Class *
package murach.texteditor;
public class Main {
public static void main(String[] args) { MainWindow mainWin = new MainWindow(); } }
---------------------------------------------------------------------------
* MainWindow Class *
package murach.texteditor;
import java.awt.BorderLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.io.File; import java.io.IOException; import java.nio.file.Files; import java.nio.file.Paths; import javax.swing.JButton; import javax.swing.JFileChooser; import javax.swing.JFrame; import javax.swing.JOptionPane; import javax.swing.JPanel; import javax.swing.JScrollBar; import javax.swing.JTextArea; import javax.swing.UIManager; import javax.swing.UnsupportedLookAndFeelException;
public class MainWindow extends JFrame {
private File file; private String fileContents = ""; private JTextArea textArea;
public MainWindow() { try { UIManager.setLookAndFeel( UIManager.getSystemLookAndFeelClassName()); } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException e) { System.out.println(e); } setTitle("Text Editor"); setSize(800, 600); setLocationByPlatform(true); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// TODO: Add the button pannel to the JFrame. setLayout(new BorderLayout()); JPanel panel = buildButtonPanel(); panel.setBounds(0, 0, 800, 50); add(panel, BorderLayout.NORTH); textArea = new JTextArea(); textArea.setBounds(0, 0, 800, 500); add(textArea, BorderLayout.CENTER); setVisible(true); }
private JPanel buildButtonPanel() { JPanel panel = new JPanel(); panel.setBounds(0, 0, 800, 50); // TODO: Implement the rest of this method. JButton open = new JButton("Open"); JButton save = new JButton("Save");
panel.add(open); panel.add(save);
open.addActionListener(new ActionListener() {
@Override public void actionPerformed(ActionEvent e) { doOpenButton(); } }); save.addActionListener(new ActionListener() {
@Override public void actionPerformed(ActionEvent e) { doSaveButton(); } });
return panel; }
private void doOpenButton() { JFileChooser openDialog = new JFileChooser(); int choice = openDialog.showOpenDialog(this);
if (choice == JFileChooser.APPROVE_OPTION) { file = openDialog.getSelectedFile(); fileContents = ""; try { fileContents = new String( Files.readAllBytes(Paths.get(file.toURI()))); } catch (IOException ex) { JOptionPane.showMessageDialog(this, "The file could not be read.", "File read error", JOptionPane.ERROR_MESSAGE); } // TODO: display file contents in JTextArea control instead of on console textArea.setText(fileContents); } }
private void doSaveButton() { if (file != null) { // TODO: get file contents from JTextArea control instead of on console try { fileContents = textArea.getText(); Files.write(Paths.get(file.toURI()), fileContents.getBytes()); JOptionPane.showMessageDialog(null, "The file was saved!"); } catch (IOException ex) { JOptionPane.showMessageDialog(this, "The file could not be written.", "File write error", JOptionPane.ERROR_MESSAGE); } // TODO: display file contents in JTextArea control instead of on console textArea.setText(fileContents); } } }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
