Results 1 to 3 of 3
  1. #1
    Amhisaw12 is offline Member
    Join Date
    Mar 2014
    Posts
    3
    Rep Power
    0

    Default BlueJ Assignment Help

    Hello!
    I am quite new to Java programming. I'm actually taking a class and I have an assignment due tonight. The program below compiles and functions correctly for the most part. It is supposed to be a simple GUI Text Editor with Edit and Format functions. The content pane is supposed to be a scroll pane, however no matter how much I type into the editor, the scroll bar does not appear. Also, the none of the functions under the "Edit" menu work. Can anyone provide any assistance?

    Java Code:
    import java.awt.*;
    import javax.swing.*;
    import java.awt.event.*;
    import java.io.*;
    import java.util.*;
    import java.util.StringTokenizer;
    import java.util.regex.Pattern;
    
    /**
     * A simple text editor. It creates and displays the GUI Text Editor.
     * 
     * @author Amber Hisaw
     * @version March 22, 2014
     */
    public class TextEditor extends JFrame implements ActionListener
    {
        // instance variables - replace the example below with your own
        private JFrame frame;
        private JPanel mainpanel;
        private JTextArea textArea;
        private JMenuBar menubar;
        private JMenu edit, format, font, fontsty, fontsize;
        private JMenuItem cut, copy, paste, delete, select, wordcount;
        private JMenuItem fname1, fname2, fname3, fname4;
        private JMenuItem fstyle1, fstyle2, fstyle3, fstyle4;
        private JMenuItem fsize1, fsize2, fsize3, fsize4;
        private Font f;
        private String command, str;
        private int i, pos1;
    
        /**
         * Constructor for objects of class TextEditor
         */
        public TextEditor()
        {
            textManipulate();
        }
    
        /**
         * Manipulates the text area.
         */
        public void textManipulate()
        {
            frame = new JFrame("TextEditor");
            Container contentPane = frame.getContentPane();
            
            contentPane.setLayout(new FlowLayout());
            
            menubar = new JMenuBar();
            frame.setJMenuBar(menubar);
            
            edit = new JMenu("Edit");
            format = new JMenu("Format");
            font = new JMenu("Font");
            fontsty = new JMenu("Font Style");
            fontsize = new JMenu("Size");
            
            edit.add(cut = new JMenuItem("Cut (Ctrl+X)"));
            edit.add(copy = new JMenuItem("Copy (Ctrl+C)"));
            edit.add(paste = new JMenuItem("Paste (Ctrl+V)"));
            edit.add(delete = new JMenuItem("Delete"));
            edit.add(select = new JMenuItem("Select All (Ctrl+A)"));
            edit.add(wordcount = new JMenuItem("Word Count"));
            menubar.add(edit);
            
            format.add(font);
            format.add(fontsty);
            format.add(fontsize);
            
            font.add(fname1 = new JMenuItem("Courier"));
            font.add(fname2 = new JMenuItem("Sans Serif"));
            font.add(fname3 = new JMenuItem("Monospaced"));
            font.add(fname4 = new JMenuItem("Symbol"));
            
            fontsty.add(fstyle1 = new JMenuItem("Regular"));
            fontsty.add(fstyle2 = new JMenuItem("Bold"));
            fontsty.add(fstyle3 = new JMenuItem("Italic"));
            fontsty.add(fstyle4 = new JMenuItem("Bold Italic"));
            
            fontsize.add(fsize1 = new JMenuItem("12"));
            fontsize.add(fsize2 = new JMenuItem("14"));
            fontsize.add(fsize3 = new JMenuItem("18"));
            fontsize.add(fsize4 = new JMenuItem("20"));
            
            menubar.add(format);
            
            cut.addActionListener(this);
            copy.addActionListener(this);
            paste.addActionListener(this);
            delete.addActionListener(this);
            select.addActionListener(this);
            wordcount.addActionListener(this);
            
            fname1.addActionListener(this);
            fname2.addActionListener(this);
            fname3.addActionListener(this);
            fname4.addActionListener(this);
            
            fstyle1.addActionListener(this);
            fstyle2.addActionListener(this);
            fstyle3.addActionListener(this);
            fstyle4.addActionListener(this);
            
            fsize1.addActionListener(this);
            fsize2.addActionListener(this);
            fsize3.addActionListener(this);
            fsize4.addActionListener(this);
            
            textArea = new JTextArea(5, 20);
            
            JScrollPane scrollPane = new JScrollPane(textArea);
            
            textArea.setEditable(true);
            f= new Font ("Monotype Corsiva" , Font.PLAIN,15);
            textArea.setFont(f);
            textArea.setLineWrap(true);
            textArea.setWrapStyleWord(true);
            
            scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
            scrollPane.setPreferredSize(new Dimension (250, 250));
            
            contentPane.add(textArea);
            
            frame.pack();
            frame.setVisible(true);
        }
        public void actionPerformed(ActionEvent ae)
        {
            command = (String)ae.getActionCommand();
            
            if(command.equals("Cut"))
            {
                str = textArea.getSelectedText();
                i = textArea.getText() .indexOf(str);
                textArea.replaceRange(" ", i, i + str.length());
            }
            if(command.equals("Copy"))
            {
                str = textArea.getSelectedText();
            }
            if(command.equals("Paste"))
            {
                pos1 = textArea.getCaretPosition();
                textArea.insert(str,pos1);
            }
            if(command.equals("Delete"))
            {
                String msg = textArea.getSelectedText();
                i = textArea.getText() .indexOf(msg);
                textArea.replaceRange(" ", i, i + msg.length());
            }
            if(command.equals("Word Count"))
            {
                String data = textArea.getText();
                Scanner s = new Scanner(data);
                Pattern p = Pattern.compile(" ");
                String words = null;
                int count = 0;
                while (s.hasNext())
                {
                    words = s.next();
                    count += 1;
                }
                JOptionPane.showMessageDialog(null, "Word Count:" + count);
            }
            if(command.equals("Courier"))
            {
                String fontName = f.getName();
                String fontFamily = f.getFamily();
                int fontSize = f.getSize();
                int fontStyle = f.getStyle();
                
                f = new Font ("Courier", fontStyle,fontSize);
                textArea.setFont(f);
            }
            if(command.equals("Sans Serif"))
            {
                String fontName = f.getName();
                String fontFamily = f.getFamily();
                int fontSize = f.getSize();
                int fontStyle = f.getStyle();
                
                f = new Font("Sans Serif",fontStyle,fontSize);
                textArea.setFont(f);
            }
            if(command.equals("Monospaced"))
            {
                String fontName = f.getName();
                String fontFamily = f.getFamily();
                int fontSize = f.getSize();
                int fontStyle = f.getStyle();
                
                f = new Font("Monospaced",fontStyle,fontSize);
                textArea.setFont(f);
            }
            if(command.equals("Symbol"))
            {
                String fontName = f.getName();
                String fontFamily = f.getFamily();
                int fontSize = f.getSize();
                int fontStyle = f.getStyle();
                
                f = new Font("Symbol",fontStyle,fontSize);
                textArea.setFont(f);
                System.out.println(f.getFamily());
            }
            if(command.equals("Regular"))
            {
                String fontName = f.getName();
                String fontFamily = f.getFamily();
                int fontSize = f.getSize();
                int fontStyle = f.getStyle();
                
                f = new Font(fontName,Font.PLAIN,fontSize);
                textArea.setFont(f);
            }
            if(command.equals("Bold"))
            {
              String fontName = f.getName();
              String fontFamily = f.getFamily();
              int fontSize = f.getSize();
              int fontStyle = f.getStyle();
                
              f = new Font(fontName,Font.BOLD,fontSize);
              textArea.setFont(f);
            }
            if(command.equals("Italic"))
            {
                String fontName = f.getName();
                String fontFamily = f.getFamily();
                int fontSize = f.getSize();
                int fontStyle = f.getStyle();
                
                f = new Font(fontName,Font.ITALIC,fontSize);
                textArea.setFont(f);
            }
            if(command.equals("Bold Italic"))
            {
                String fontName = f.getName();
                String fontFamily = f.getFamily();
                int fontSize = f.getSize();
                int fontStyle = f.getStyle();
                
                f = new Font(fontName,Font.BOLD|Font.ITALIC,fontSize);
                textArea.setFont(f);
            }
            if(command.equals("12"))
            {
                String fontName = f.getName();
                String fontFamily = f.getFamily();
                int fontSize = f.getSize();
                int fontStyle = f.getStyle();
                
                f = new Font(fontName,fontStyle,12);
                textArea.setFont(f);
            }
            if(command.equals("14"))
            {
                String fontName = f.getName();
                String fontFamily = f.getFamily();
                int fontSize = f.getSize();
                int fontStyle = f.getStyle();
                
                f = new Font(fontName,fontStyle,14);
                textArea.setFont(f);
            }
            if(command.equals("18"))
            {
                String fontName = f.getName();
                String fontFamily = f.getFamily();
                int fontSize = f.getSize();
                int fontStyle = f.getStyle();
                
                f = new Font(fontName,fontStyle,18);
                textArea.setFont(f);
            }
            if(command.equals("20"))
            {
                String fontName = f.getName();
                String fontFamily = f.getFamily();
                int fontSize = f.getSize();
                int fontStyle = f.getStyle();
                
                f = new Font(fontName,fontStyle,20);
                textArea.setFont(f);
            }
            if(command.equals("Select All"))
            {
                String strText = textArea.getText();
                int strLen = strText.length();
                textArea.select(0, strLen);
            }
        }
    }

  2. #2
    pj6444 is offline Senior Member
    Join Date
    Jan 2013
    Posts
    164
    Rep Power
    2

    Default Re: BlueJ Assignment Help

    So the problem with your scroll bar is that you never actually added it to the panel. When you use a scroll pane, instead of adding the objects inside of it, in your case "textArea", you add the scroll pane itself. So on line 122 replace textArea with your scroll pane.

  3. #3
    Amhisaw12 is offline Member
    Join Date
    Mar 2014
    Posts
    3
    Rep Power
    0

    Default Re: BlueJ Assignment Help

    Thank you so much! I just literally had to change two words and that fixed the scroll bar!

Similar Threads

  1. BlueJ IDE
    By jcbmack in forum New To Java
    Replies: 9
    Last Post: 08-08-2012, 07:26 AM
  2. bluej help
    By xpanhol in forum Java Applets
    Replies: 4
    Last Post: 01-29-2012, 12:46 AM
  3. BlueJ - UML
    By Nghi97 in forum New To Java
    Replies: 5
    Last Post: 11-28-2011, 11:03 AM
  4. Need help with blueJ
    By samd89 in forum Other IDEs
    Replies: 6
    Last Post: 12-06-2010, 04:59 PM
  5. BlueJ 2.2.0
    By JavaBean in forum Java Software
    Replies: 0
    Last Post: 07-07-2007, 02:28 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •