Logo 
Search:

Java Forum

Ask Question   UnAnswered
Home » Forum » Java       RSS Feeds

shortcut for menu

  Asked By: Conrad    Date: Jun 23    Category: Java    Views: 830
  

I creat a notepad using Java .

I have creat the menubar but I can't add shorcut to MenuItem and MenuBar ( e.g
Alt + F for file menu , F for file , Ctrl + O to Open file ) . I just need some
sample code

If you could help me then here is my code:



import java.io.*;
import java.awt.*;
import java.awt.event.*;


class Notepad extends Frame implements MouseListener{
// Frame myFrame = new Frame("This is a Frame");
TextArea ta = new TextArea(15, 60);
int textSize = 15;
Button btnLow = new Button("Lower");
Button btnUp = new Button("Upper");
Button bigBut = new Button("Bigger");
Button smallBut = new Button("Smaller");
Checkbox cbBold = new Checkbox("Bold");
Checkbox cbItalic = new Checkbox("Italic");
Choice colors = new Choice();
PopupMenu optionsMenu;
Label lb1 = new Label("Choose color");
GridBagLayout gb = new GridBagLayout();
GridBagConstraints gbc = new GridBagConstraints();
String strFileName = new String();
Panel p1 = new Panel();
Panel p2 = new Panel();


public Notepad(String title) {
super(title);



MenuBar mbar=new MenuBar();
setMenuBar(mbar);

Menu file=new Menu("File");
mbar.add(file);

MenuItem newItem=new MenuItem("New");
newItem.addActionListener (new MenuProccessor(this));
file.add(newItem);

MenuItem openItem=new MenuItem("Open");
openItem.addActionListener (new MenuProccessor(this));
file.add(openItem);

MenuItem closeItem=new MenuItem("Close");
closeItem.addActionListener (new MenuProccessor(this));
file.add(closeItem);

file.addSeparator();

MenuItem saveItem=new MenuItem("Save");
saveItem.addActionListener (new MenuProccessor(this));
file.add(saveItem);


MenuItem saveAsItem=new MenuItem("Save as");
saveAsItem.addActionListener(new MenuProccessor(this));
file.add(saveAsItem);

file.addSeparator();

MenuItem exitItem=new MenuItem("Exit");
exitItem.addActionListener (new MenuProccessor(this));
file.add(exitItem);


Menu editMenu=new Menu("Edit");
mbar.add(editMenu);

MenuItem cutItem=new MenuItem("Cut");
cutItem.addActionListener (new MenuProccessor(this));
editMenu.add(cutItem);

MenuItem copyItem=new MenuItem("Copy");
copyItem.addActionListener (new MenuProccessor(this));
editMenu.add(copyItem);

MenuItem pasteItem=new MenuItem("Paste");
// pasteItem.addActionListener(this);
editMenu.add(pasteItem);

MenuItem deleteItem=new MenuItem("Delete");
// deleteItem.addActionListener(this);
editMenu.add(deleteItem);

Menu helpMenu = new Menu("Help");
mbar.add(helpMenu);
// helpMenu.addActionListener(this);
MenuItem aboutItem = new MenuItem("About");
helpMenu.add(aboutItem);
aboutItem.addActionListener(new MenuProccessor(this));
MenuItem contentItem = new MenuItem("Content");
helpMenu.add(contentItem);
MenuItem indexItem = new MenuItem("Index");
helpMenu.add(indexItem);

Menu findMenu = new Menu("Find");
helpMenu.add(findMenu);
findMenu.addMouseListener (new MenuProccessor(this));
MenuItem searchChar = new MenuItem("Search by character");
findMenu.add(searchChar);
MenuItem searchWord = new MenuItem("Search by word");
findMenu.add(searchWord);

optionsMenu = new PopupMenu("Options");
editMenu.add(optionsMenu);
optionsMenu.addActionListener(new ButtonProccessor(this));
MenuItem readItem = new MenuItem("Select All");
optionsMenu.add(readItem);
optionsMenu.addSeparator();

Menu formatMenu = new Menu("Format text");
optionsMenu.add(formatMenu);
this.add(optionsMenu);
// formatMenu.addActionListener(this);
CheckboxMenuItem insertItem = new CheckboxMenuItem("Insert",true);
formatMenu.add(insertItem);
CheckboxMenuItem overtypeItem = new CheckboxMenuItem("Overtype",false);
formatMenu.add(overtypeItem);

addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent we) {
System.exit(0);
}
}
);
setSize(300, 300);

btnUp.addActionListener (new ButtonProccessor(this));
btnLow.addActionListener (new ButtonProccessor(this));
cbBold.addItemListener (new ItemProccessor(this));
cbItalic.addItemListener (new ItemProccessor(this));
bigBut.addActionListener (new ButtonProccessor(this));
smallBut.addActionListener (new ButtonProccessor(this));

