1

I am trying to write a clipboard program that can copy/paste and save to a txt file. While the program works, I am trying to change the buttons into a Menu with MenuItems, however, I cannot figure out how to use the Menu item properly, as I cannot add it to a panel.

Please notice I am using AWT and not Swing, so no JPanel/JFrame, etc. Any tip/help is appreciated.

This is my code and attempt at changing it into a menu, please let me know what I am doing wrong:

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

public class CheesyWP extends Frame implements ActionListener {

    /**
     * @param args
     */

    //new panel for menu
    Panel north;

    //original
    Panel center;
    Panel south;
    Button save;
    Button load;
    Button clip;
    Button finish;
    Menu mn;
    MenuItem mSave;
    MenuItem mLoad;
    MenuItem mClip;
    MenuItem mFinish;
    TextArea ta;

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        CheesyWP cwp = new CheesyWP();
        cwp.doIt();

    }

    public void doIt() {
        center = new Panel();
        south = new Panel();
        clip = new Button("Open Clipboard");
        save = new Button("Save");
        load = new Button("Load");
        finish = new Button("Finish");

        //menu items
        north = new Panel();
        mn = new Menu();
        mSave = new MenuItem("Save");
        mLoad = new MenuItem("Load");
        mClip = new MenuItem("Open Clipboard");
        mFinish = new MenuItem("Finish");
        mn.add(mSave);
        mn.add(mLoad);
        mn.add(mClip);
        mn.add(mFinish);
        mSave.addActionListener(this);
        mLoad.addActionListener(this);
        mClip.addActionListener(this);
        mFinish.addActionListener(this);
        //north.add(mn); <-------//PROBLEM HERE

        clip.addActionListener(this);
        save.addActionListener(this);
        load.addActionListener(this);
        finish.addActionListener(this);
        ta = new TextArea(20, 80);
        center.add(ta);
        south.add(load);
        south.add(save);
        south.add(clip);
        south.add(finish);
        this.add(center, BorderLayout.CENTER);
        this.add(south, BorderLayout.SOUTH);
        this.setSize(600, 300);
        this.setVisible(true);
    }

    public void actionPerformed(ActionEvent ae) {
        if (ae.getSource() == save) {
            try {
                File junk = new File("junk.txt");
                FileWriter fw = new FileWriter(junk);
                fw.write(ta.getText()); // write whole TextArea contents
                fw.close();
            } catch (IOException ioe) {
            }
        }// ends if
        if (ae.getSource() == load) {
            String temp = "";
            try {
                File junk = new File("junk.txt");
                FileReader fr = new FileReader(junk);
                BufferedReader br = new BufferedReader(fr);
                while ((temp = br.readLine()) != null) {
                    ta.append(temp + "\n");
                }
                br.close();
            } catch (FileNotFoundException fnfe) {
            } catch (IOException ioe) {
            }
        }
        if (ae.getSource() == finish) {
            System.exit(0);
        }
        if(ae.getSource()==clip){
            new ClipBoard();
        }
    }

    class ClipBoard extends Frame {
        public ClipBoard() { // a constructor
            this.setTitle("Clipboard");
            this.setLayout(new FlowLayout());
            this.add(new TextArea(10, 50));
            this.setSize(400, 160);
            this.setVisible(true);
        }
    }
}
Brian
  • 187
  • 5
  • 15
  • `"Please notice I am using AWT and not Swing, so no JPanel/JFrame..."` -- duly noted, but I'm curious as to why? AWT has been out of date since 1998. Note that since so few folks use AWT now, you may find it hard to find experts to answer your questions. – Hovercraft Full Of Eels May 12 '14 at 22:45
  • *"Please notice I am using AWT and not Swing, so no JPanel/JFrame, etc."* ***Why*** AWT rather than Swing? See my answer on [Swing extras over AWT](http://stackoverflow.com/a/6255978/418556) for many good reasons to abandon using AWT components. – Andrew Thompson May 13 '14 at 07:19

2 Answers2

0
this.validate();

Swing components have a default state of being invalid and won't be painted to the screen unless validated (by calling the .validate() method on either the component itself or on one of the parent containers).

0

Just change this

 Panel north;

To this

MenuBar north;

Because using awt library , you cannot add Menu to Panel , however you can add Menu to MenuBar

MCHAppy
  • 1,012
  • 9
  • 15
  • how then, do I add a MenuBar to the application? – Brian May 12 '14 at 23:41
  • after changing " Panel north; " to " MenuBar north " , you should automatically change " north=new Panel(); " to " north=new MenuBar(); " and now your problem is resolved " north.add(mn); " – MCHAppy May 13 '14 at 07:35