n this lab you will create a custom word pad with saving and
n 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
Simple Wordpad File java.awt.Color 255, g 0,b 01 java.awt.Color 0,g 0,b 255] Add Color Delete ColorSolution
package net.codejava.swing;
import javax.swing.DefaultListModel;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JScrollPane;
import javax.swing.SwingUtilities;
import net.codejava.model.Country;
/**
* JList Custom Renderer Example
*
* @author wwww.codejava.net
*/
public class JListCustomRendererExample extends JFrame {
public JListCustomRendererExample() {
Country us = new Country(\"USA\", \"us\");
Country in = new Country(\"India\", \"in\");
Country vn = new Country(\"Vietnam\", \"vn\");
Country ca = new Country(\"Canada\", \"ca\");
Country de = new Country(\"Denmark\", \"de\");
Country fr = new Country(\"France\", \"fr\");
Country gb = new Country(\"Great Britain\", \"gb\");
Country jp = new Country(\"Japan\", \"jp\");
//create the model and add elements
DefaultListModel<Country> listModel = new DefaultListModel<>();
listModel.addElement(us);
listModel.addElement(in);
listModel.addElement(vn);
listModel.addElement(ca);
listModel.addElement(de);
listModel.addElement(fr);
listModel.addElement(gb);
listModel.addElement(jp);
//create the list
JList<Country> countryList = new JList<>(listModel);
add(new JScrollPane(countryList));
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setTitle(\"JList Renderer Example\");
this.setSize(200, 200);
this.setLocationRelativeTo(null);
this.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
new JListCustomRendererExample();
}
});
}
}
package net.codejava.swing;
import java.awt.Component;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.ListCellRenderer;
import net.codejava.model.Country;
/**
* Custom renderer to display a country\'s flag alongside its name
*
* @author wwww.codejava.net
*/
public class CountryRenderer extends JLabel implements ListCellRenderer<Country> {
@Override
public Component getListCellRendererComponent(JList<? extends Country> list, Country country, int index,
boolean isSelected, boolean cellHasFocus) {
String code = country.getCode();
ImageIcon imageIcon = new ImageIcon(getClass().getResource(\"/images/\" + code + \".png\"));
setIcon(imageIcon);
setText(country.getName());
return this;
}
}
package net.codejava.swing;
import java.awt.Color;
import javax.swing.DefaultListModel;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JScrollPane;
import javax.swing.SwingUtilities;
import net.codejava.model.Country;
/**
* JList Custom Renderer Example
*
* @author wwww.codejava.net
*/
public class JListCustomRendererExample extends JFrame {
public JListCustomRendererExample() {
Country us = new Country(\"USA\", \"us\");
Country in = new Country(\"India\", \"in\");
Country vn = new Country(\"Vietnam\", \"vn\");
Country ca = new Country(\"Canada\", \"ca\");
Country de = new Country(\"Denmark\", \"de\");
Country fr = new Country(\"France\", \"fr\");
Country gb = new Country(\"Great Britain\", \"gb\");
Country jp = new Country(\"Japan\", \"jp\");
//create the model and add elements
DefaultListModel<Country> listModel = new DefaultListModel<>();
listModel.addElement(us);
listModel.addElement(in);
listModel.addElement(vn);
listModel.addElement(ca);
listModel.addElement(de);
listModel.addElement(fr);
listModel.addElement(gb);
listModel.addElement(jp);
//create the list
JList<Country> countryList = new JList<>(listModel);
add(new JScrollPane(countryList));
countryList.setCellRenderer(new CountryRenderer());
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setTitle(\"JList Renderer Example\");
this.setSize(200, 200);
this.setLocationRelativeTo(null);
this.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
new JListCustomRendererExample();
}
});
}
}