p1.setLayout(new BorderLayout());

p1.add("Center",ta);


p2.setLayout(new FlowLayout());
p2.add(btnUp);
p2.add(btnLow);
p2.add(bigBut);
p2.add(smallBut);
p2.add(cbBold);
p2.add(cbItalic);
p2.add(lb1);
p2.add(colors);
/*gbc.fill=GridBagConstraints.HORIZONTAL;
p2.addComponent(btnUp,0,5,1,1);
gbc.fill=GridBagConstraints.HORIZONTAL;
p2.addComponent(btnLow,0,6,1,1);
gbc.fill=GridBagConstraints.HORIZONTAL;
p2.addComponent(bigBut,1,5,1,1);
p2.addComponent(smallBut,1,6,1,1);
gbc.fill=GridBagConstraints.HORIZONTAL;
p2.addComponent(cbBold,5,1,1,1);
gbc.fill=GridBagConstraints.HORIZONTAL;
p2.addComponent(cbItalic,5,2,1,1);
gbc.fill=GridBagConstraints.HORIZONTAL;
p2.addComponent(lb1,5,3,2,1);
gbc.fill=GridBagConstraints.HORIZONTAL;
 p2.addComponent(colors,5,4,1,1);*/

colors.addItem("black");
colors.addItem("white");
colors.addItem("red");
colors.addItem("green");
colors.addItem("blue");

colors.addItemListener(new ItemProccessor(this));

setLayout(new BorderLayout());
add("Center",p1);
add("South",p2);


}
public void addComponent(Component c,int row, int col , int nrow, int ncol){
gbc.gridx=col;
gbc.gridy=row;

gbc.gridwidth=ncol;
gbc.gridheight=nrow;

gb.setConstraints(c,gbc);
add(c);
}
public void mouseEntered(MouseEvent m) {}
public void mouseExited(MouseEvent m) {}
public void mouseClicked(MouseEvent m)
{
optionsMenu.show(this,m.getX(),m.getY());
}
public void mouseReleased(MouseEvent m) {}
public void mousePressed(MouseEvent m) {}



public static void main(String args[]) {
Notepad t = new Notepad("Notepad created by Java");
t.setSize(600,400);
t.show();
}
}


