I am trying to add Jtext area to a Java application, load text from a Notepad file & allow user to change color of text & make text invisible/visible using Menu bar. I am having trouble with the 1st part--loading the contents of a text file. Any help would be appreciated.

import java.util.Scanner;
import java.io.*;

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

/**
   The MenuWindow class demonstrates a menu system.
*/

public class MenuWindow extends JFrame
{
   private JLabel messageLabel; // Displays a message
   private final int LABEL_WIDTH = 1024;  // Label's width
   private final int LABEL_HEIGHT = 768; // Label's height

   // The following will reference menu components.
   private JMenuBar menuBar;    // The menu bar
   private JMenu fileMenu;      // The File menu
   private JMenu textMenu;      // The Text menu
   private JMenuItem exitItem;  // To exit
   private JRadioButtonMenuItem blackItem; // Makes text black
   private JRadioButtonMenuItem redItem;   // Makes text red
   private JRadioButtonMenuItem blueItem;  // Makes text blue
   private JCheckBoxMenuItem visibleItem;  // Toggle visibility
   private JTextArea inputText;

   /**
      Constructor
   */

   public MenuWindow(String str) throws IOException
   {
       JTextArea inputText = new JTextArea();
inputText.setBackground(null);
inputText.setEditable(false);


inputText.read(reader, description);

JScrollPane scroll = new JScrollPane(inputText);
add(scroll);

            //  File file = new File(str);
       //Scanner inputFile = new Scanner(file);

       //String line = inputFile.nextLine();
       //inputFile.close();

      // Set the title.
      setTitle("Example Menu System");

      // Specify an action for the close button.
      setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

      // Create the messageLabel label.
      //messageLabel = new JLabel(line, SwingConstants.CENTER);

      // Set the label's preferred size.
      messageLabel.setPreferredSize(
             new Dimension(LABEL_WIDTH, LABEL_HEIGHT));

      // Set the label's foreground color.
      messageLabel.setForeground(Color.black);

      // Add the label to the content pane.
      add(messageLabel);

      // Build the menu bar.
      buildMenuBar();

      // Pack and display the window.
      pack();
      setVisible(true);
   }

   /**
      The buildMenuBar method builds the menu bar.
   */

   private void buildMenuBar()
   {
      // Create the menu bar.
      menuBar = new JMenuBar();

      // Create the file and text menus.
      buildFileMenu();
      buildTextMenu();

      // Add the file and text menus to the menu bar.
      menuBar.add(fileMenu);
      menuBar.add(textMenu);

      // Set the window's menu bar.
      setJMenuBar(menuBar);
   }

   /**
      The buildFileMenu method builds the File menu
      and returns a reference to its JMenu object.
   */

   private void buildFileMenu()
   {
      // Create an Exit menu item.
      exitItem = new JMenuItem("Exit");
      exitItem.setMnemonic(KeyEvent.VK_X);
      exitItem.addActionListener(new ExitListener());

      // Create a JMenu object for the File menu.
      fileMenu = new JMenu("File");
      fileMenu.setMnemonic(KeyEvent.VK_F);

      // Add the Exit menu item to the File menu.
      fileMenu.add(exitItem);
   }

   /**
      The buildTextMenu method builds the Text menu
      and returns a reference to its JMenu object.
   */

   private void buildTextMenu()
   {
      // Create the radio button menu items to change
      // the color of the text. Add an action listener
      // to each one.
      blackItem = new JRadioButtonMenuItem("Black", true);
      blackItem.setMnemonic(KeyEvent.VK_B);
      blackItem.addActionListener(new ColorListener());

      redItem = new JRadioButtonMenuItem("Red");
      redItem.setMnemonic(KeyEvent.VK_R);
      redItem.addActionListener(new ColorListener());

      blueItem = new JRadioButtonMenuItem("Blue");
      blueItem.setMnemonic(KeyEvent.VK_U);
      blueItem.addActionListener(new ColorListener());

      // Create a button group for the radio button items.
      ButtonGroup group = new ButtonGroup();
      group.add(blackItem);
      group.add(redItem);
      group.add(blueItem);

      // Create a check box menu item to make the text
      // visible or invisible.
      visibleItem = new JCheckBoxMenuItem("Visible", true);
      visibleItem.setMnemonic(KeyEvent.VK_V);
      visibleItem.addActionListener(new VisibleListener());

      // Create a JMenu object for the Text menu.
      textMenu = new JMenu("Text");
      textMenu.setMnemonic(KeyEvent.VK_T);

      // Add the menu items to the Text menu.
      textMenu.add(blackItem);
      textMenu.add(redItem);
      textMenu.add(blueItem);
      textMenu.addSeparator(); // Add a separator bar.
      textMenu.add(visibleItem);
   }

   /**
      Private inner class that handles the event that
      is generated when the user selects Exit from
      the File menu.
   */

    private class ExitListener implements ActionListener
    {
       public void actionPerformed(ActionEvent e)
       {
          System.exit(0);
       }
    }

   /**
      Private inner class that handles the event that
      is generated when the user selects a color from
      the Text menu.
   */

    private class ColorListener implements ActionListener
    {
       public void actionPerformed(ActionEvent e)
       {
          if (blackItem.isSelected())
             messageLabel.setForeground(Color.black);
          else if (redItem.isSelected())
             messageLabel.setForeground(Color.red);
          else if (blueItem.isSelected())
             messageLabel.setForeground(Color.blue);
       }
    }

   /**
      Private inner class that handles the event that
      is generated when the user selects Visible from
      the Text menu.
   */

   private class VisibleListener implements ActionListener
   {
      public void actionPerformed(ActionEvent e)
      {
         if (visibleItem.isSelected())
            messageLabel.setVisible(true);
         else
            messageLabel.setVisible(false);
      }
   }

   /**
      The main method creates an instance of the
      MenuWindow class, which causes it to display
      its window.
   */

   public static void main(String[] args) throws IOException
   {
       String filename = JOptionPane.showInputDialog(null, "Enter a Filename:");

       new MenuWindow(filename);
   }
}

Recommended Answers

All 3 Replies

Post using code tags in the future. It's the little button that looks like a # sign on the reply box. Put your code inbetween the brackets that show up after you click it. Anyway, you should also give more information here. Can you not read from the file, or can you not put the info you read from the file into the JTextArea? And where [what method] are you attempting to put the info into the JTextArea, if you got that far yet?

Getting the contents of a textfile copied into the JTextArea is what I have been having problems with. Thanks!

The code abouve does not compile because the reader and description varialbes are not declared.

inputText.read(reader, description);

the messageLabel is olso not initilized the NullPointerException is thrown at this statement:

messageLabel.setPreferredSize(
new Dimension(LABEL_WIDTH, LABEL_HEIGHT));

and there is no code that read the data from file the MenuWindow class.

hope it helps.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.