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 Color

Solution

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class TextAreaPanel extends JPanel
{
   private JPanel controlPanel;
  
   private void showTextArea(){
  
   controlPanel = new JPanel();
   controlPanel.setLayout(new FlowLayout());
  
JTextArea TextArea = new JTextArea(20,40);
   TextArea.setLineWrap(true);
   TextArea.setWrapStyleWord(true);
JScrollPane scrollPane = new JScrollPane(TextArea);
JButton showButton = new JButton(\"Show\");

showButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {   
  
}
});   
controlPanel.add(scrollPane);
controlPanel.add(showButton);
  
}
}

public class ColorListPanel extends JPanel
{
  
   private JPanel controlPanel;
   private JLabel statusLabel;

   private void showList(){
  
   controlPanel = new JPanel();
controlPanel.setLayout(new FlowLayout());

final DefaultListModel colors = new DefaultListModel();
   colors.addElement(Color.RED);
colors.addElement(Color.GREEN);
colors.addElement(Color.BLUE);
colors.addElement(Color.YELLOW);
  
   final JList colorsList = new JList(colors);
colorsList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
colorsList.setSelectedIndex(0);
colorsList.setVisibleRowCount(4);
   JScrollPane colorsListScrollPane = new JScrollPane(colorsList);
  
   JButton AddButton = new JButton(\"Add Color\");
   JButton DelButton = new JButton(\"Delete Color\");
  
   AddButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String data = \"\";
if (colorsList.getSelectedIndex() != -1) {   
data = \"color Selected: \" + colorsList.getSelectedValue();
statusLabel.setText(data);
}
statusLabel.setText(data);
}
});
  
   DelButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String data = \"\";
if (colorsList.getSelectedIndex() != -1) {   
data = \"color Selected: \" + colorsList.getSelectedValue();
statusLabel.setText(data);
}
statusLabel.setText(data);
}
});
controlPanel.add(colorsListScrollPane);
controlPanel.add(AddButton);
   controlPanel.add(DelButton);
   }
}

public class TextBoxGUI {
  
   private JFrame mainFrame;
  
   public void showPad()
   {
   TextAreaPanel textAreaPanel = new TextAreaPanel();
   ColorListPanel colorListPanel = new ColorListPanel();
  
   mainFrame = new JFrame(\"Java Swing Examples\");
mainFrame.setSize(400,400);
mainFrame.setLayout(new BorderLayout());
  
   mainFrame.add(textAreaPanel, BorderLayout.CENTER);
   mainFrame.add(colorListPanel, BorderLayout.EAST);
  
   JMenuBar menuBar = new JMenuBar();
   JMenu fileMenu = new JMenu(\"File\");
  
   JMenuItem SaveMenuItem = new JMenuItem(\"Save\");
SaveMenuItem.setActionCommand(\"Save\");
  
   JMenuItem LoadMenuItem = new JMenuItem(\"Load\");
LoadMenuItem.setActionCommand(\"Load\");
  
   JMenuItem ExitMenuItem = new JMenuItem(\"Exit\");
ExitMenuItem.setActionCommand(\"Exit\");
  
   MenuItemListener menuItemListener = new MenuItemListener();
   SaveMenuItem.addActionListener(menuItemListener);
LoadMenuItem.addActionListener(menuItemListener);
   ExitMenuItem.addActionListener(menuItemListener);
  
   fileMenu.add(SaveMenuItem);
fileMenu.add(LoadMenuItem);
fileMenu.add(ExitMenuItem);
  
   menuBar.add(fileMenu);
  
   mainFrame.setJMenuBar(menuBar);
mainFrame.setVisible(true);
  
   }
   class MenuItemListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
statusLabel.setText(e.getActionCommand() + \" Item clicked.\");
}
}
}

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 mak
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 mak
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 mak
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 mak

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site