class MenuProccessor implements ActionListener{
Notepad parent;
// MyDialog warningDialog;

String strContent;
public MenuProccessor(Notepad object){

parent = object;
}
public void actionPerformed(ActionEvent ae){

// creat new file

if (ae.getActionCommand().equals("New"))
{
parent.strFileName = new String();
parent.ta.setText(new String());
System.out.println("New File created ...");
}



// Open file

if (ae.getActionCommand().equals("Open")){
FileDialog fdOpen;
// String parent.strFileName;
fdOpen =new FileDialog(parent, "Open file", FileDialog.LOAD);
fdOpen.show();
parent.strFileName = fdOpen.getFile();
try{
FileInputStream fi = new FileInputStream(parent.strFileName);
int ibyts=fi.available();
byte ibuf[]=new byte[ibyts];
int byrd=fi.read(ibuf,0,ibyts);
String content=new String(ibuf);
parent.ta.setText(content);
fi.close();
System.out.println("File LOADED: " + parent.strFileName);
}catch(Exception e){
System.out.println("Action has been stopped");
}

}

// Close file

if (ae.getActionCommand().equals("Close")){
if (parent.strFileName.length()!=0){
MyDialog warningDialog = new MyDialog(parent,"Warning","You should save your
document before close it");
System.out.println("File " + parent.strFileName + " has been succesful
closed");
parent.strFileName="";
parent.ta.setText("");
}else {

}


}

// Save file
if (ae.getActionCommand().equals("Save")){

if (parent.strFileName.length()==0){

FileDialog fdSave;
// String strContent;

fdSave = new FileDialog(parent, "Save to file", FileDialog.SAVE);
fdSave.show();
System.out.println("Saving");
parent.strFileName = fdSave.getFile();
strContent = parent.ta.getText();
try {
FileOutputStream fo;
fo = new FileOutputStream(parent.strFileName);
fo.write(strContent.getBytes());
fo.close();
System.out.println("File saved: " + parent.strFileName);
System.out.println("Finished Saving");
}catch(Exception e){
System.out.println("Process has been cancelled ");
}
}
else{
try{
//delete a recent file
File f = new File(parent.strFileName);
f.delete();

System.out.println("Saving");
strContent = parent.ta.getText();
FileOutputStream fo;
fo = new FileOutputStream(parent.strFileName);
fo.write(strContent.getBytes());
fo.close();
System.out.println("File saved: " + parent.strFileName);
System.out.println("Finished Saving");
}catch(Exception e){
System.out.println("Process has been cancelled ");
}
}


}

// Save as new file
if (ae.getActionCommand().equals("Save as")){
FileDialog fdSave;
String strContent;

fdSave = new FileDialog(parent, "Save to file", FileDialog.SAVE);
fdSave.show();
System.out.println("Saving");
parent.strFileName = fdSave.getFile();
strContent = parent.ta.getText();
try {
FileOutputStream fo;
fo = new FileOutputStream(parent.strFileName);
fo.write(strContent.getBytes());
fo.close();
System.out.println("File saved: " + parent.strFileName);
System.out.println("Finished Saving");
}catch(Exception e){
System.out.println("Process has been cancelled ");
}
}

// exit program
if (ae.getActionCommand().equals("Exit")){

if (parent.strFileName.length()!=0){
MyDialog warningDialog = new MyDialog(parent,"Warning","You should save your
document before exit");
}
System.exit(0);
System.out.println("Good bye. CU soon.");
}

if (ae.getActionCommand().equals("Cut")){
// parent.textSize(textSize);


}

//author information
if (ae.getActionCommand().equals("About")){
try{
FileInputStream fi = new FileInputStream("about.txt");
int ibyts=fi.available();
byte ibuf[]=new byte[ibyts];
int byrd=fi.read(ibuf,0,ibyts);
String content=new String(ibuf);
fi.close();
MyDialog dialog = new MyDialog (parent,"About",content);

}catch(Exception e){
System.out.println("File not found");
}

}


}
}
class ButtonProccessor implements ActionListener {
Notepad parent;

public ButtonProccessor(Notepad object) {
parent = object;
}

public void actionPerformed(ActionEvent ae) {
if (ae.getSource() == parent.btnUp) {
String selText = parent.ta.getSelectedText();
int startPos = parent.ta.getSelectionStart();
int endPos = parent.ta.getSelectionEnd();
parent.ta.replaceText(selText.toUpperCase(), startPos,endPos);;
}
if (ae.getSource() == parent.btnLow) {
String selText = parent.ta.getSelectedText();
int startPos = parent.ta.getSelectionStart();
int endPos = parent.ta.getSelectionEnd();
parent.ta.replaceText(selText.toLowerCase(), startPos,endPos);;
}
if (ae.getSource()==parent.bigBut){
parent.textSize++;
parent.ta.addNotify();

}
if (ae.getSource()==parent.smallBut){
parent.textSize--;
parent.ta.addNotify();

}

if (ae.getActionCommand().equals("Select All")){
parent.ta.selectAll();
}




}


}

class ItemProccessor implements ItemListener{
Notepad parent;
public ItemProccessor(Notepad object){
parent = object;
}

public void itemStateChanged(ItemEvent e) {
if (!parent.cbBold.getState()&&!parent.cbItalic.getState()) {
parent.ta.setFont(new Font("Arial", Font.PLAIN, parent.textSize));
}
if (parent.cbBold.getState() &&!parent.cbItalic.getState()) {
parent.ta.setFont(new Font("Arial", Font.BOLD, parent.textSize));
}
if (!parent.cbBold.getState()&&parent.cbItalic.getState()) {
parent.ta.setFont(new Font("Arial", Font.ITALIC, parent.textSize));
}
if (parent.cbBold.getState() &&parent.cbItalic.getState()) {
parent.ta.setFont(new Font("Arial", Font.BOLD | Font.ITALIC,
parent.textSize));
}

if (e.getItem().equals("white")){
parent.ta.setForeground(Color.white);
}
if (e.getItem().equals("black")){
parent.ta.setForeground(Color.black);
}
if (e.getItem().equals("red")){
parent.ta.setForeground(Color.red);
}
if (e.getItem().equals("green")){
parent.ta.setForeground(Color.green);
}
if (e.getItem().equals("blue")){
parent.ta.setForeground(Color.blue);
}

}
}

class MyDialog extends Dialog implements ActionListener{
String title,mes;
Label lb3;
Button closeButton;
public MyDialog(Frame f, String title, String mes ){
super(f,title, true);
setLayout(new FlowLayout());
this.mes = mes;
lb3=new Label(mes);
add(lb3);

closeButton = new Button("Close");closeButton.addActionListener(this);
add("South",closeButton);

pack();
setVisible(true);
}

public void actionPerformed(ActionEvent ae){
if (ae.getSource()==closeButton) dispose();
}
}

Share: 

 

No Answers Found. Be the First, To Post Answer.

 
Didn't find what you were looking for? Find more on shortcut for menu Or get search suggestion and latest updates.




Tagged: