In this lab you will create a custom word pad with saving an
In this lab, you will create a custom word pad with saving and loading functionality, an exit option, and a customizable background color. First off, please make the following three files:
- ColorListPanel – This class should extend JPanel. This panel will hold the list of background colors, as well as add and delete buttons for the color list
- TextAreaPanel – This class should extend JPanel. This panel only holds the JTextArea
- TextBoxGUI – This class should extend JFrame. This JFrame has a ColorListPanel in the east and a TextAreaPanel in the center, as shown. Further, it will hold all event listeners (the action listeners and the list selection listeners).
For the ColorListPanel:
- Please make your DefaultListModel and JList use Colors as their parameterized type (e.g. JList<Color> list = new JList<Color>();)
- Give the list a visibile row count of 4, and a JScrollPane
- Make the list Single Selection only
- Please have two buttons, Add Color and Delete Color, as shown
- You may need to make setters and getters to make the event listeners work, for the listModel, the list, the currently selected list index, etc.
- ColorListPanel should have a way to get the ActionListeners and ListSelectionListeners you write in TextBoxGUI registered to the list and buttons inside the panel
For the TextAreaPanel:
- Please give the JScrollPane holding the JTextArea a preferred size using .setPreferredSize(new Dimension(width, height)). The dimensions are up to you
- You should make the JTextArea’s text accessible to TextBoxGUI for saving and loading purposes
- Wrap on words
For the TextBoxGUI:
- Please add the two panels as specified. Further, add a JMenuBar to this GUI. It should have one menu, File, with three MenuItems – Save, Load, and Exit
- Please give File, Save, Load, and Exit hotkeys/mnemonics – F, S, L, and X, respectively
- Please give Save, Load, and Exit tooltips
- Please write all listeners in this class, to make TextBoxGUI our “controller,” class in our MVC architecture. You need to provide the following functionality, for the following event generators:
- For the Save MenuItem, open a JFileChooser’s save dialog, allow the user to enter a file name, and save the contents of the JTextArea in that file. You may need to use the text area’s .getText() method. You should use PrintWriter/FileWriter, but can use other methods. You will need to wrap the code in a try/catch – let Eclipse do this for you.
- For the Load MenuItem, open a JFileChooser’s open dialog, allow the user to enter a file name, and load the contents of that file into the JTextArea. You may need to use the text area’s .setText(String) method. You should use File/Scanner, but can use other methods. You will need to wrap the code in a try/catch – let Eclipse do this for you.
- For the Exit MenuItem, use System.exit(0)
- For the Add Color button, present the user with a JColorChooser, then add their selection to the list
- For the Delete Color button, delete the color the user currently has selected
- When a color is selected in the color list, please change the background color of the center panel as shown. This change should only occur IF the value is not adjusting AND the list’s currently selected index is not -1
Below follow a few screenshots, of the GUI in different states:
The GUI with a bunch of colors added, showing the save tooltip and the list scroll
The GUI with all the colors removed (with red as the last selected color), showing the exit tooltip, and the JTextArea scroll
Extra Credit Opportunity:
As shown, the list shows the colors as a somewhat ugly RGB value list. If you can implement a custom ColorItem class that presents colors as a string name (that the user can enter on selection or you can determine based on the RGB, up to you) instead and allows for full JColorChooser user, I will give you up to 10 extra points. It is quite a bit of work!
Simple Wordpad File java.awt.Color 255, g 0,b 01 java.awt.Color 0,g 0,b 255] Add Color Delete ColorSolution
TextBoxGUI.java
------------------------
package chegg.wordped;
import java.awt.GridLayout;
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 java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.ListSelectionModel;
public class TextBoxGUI extends JFrame {
private static final long serialVersionUID = 1L;
private JFrame paraent = null;
private ColorListPanel topPanel = null;
private TextAreaPanel middlePanel = null;
private JPanel buttomPanel = null;
private JButton addcolor = null;
private JButton deleteColor = null;
@SuppressWarnings(\"unchecked\")
public TextBoxGUI() {
paraent = this;
GridLayout gridLayout = new GridLayout(4, 1);
setTitle(\"Word Pad.\");
setSize(900, 600);
setLayout(gridLayout);
addMenuBarToFrame();
addcolor = new JButton(\"Add Color\");
deleteColor = new JButton(\"Delete Color\");
topPanel = new ColorListPanel();
middlePanel = new TextAreaPanel();
buttomPanel = new JPanel();
buttomPanel.setLayout(new GridLayout(1, 1));
add(topPanel);
add(middlePanel);
add(buttomPanel);
JPanel sidePanel = new JPanel();
middlePanel.add(sidePanel);
sidePanel.add(addcolor);
sidePanel.add(deleteColor);
sidePanel.add(new JPanel());
sidePanel.setAlignmentY(RIGHT_ALIGNMENT);
addcolor.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
setBackground(topPanel.colors[topPanel.getMySpriteOptions()
.getSelectedIndex()]);
}
});
topPanel.getMySpriteOptions().setSelectionMode(
ListSelectionModel.SINGLE_SELECTION);
// do not allow multiple selections colorList.
// add a JScrollPane containing the JList // to the content pane
// add( new JScrollPane( topPanel.getMySpriteOptions() ) );
// set up event handler
setLocationRelativeTo(null);
setResizable(true);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void addMenuBarToFrame() {
// Creates a menubar for a JFrame
JMenuBar menuBar = new JMenuBar();
// Add the menubar to the frame
setJMenuBar(menuBar);
// Define and add two drop down menu to the menubar
JMenu fileMenu = new JMenu(\"File\");
menuBar.add(fileMenu);
// Create and add simple menu item to one of the drop down menu
JMenuItem saveAction = new JMenuItem(\"Save\");
JMenuItem editAction = new JMenuItem(\"Edit\");
JMenuItem exitAction = new JMenuItem(\"Exit\");
fileMenu.add(saveAction);
fileMenu.add(editAction);
fileMenu.add(exitAction);
saveAction.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
System.out.println(\"You have clicked on the new action\");
JFileChooser fc = new JFileChooser();
int returnVal = fc.showOpenDialog(paraent);
if (returnVal == JFileChooser.APPROVE_OPTION) {
System.out.println(fc.getCurrentDirectory());
System.out.println(fc.getSelectedFile());
File file = new File(fc.getCurrentDirectory() + \"//\"
+ fc.getSelectedFile());
System.out.println(file.getAbsolutePath());
System.out.println(file.getPath());
// This is where a real application would open the file.
System.out.println(\"Opening: \" + file.getAbsolutePath()
+ \".\" + \"\ \");
TextBoxGUI.writeFile(fc.getSelectedFile(), middlePanel
.getTestArea().getText());
} else {
System.out
.println(\"Open command cancelled by user.\" + \"\ \");
}
}
});
editAction.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
System.out.println(\"You have clicked on the new action\");
JFileChooser fc = new JFileChooser();
int returnVal = fc.showOpenDialog(paraent);
if (returnVal == JFileChooser.APPROVE_OPTION) {
// This is where a real application would open the file.
List<String> list = TextBoxGUI.readFile(fc
.getSelectedFile());
StringBuilder content = new StringBuilder();
for (String string : list) {
content.append(string + \" \");
}
middlePanel.getTestArea().setText(content.toString());
} else {
System.out
.println(\"Open command cancelled by user.\" + \"\ \");
}
}
});
exitAction.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
System.out.println(\"You have clicked on the new action\");
System.exit(0);
}
});
}
/**
*
* @param fileName
* @return
*/
private static List<String> readFile(File fileName) {
List<String> animals = new ArrayList<String>();
try (BufferedReader br = new BufferedReader(new FileReader(fileName))) {
String sCurrentLine;
while ((sCurrentLine = br.readLine()) != null) {
animals.add(sCurrentLine);
}
} catch (IOException e) {
e.printStackTrace();
}
return animals;
}
/**
*
*/
private static void writeFile(File fileName, String content) {
System.out.println(content + \"/\" + fileName);
if (content == null || content.length() < 0)
return;
try (FileWriter fw = new FileWriter(fileName, true);
BufferedWriter bw = new BufferedWriter(fw);
PrintWriter out = new PrintWriter(bw)) {
out.println(content);
} catch (IOException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
new TextBoxGUI();
}
}
ColorListPanel.java
----------------------------
package chegg.wordped;
import java.awt.Color;
import java.awt.GridLayout;
import javax.swing.AbstractListModel;
import javax.swing.BorderFactory;
import javax.swing.JList;
import javax.swing.JPanel;
public class ColorListPanel extends JPanel {
private JList<String> mySpriteOptions = null;
public static final Color colors[] = { Color.black, Color.blue, Color.cyan,
Color.darkGray, Color.gray, Color.green, Color.lightGray,
Color.magenta, Color.orange, Color.pink, Color.red, Color.white,
Color.yellow };
/**
*
*/
private static final long serialVersionUID = 3014954806676129951L;
@SuppressWarnings({ \"unchecked\", \"rawtypes\" })
public ColorListPanel() {
setLayout(new GridLayout(1, 2, 100, 50));
mySpriteOptions = new JList<String>();
mySpriteOptions.setModel(new AbstractListModel() {
private static final long serialVersionUID = 1L;
String colorNames[] = { \"Black\", \"Blue\", \"Cyan\", \"Dark Gray\",
\"Gray\", \"Green\", \"Light Gray\", \"Magenta\", \"Orange\", \"Pink\",
\"Red\", \"White\", \"Yellow\" };
JList<Color> jlist = new JList<Color>(colors);
public int getSize() {
return colors.length;
}
public Object getElementAt(int i) {
return colors[i];
}
});
mySpriteOptions.setBorder(BorderFactory.createLineBorder(Color.BLACK));
add(new JPanel());
add(mySpriteOptions);
}
public JList<String> getMySpriteOptions() {
return mySpriteOptions;
}
public void setMySpriteOptions(JList<String> mySpriteOptions) {
this.mySpriteOptions = mySpriteOptions;
}
}
TextAreaPanel.java
--------------------------
package chegg.wordped;
import java.awt.GridLayout;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
public class TextAreaPanel extends JPanel {
/**
*
*/
private static final long serialVersionUID = 4599052279441711318L;
private JTextArea testArea = null;
public TextAreaPanel() {
setLayout(new GridLayout(1, 2, 100, 50));
testArea = new JTextArea(\"kdsjf\");
JScrollPane scroll = new JScrollPane(testArea);
// testArea.setMinimumSize(new Dimension(300,300));
// testArea.setPreferredSize(new Dimension(400,300-50));
scroll.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
scroll.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
add(scroll);
}
public JTextArea getTestArea() {
return testArea;
}
public void setTestArea(JTextArea testArea) {
this.testArea = testArea;
}
public static long getSerialversionuid() {
return serialVersionUID;
}
}





