In: Computer Science
this there any way to shorten the line of code. Right now it about 300 line of code is there anyway to shorten it to 200 or so line of code?
import java.awt.*;
import java.awt.Color;
import java.awt.Desktop;
import java.awt.Font;
import java.awt.event.*;
import java.net.URL;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import javax.swing.JCheckBox;
import javax.swing.JColorChooser;
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.JRadioButton;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.KeyStroke;
//MyMenuFrame will use Jframe with actionlistener
class MyMenuFrame extends JFrame implements ActionListener{
//creating the main menu items
JMenu menuEdit = new JMenu("Edit");
JMenu menuPrint = new JMenu("Print");
JMenu mnFile = new JMenu("File");
JMenu menuHelp = new JMenu("Help");
//creating the submenu items here because we are gonna use these
across the code
JRadioButton subMenuItem1 = new JRadioButton("Times New
Roman");
JRadioButton subMenuItem2 = new JRadioButton("Arial");
JRadioButton subMenuItem3 = new JRadioButton("Serif");
JCheckBox subMenuItem4 = new JCheckBox("Bold");
JCheckBox subMenuItem5 = new JCheckBox("Italic");
//provide scrollable view of a component
JScrollPane scrollPane;
//creating notePadArea for notepad to input the text
JTextArea notePadArea;
public MyMenuFrame() {
//setting the border layout for JFrame
this.setLayout(new BorderLayout());
// create menu bar named menuBar
JMenuBar menuBar = new JMenuBar();
this.setJMenuBar(menuBar);//adding the menubar to JFrame
// create File menu
mnFile.setMnemonic(KeyEvent.VK_F);//Alt+F
menuBar.add(mnFile);//adding the menufile
// create Open menu item
JMenuItem mntmOpen = new JMenuItem("Open");//creating the Open menu
mntmOpen.setMnemonic(KeyEvent.VK_O);//Alt+O command
mntmOpen.setActionCommand("open");//when the command equals to 'open' then the corresponding action will be performed
mntmOpen.setAccelerator(KeyStroke.getKeyStroke('O', KeyEvent.CTRL_DOWN_MASK));//respond when user clicks Ctrl+O
mntmOpen.addActionListener(this);//adding actionLister to the Menu Option Open
// create Save menu item
JMenuItem mntmSave = new JMenuItem("Save");//creating the Save menu
mntmSave.setMnemonic(KeyEvent.VK_S);//Alt+S command
mntmSave.setActionCommand("save");//when the command equals to 'save' then the corresponding action will be performed
mntmSave.setAccelerator(KeyStroke.getKeyStroke('S', KeyEvent.CTRL_DOWN_MASK));//respond when user clicks Ctrl+S
mntmSave.addActionListener(this);//adding actionLister to the Menu Option Save
// create Exit menu item
JMenuItem mntmExit = new JMenuItem("Exit");//creating the Exit menu
mntmExit.setMnemonic(KeyEvent.VK_X);//Alt+X command
mntmExit.setActionCommand("exit");//when the command equals to 'exit' then the corresponding action will be performed
mntmExit.setAccelerator(KeyStroke.getKeyStroke('X', KeyEvent.CTRL_DOWN_MASK));//respond when user clicks Ctrl+X
mntmExit.addActionListener(this);//adding actionLister to the Menu Option Exit
// add open, save and exit menu to menu-bar
mnFile.add(mntmOpen);
mnFile.addSeparator();//adding separator between open and save
mnFile.add(mntmSave);
mnFile.addSeparator();//adding separator between save and exit
mnFile.add(mntmExit);
// create Edit menu
menuEdit.setMnemonic(KeyEvent.VK_E);//creating shortcut menu when user press Alt+E
menuBar.add(menuEdit);//adding the Edit to the menubar
JMenu submenu1 = new JMenu("Color");//creating the new menu
which comes under Edit
submenu1.setMnemonic(KeyEvent.VK_C);//creating shortcut menu when
user press Alt+C
JMenuItem menuItem0 = new JMenuItem("Change Color");//creating
submenu item called change color
menuItem0.setAccelerator(KeyStroke.getKeyStroke('C',
KeyEvent.CTRL_DOWN_MASK));//it responds when user click
Ctrl+C
menuItem0.setActionCommand("color");//setting the command used to
call the correcponding action when user click this
menuItem0.addActionListener(this);//adding actionlistener
submenu1.add(menuItem0);//adding this menu item to submenu
menuEdit.add(submenu1);//adding this submenu to editmenu
menuEdit.addSeparator();//creating separator between Color and
Font
ActionListener sm1 = new ActionListener() {
public void actionPerformed(ActionEvent actionEvent) {
subMenuItem1.setSelected(true);
subMenuItem2.setSelected(false);
subMenuItem3.setSelected(false);
notePadArea.setFont(new Font("Times New Roman", Font.PLAIN,
20));
}
};
ActionListener sm2 = new ActionListener() {
public void actionPerformed(ActionEvent actionEvent) {
subMenuItem2.setSelected(true);
subMenuItem1.setSelected(false);
subMenuItem3.setSelected(false);
notePadArea.setFont(new Font("Arial", Font.PLAIN, 20));
}
};
ActionListener sm3 = new ActionListener() {
public void actionPerformed(ActionEvent actionEvent) {
subMenuItem3.setSelected(true);
subMenuItem2.setSelected(false);
subMenuItem1.setSelected(false);
notePadArea.setFont(new Font("Serif", Font.PLAIN, 20));
}
};
JMenu submenu = new JMenu("Font");//creating the new menu which
comes under Edit
submenu.setMnemonic(KeyEvent.VK_F);//creating shortcut menu when
user press Alt+F
subMenuItem1.setMnemonic(KeyEvent.VK_T);//creating shortcut menu
when user press Alt+T for Times New Roman
subMenuItem1.setActionCommand("times_new_roman");//setting the
command used to call the correcponding action when user click
this
subMenuItem1.addActionListener(sm1);//adding actionlistener
submenu.add(subMenuItem1);//adding to the submenu
subMenuItem2.setMnemonic(KeyEvent.VK_A);//creating shortcut key
Alt+A
subMenuItem2.setActionCommand("arial");//respond when the command
equals to arial
subMenuItem2.addActionListener(sm2);//adding action listener
submenu.add(subMenuItem2);//adding it to the submenu
subMenuItem3.setMnemonic(KeyEvent.VK_S);
subMenuItem3.setActionCommand("serif");
subMenuItem3.addActionListener(sm3);
submenu.add(subMenuItem3);
submenu.addSeparator();
subMenuItem4.setMnemonic(KeyEvent.VK_B);
subMenuItem4.setActionCommand("bold");
subMenuItem4.addActionListener(this);
submenu.add(subMenuItem4);
subMenuItem5.setMnemonic(KeyEvent.VK_I);
subMenuItem5.setActionCommand("italic");
subMenuItem5.addActionListener(this);
submenu.add(subMenuItem5);
menuEdit.add(submenu);
// create Print menu
menuPrint.setMnemonic(KeyEvent.VK_P);
menuBar.add(menuPrint);
JMenuItem menuItemPrint = new JMenuItem("Send To Printer");
menuItemPrint.setAccelerator(KeyStroke.getKeyStroke('P', KeyEvent.CTRL_DOWN_MASK));
menuItemPrint.setActionCommand("print");
menuItemPrint.addActionListener(this);
menuPrint.add(menuItemPrint);
// create Help menu
menuHelp.setMnemonic(KeyEvent.VK_H);
menuBar.add(menuHelp);
JMenuItem menuItemHelp = new JMenuItem("About");
menuItemHelp.setAccelerator(KeyStroke.getKeyStroke('A', KeyEvent.CTRL_DOWN_MASK));
menuItemHelp.setActionCommand("about");
menuItemHelp.addActionListener(this);
JMenuItem menuItemVisitHomePage = new JMenuItem("Visit Home Page");
menuItemVisitHomePage.setAccelerator(KeyStroke.getKeyStroke('V', KeyEvent.CTRL_DOWN_MASK));
menuItemVisitHomePage.setActionCommand("visithomepage");
menuItemVisitHomePage.addActionListener(this);
menuHelp.add(menuItemHelp);
menuHelp.addSeparator();
menuHelp.add(menuItemVisitHomePage);
notePadArea = new JTextArea();
// set no word wrap
notePadArea.setWrapStyleWord(false);
// create scrollable pane
scrollPane = new JScrollPane(notePadArea, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS , JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
this.add(scrollPane,BorderLayout.CENTER);
}
@Override
public void actionPerformed(ActionEvent e) {
if(e.getActionCommand().equals("exit")) {
System.exit(0);
}else if (e.getActionCommand().equals("open")) {
JFileChooser file = new JFileChooser();
String fileName = "";//initial filename was empty
// show open file dialog
if (file.showOpenDialog(this) == JFileChooser.APPROVE_OPTION) {
fileName = file.getSelectedFile().getAbsolutePath();
} else {
return;
}
try(BufferedReader bufferedReader = new BufferedReader(new FileReader(fileName));) {
// load file content into text area
StringBuffer stringBuffer = new StringBuffer();//creating a string buffer for reading data from file
String lines = "";//for reading the lines from the selecting file
while((lines = bufferedReader.readLine() ) != null) {//it'll read untill the file ends
stringBuffer.append(lines).append("\n");//for every line read insert new line in stringBuffer
}
bufferedReader.close();//after reading of file done, the bufferedReader will be close
notePadArea.setText(stringBuffer.toString());//converting the read text to string and inserting this text into textArea
} catch (Exception error1) {//if any exception occures
System.out.println(error1.toString());//convert the expection into string and print it
}
} else if (e.getActionCommand().equals("save")) {//if the user click the save command then the file will gonna saved
JFileChooser file = new JFileChooser();//creating the file
chooser
String fileName = "";//initial file name is empty
// show open file dialog
if (file.showSaveDialog(this) == JFileChooser.APPROVE_OPTION) {//if the user select file and clicks OK button
fileName = file.getSelectedFile().getAbsolutePath();
} else {//other wise will be closed
return;
}
try(BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(fileName));) {
// write editor's content to selected file.
bufferedWriter.write(notePadArea.getText());//get the text
entered in textarea
bufferedWriter.flush();//clear the writer
} catch(Exception ex1) {}
} else if (e.getActionCommand().equals("color")) {
Color select_color = JColorChooser.showDialog(this, "Select a
color", Color.RED);
notePadArea.setForeground(select_color);
} else if (e.getActionCommand().equals("times_new_roman"))
{
if(subMenuItem1.isSelected())
notePadArea.setFont(new Font("Times New Roman", Font.PLAIN,
20));
} else if (e.getActionCommand().equals("arial")) {
if(subMenuItem2.isSelected())
notePadArea.setFont(new Font("Arial", Font.PLAIN, 20));
} else if (e.getActionCommand().equals("serif")) {
if(subMenuItem3.isSelected())
notePadArea.setFont(new Font("Serif", Font.PLAIN, 20));
} else if (e.getActionCommand().equals("bold")) {
if(subMenuItem4.isSelected()){
if(subMenuItem5.isSelected()){
notePadArea.setFont(new Font("Serif", Font.BOLD+Font.ITALIC,
20));
}else{
notePadArea.setFont(new Font("Serif", Font.BOLD, 20));
}
}else{
if(!subMenuItem5.isSelected())
notePadArea.setFont(new Font("Serif", Font.PLAIN, 20));
}
} else if (e.getActionCommand().equals("italic")) {
if(subMenuItem5.isSelected()){
if(subMenuItem4.isSelected()){
notePadArea.setFont(new Font("Serif", Font.BOLD+Font.ITALIC,
20));
}else{
notePadArea.setFont(new Font("Serif", Font.ITALIC, 20));
}
}else{
if(!subMenuItem4.isSelected())
notePadArea.setFont(new Font("Serif", Font.PLAIN, 20));
}
} else if (e.getActionCommand().equals("print")) {
int output = JOptionPane.showConfirmDialog(this, "Do you want to
print the File","Confirmation", JOptionPane.YES_NO_OPTION);
if(output==0){
JOptionPane.showMessageDialog(this, "The file is successfully
printed","Confirmation", JOptionPane.INFORMATION_MESSAGE);
}
} else if (e.getActionCommand().equals("changecolor")){
System.out.println("Color clicked");
}
else if (e.getActionCommand().equals("about")) {
JOptionPane.showMessageDialog(this, "This software is developed in 2019\nVersion is 1.0","About", JOptionPane.INFORMATION_MESSAGE);
} else if (e.getActionCommand().equals("visithomepage")) {
openWebpage("http://www.microsoft.com");
}
}
private void openWebpage (String urlString) {
try {
Desktop.getDesktop().browse(new URL(urlString).toURI());
}
catch (Exception e) {
e.printStackTrace();
}
}
}
import javax.swing.JFrame;
public class MyMenuFrameTest {
public static void main(String[] args) {
MyMenuFrame frame = new MyMenuFrame();
frame.setTitle("MyNotepad");
//for the title of the box
frame.setSize(600, 400);
//for the size of the box
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
I have managed to reduce from 350 to 211, and no more further line can be reduced, by the way, I have solved your earlier query of multiple radio button selected, believe me, we cannot reduce this further further
Do give a thumbs up
import java.awt.*;
import java.awt.event.*;
import java.net.URL;
import java.io.*;
import javax.swing.*;
class MyMenuFrame extends JFrame implements ActionListener
{
JMenu menuEdit = new JMenu("Edit");
JMenu menuPrint = new JMenu("Print");
JMenu mnFile = new JMenu("File");
JMenu menuHelp = new JMenu("Help");
JRadioButton subMenuItem1 = new JRadioButton("Times New
Roman");
JRadioButton subMenuItem2 = new JRadioButton("Arial");
JRadioButton subMenuItem3 = new JRadioButton("Serif");
JCheckBox subMenuItem4 = new JCheckBox("Bold");
JCheckBox subMenuItem5 = new JCheckBox("Italic");
JScrollPane scrollPane;
JTextArea notePadArea;
public MyMenuFrame() {
this.setLayout(new BorderLayout());
JMenuBar menuBar = new JMenuBar();
this.setJMenuBar(menuBar);
mnFile.setMnemonic(KeyEvent.VK_F);
menuBar.add(mnFile);
JMenuItem mntmOpen = new JMenuItem("Open");
mntmOpen.setMnemonic(KeyEvent.VK_O);
mntmOpen.setActionCommand("open");
mntmOpen.setAccelerator(KeyStroke.getKeyStroke('O',
KeyEvent.CTRL_DOWN_MASK));
mntmOpen.addActionListener(this);
JMenuItem mntmSave = new JMenuItem("Save");
mntmSave.setMnemonic(KeyEvent.VK_S);
mntmSave.setActionCommand("save");
mntmSave.setAccelerator(KeyStroke.getKeyStroke('S',
KeyEvent.CTRL_DOWN_MASK));
mntmSave.addActionListener(this);
JMenuItem mntmExit = new JMenuItem("Exit");
mntmExit.setMnemonic(KeyEvent.VK_X);
mntmExit.setActionCommand("exit");
mntmExit.setAccelerator(KeyStroke.getKeyStroke('X',
KeyEvent.CTRL_DOWN_MASK));
mntmExit.addActionListener(this);
mnFile.add(mntmOpen);
mnFile.addSeparator();
mnFile.add(mntmSave);
mnFile.addSeparator();
mnFile.add(mntmExit);
menuEdit.setMnemonic(KeyEvent.VK_E);
menuBar.add(menuEdit);
JMenu submenu1 = new JMenu("Color");
submenu1.setMnemonic(KeyEvent.VK_C);
JMenuItem menuItem0 = new JMenuItem("Change Color");
menuItem0.setAccelerator(KeyStroke.getKeyStroke('C',
KeyEvent.CTRL_DOWN_MASK));
menuItem0.setActionCommand("color");
menuItem0.addActionListener(this);
submenu1.add(menuItem0);
menuEdit.add(submenu1);
menuEdit.addSeparator();
ActionListener sm1 = new ActionListener() {
public void actionPerformed(ActionEvent actionEvent) {
subMenuItem1.setSelected(true);
subMenuItem2.setSelected(false);
subMenuItem3.setSelected(false);
notePadArea.setFont(new Font("Times New Roman", Font.PLAIN,
20));
}
};
ActionListener sm2 = new ActionListener() {
public void actionPerformed(ActionEvent actionEvent) {
subMenuItem2.setSelected(true);
subMenuItem1.setSelected(false);
subMenuItem3.setSelected(false);
notePadArea.setFont(new Font("Arial", Font.PLAIN, 20));
}
};
ActionListener sm3 = new ActionListener() {
public void actionPerformed(ActionEvent actionEvent) {
subMenuItem3.setSelected(true);
subMenuItem2.setSelected(false);
subMenuItem1.setSelected(false);
notePadArea.setFont(new Font("Serif", Font.PLAIN, 20));
}
};
JMenu submenu = new JMenu("Font"); //creating the new menu which
comes under Edit
submenu.setMnemonic(KeyEvent.VK_F); //creating shortcut menu when
user press Alt+F
subMenuItem1.setMnemonic(KeyEvent.VK_T); //creating shortcut menu
when user press Alt+T for Times New Roman
subMenuItem1.setActionCommand("times_new_roman"); //setting the
command used to call the correcponding action when user click
this
subMenuItem1.addActionListener(sm1); //adding actionlistener
submenu.add(subMenuItem1); //adding to the submenu
subMenuItem2.setMnemonic(KeyEvent.VK_A); //creating shortcut key
Alt+A
subMenuItem2.setActionCommand("arial"); //respond when the command
equals to arial
subMenuItem2.addActionListener(sm2); //adding action listener
submenu.add(subMenuItem2); //adding it to the submenu
subMenuItem3.setMnemonic(KeyEvent.VK_S);
subMenuItem3.setActionCommand("serif");
subMenuItem3.addActionListener(sm3);
submenu.add(subMenuItem3);
submenu.addSeparator();
subMenuItem4.setMnemonic(KeyEvent.VK_B);
subMenuItem4.setActionCommand("bold");
subMenuItem4.addActionListener(this);
submenu.add(subMenuItem4);
subMenuItem5.setMnemonic(KeyEvent.VK_I);
subMenuItem5.setActionCommand("italic");
subMenuItem5.addActionListener(this);
submenu.add(subMenuItem5);
menuEdit.add(submenu);
menuPrint.setMnemonic(KeyEvent.VK_P);
menuBar.add(menuPrint);
JMenuItem menuItemPrint = new JMenuItem("Send To Printer");
menuItemPrint.setAccelerator(KeyStroke.getKeyStroke('P',
KeyEvent.CTRL_DOWN_MASK));
menuItemPrint.setActionCommand("print");
menuItemPrint.addActionListener(this);
menuPrint.add(menuItemPrint);
menuHelp.setMnemonic(KeyEvent.VK_H);
menuBar.add(menuHelp);
JMenuItem menuItemHelp = new JMenuItem("About");
menuItemHelp.setAccelerator(KeyStroke.getKeyStroke('A',
KeyEvent.CTRL_DOWN_MASK));
menuItemHelp.setActionCommand("about");
menuItemHelp.addActionListener(this);
JMenuItem menuItemVisitHomePage = new JMenuItem("Visit Home
Page");
menuItemVisitHomePage.setAccelerator(KeyStroke.getKeyStroke('V',
KeyEvent.CTRL_DOWN_MASK));
menuItemVisitHomePage.setActionCommand("visithomepage");
menuItemVisitHomePage.addActionListener(this);
menuHelp.add(menuItemHelp);
menuHelp.addSeparator();
menuHelp.add(menuItemVisitHomePage);
notePadArea = new JTextArea();
notePadArea.setWrapStyleWord(false);
scrollPane = new JScrollPane(notePadArea,
JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
this.add(scrollPane, BorderLayout.CENTER);
}
public void actionPerformed(ActionEvent e) {
if (e.getActionCommand().equals("exit")) {
System.exit(0);
} else if (e.getActionCommand().equals("open")) {
JFileChooser file = new JFileChooser();
String fileName = ""; //initial filename was empty
if (file.showOpenDialog(this) == JFileChooser.APPROVE_OPTION)
{
fileName = file.getSelectedFile().getAbsolutePath();
} else
return;
try (BufferedReader bufferedReader = new BufferedReader(new
FileReader(fileName));) {
StringBuffer stringBuffer = new StringBuffer(); //creating a string
buffer for reading data from file
String lines = ""; //for reading the lines from the selecting
file
while ((lines = bufferedReader.readLine()) != null) //it'll read
untill the file ends
stringBuffer.append(lines).append("\n"); //for every line read
insert new line in s
bufferedReader.close(); //after reading of file done, the
bufferedReader will be close
notePadArea.setText(stringBuffer.toString()); //converting the read
text to string and inserting this text into textArea
} catch (Exception error1) { //if any exception occures
System.out.println(error1.toString()); //convert the expection into
string and print it
}
} else if (e.getActionCommand().equals("save")) { //if the user
click the save command then the file will gonna saved
JFileChooser file = new JFileChooser(); //creating the file
chooser
String fileName = ""; //initial file name is empty
if (file.showSaveDialog(this) == JFileChooser.APPROVE_OPTION)
fileName = file.getSelectedFile().getAbsolutePath();
else
return;
try (BufferedWriter bufferedWriter = new BufferedWriter(new
FileWriter(fileName));) {
bufferedWriter.write(notePadArea.getText()); //get the text entered
in textarea
bufferedWriter.flush(); //clear the writer
} catch (Exception ex1) {}
} else if (e.getActionCommand().equals("color")) {
Color select_color = JColorChooser.showDialog(this, "Select a
color", Color.RED);
notePadArea.setForeground(select_color);
} else if (e.getActionCommand().equals("bold")) {
if (subMenuItem4.isSelected()) {
if (subMenuItem5.isSelected())
notePadArea.setFont(new Font("Serif", Font.BOLD + Font.ITALIC,
12));
else
notePadArea.setFont(new Font("Serif", Font.BOLD, 12));
} else {
if (!subMenuItem5.isSelected())
notePadArea.setFont(new Font("Serif", Font.PLAIN, 12));
}
} else if (e.getActionCommand().equals("italic")) {
if (subMenuItem5.isSelected()) {
if (subMenuItem4.isSelected())
notePadArea.setFont(new Font("Serif", Font.BOLD + Font.ITALIC,
12));
else
notePadArea.setFont(new Font("Serif", Font.ITALIC, 12));
} else {
if (!subMenuItem4.isSelected())
notePadArea.setFont(new Font("Serif", Font.PLAIN, 12));
}
} else if (e.getActionCommand().equals("print")) {
int output = JOptionPane.showConfirmDialog(this, "Do you want to
print the File", "Confirmation", JOptionPane.YES_NO_OPTION);
if (output == 0)
JOptionPane.showMessageDialog(this, "The file is successfully
printed", "Confirmation", JOptionPane.INFORMATION_MESSAGE);
} else if (e.getActionCommand().equals("changecolor"))
System.out.println("Color clicked");
else if (e.getActionCommand().equals("about"))
JOptionPane.showMessageDialog(this, "This software is developed in
2019\nVersion is 1.0", "About",
JOptionPane.INFORMATION_MESSAGE);
else if (e.getActionCommand().equals("visithomepage"))
openWebpage("http://www.microsoft.com");
}
private void openWebpage(String urlString) {
try {
Desktop.getDesktop().browse(new URL(urlString).toURI());
} catch (Exception e) {
e.printStackTrace();
}
}
}
class MyMenuFrameTest {
public static void main(String[] args) {
MyMenuFrame frame = new MyMenuFrame();
frame.setTitle("MyNotepad");
frame.setSize(600, 400);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}