Question

I want to make the JText area scrollable.. but my result doesn't work in that way....

I want to make the JText area scrollable.. but my result doesn't work in that way. help me plz.

package m5;

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;

public class TextAnalyzer extends JFrame implements ActionListener
{

   //Initializes component variables for the frame
  
   //For text typing area
   JLabel type = new JLabel("Type sentences in the box below.", JLabel.CENTER);
   JTextArea txt = new JTextArea(10, 10);

   JPanel txt1 = new JPanel();
  
   //For Statistic data area with a panel
   JLabel nwords = new JLabel("Number of words: ");
   JPanel nwords2 = new JPanel();
  
   JLabel lwords = new JLabel("Average words length: ");
   JPanel lwords2 = new JPanel();
  
   JLabel nchars = new JLabel("Number of characters: ");
   JPanel nchars2 = new JPanel();
  
   JButton button = new JButton("Show Statistics!");

   JPanel stat = new JPanel();
  
  
   public TextAnalyzer()
   {
       setTitle("COMP Assigmnet M5-1");
       type.setFont(new Font("Helvetica", Font.BOLD, 15));
      
      
       //Sets texts in the JTextArea would be wrapped
       txt.setLineWrap(true);
       txt.setWrapStyleWord(true);
      
      
       //Sets BorderLayout as a layout for the text typing area
       txt1.setLayout(new BorderLayout());
       txt1.setAlignmentX(Component.CENTER_ALIGNMENT);
       txt1.add(type, BorderLayout.NORTH);
       txt1.add(txt, BorderLayout.CENTER);       
      
      
       //Sets BoxLayout as a layout for the statistics area
       //Added a titled border using BorderFactory
       //Added small area between labels using Box.createRigidArea methods
       Box statBox = new Box(BoxLayout.Y_AXIS);
       statBox.setAlignmentX(Component.CENTER_ALIGNMENT);
       setLayout(new BoxLayout(statBox, BoxLayout.Y_AXIS));
       statBox.setBorder(BorderFactory.createTitledBorder("Statistics"));
       statBox.add(Box.createRigidArea(new Dimension(8,5)));
       statBox.add(nwords);
       statBox.add(Box.createRigidArea(new Dimension(8,5)));
       statBox.add(nchars);
       statBox.add(Box.createRigidArea(new Dimension(8,5)));
       statBox.add(lwords);
       statBox.add(Box.createRigidArea(new Dimension(8,20)));
       statBox.add(button);
       statBox.add(Box.createRigidArea(new Dimension(8,20)));
      
      
       //Sets BorderLayout as a layout for the whole window
       setLayout(new BorderLayout());
       add(txt1, BorderLayout.CENTER); add(statBox, BorderLayout.SOUTH);
  
      
       //Sets button as a action listener
       button.addActionListener(this);
      
       //Sets for the window to be closed by clicking 'x' button on the window
       setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
   }
  
  
   //Defines actionPerfomed methods with a possible exception
   //Defines actions that would happen when the listener events occur
   public void actionPerformed(ActionEvent evt) throws ArithmeticException
   {
       //Sets input segment as a input data of text area
       //Makes a string list 'input' for the scanner to save segments in that obtained by next() method
       String segment = txt.getText();
       Scanner scan = new Scanner(segment);
       ArrayList <String> input = new ArrayList<String>();
       int wordLength = 0;

      
       //Receive number of words by getting the size of the arraylist
       while (scan.hasNext())
           input.add(scan.next());
       nwords.setText("Number of words: " + input.size());
      
      
       //Iterates items inside the arraylist and accumulates the number of whole characters by string.length() method to get number of characters
       for (String items: input)
           wordLength += items.length();
       nchars.setText("Number of characters: " + wordLength);
      
      
       //Gets average words length by dividing number of characters by number of words
       //Uses try and catch block to catch a exception that could be thrown when the nothing was written in the text area
       double a = 0;
       try
       { a = wordLength/input.size(); }
       catch (ArithmeticException problem)
   { a = 0; }
       finally
       { lwords.setText("Average words length: " + a); }
   }
  
   //Initiates an object of TextAnalyzer as a JFrame and sets its size
   public static void main(String[] args)
   {
       TextAnalyzer app = new TextAnalyzer();
       app.pack();
       app.setSize(500, 400);
       app.setVisible(true);
   }
  
}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

/*****************************TextAnalyzer.java***********************/

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;

public class TextAnalyzer extends JFrame implements ActionListener {

   // Initializes component variables for the frame

   // For text typing area
   JLabel type = new JLabel("Type sentences in the box below.", JLabel.CENTER);
   JTextArea txt = new JTextArea(10, 10);
   JScrollPane scroll = new JScrollPane(txt);//add scrollPane
   JPanel txt1 = new JPanel();

   // For Statistic data area with a panel
   JLabel nwords = new JLabel("Number of words: ");
   JPanel nwords2 = new JPanel();

   JLabel lwords = new JLabel("Average words length: ");
   JPanel lwords2 = new JPanel();

   JLabel nchars = new JLabel("Number of characters: ");
   JPanel nchars2 = new JPanel();

   JButton button = new JButton("Show Statistics!");

   JPanel stat = new JPanel();

   public TextAnalyzer() {
       setTitle("COMP Assigmnet M5-1");
       type.setFont(new Font("Helvetica", Font.BOLD, 15));

       // Sets texts in the JTextArea would be wrapped
       txt.setLineWrap(true);
       txt.setWrapStyleWord(true);

       // Sets BorderLayout as a layout for the text typing area
       txt1.setLayout(new BorderLayout());
       txt1.setAlignmentX(Component.CENTER_ALIGNMENT);
       txt1.add(type, BorderLayout.NORTH);
       txt1.add(scroll, BorderLayout.CENTER);

       // Sets BoxLayout as a layout for the statistics area
       // Added a titled border using BorderFactory
       // Added small area between labels using Box.createRigidArea methods
       Box statBox = new Box(BoxLayout.Y_AXIS);
       statBox.setAlignmentX(Component.CENTER_ALIGNMENT);
       setLayout(new BoxLayout(statBox, BoxLayout.Y_AXIS));
       statBox.setBorder(BorderFactory.createTitledBorder("Statistics"));
       statBox.add(Box.createRigidArea(new Dimension(8, 5)));
       statBox.add(nwords);
       statBox.add(Box.createRigidArea(new Dimension(8, 5)));
       statBox.add(nchars);
       statBox.add(Box.createRigidArea(new Dimension(8, 5)));
       statBox.add(lwords);
       statBox.add(Box.createRigidArea(new Dimension(8, 20)));
       statBox.add(button);
       statBox.add(Box.createRigidArea(new Dimension(8, 20)));

       // Sets BorderLayout as a layout for the whole window
       setLayout(new BorderLayout());
       add(txt1, BorderLayout.CENTER);
       add(statBox, BorderLayout.SOUTH);

       // Sets button as a action listener
       button.addActionListener(this);

       // Sets for the window to be closed by clicking 'x' button on the window
       setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
   }

   // Defines actionPerfomed methods with a possible exception
   // Defines actions that would happen when the listener events occur
   public void actionPerformed(ActionEvent evt) throws ArithmeticException {
       // Sets input segment as a input data of text area
       // Makes a string list 'input' for the scanner to save segments in that obtained
       // by next() method
       String segment = txt.getText();
       Scanner scan = new Scanner(segment);
       ArrayList<String> input = new ArrayList<String>();
       int wordLength = 0;

       // Receive number of words by getting the size of the arraylist
       while (scan.hasNext())
           input.add(scan.next());
       nwords.setText("Number of words: " + input.size());

       // Iterates items inside the arraylist and accumulates the number of whole
       // characters by string.length() method to get number of characters
       for (String items : input)
           wordLength += items.length();
       nchars.setText("Number of characters: " + wordLength);

       // Gets average words length by dividing number of characters by number of words
       // Uses try and catch block to catch a exception that could be thrown when the
       // nothing was written in the text area
       double a = 0;
       try {
           a = wordLength / input.size();
       } catch (ArithmeticException problem) {
           a = 0;
       } finally {
           lwords.setText("Average words length: " + a);
       }
   }

   // Initiates an object of TextAnalyzer as a JFrame and sets its size
   public static void main(String[] args) {
       TextAnalyzer app = new TextAnalyzer();
       app.pack();
       app.setSize(500, 400);
       app.setVisible(true);
   }

}

/********************output*********************/

Please let me know if you have any doubt or modify the answer, Thanks :)

Add a comment
Know the answer?
Add Answer to:
I want to make the JText area scrollable.. but my result doesn't work in that way....
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • With the Code below, how would i add a "Back" Button to the bottom of the...

    With the Code below, how would i add a "Back" Button to the bottom of the history page so that it takes me back to the main function and continues to work? import java.awt.BorderLayout; import java.awt.Color; import java.awt.Dimension; import java.awt.GridLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.io.File; import java.io.FileNotFoundException; import java.io.FileWriter; import java.io.IOException; import java.util.ArrayList; import java.util.List; import java.util.Scanner; import javax.swing.BoxLayout; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.JScrollPane; import javax.swing.JTextField; public class RecipeFinder { public static void main(String[]...

  • Please fix and test my code so that all the buttons and function are working in...

    Please fix and test my code so that all the buttons and function are working in the Sudoku. CODE: SudokuLayout.java: import java.awt.BorderLayout; import java.awt.Color; import java.awt.Dimension; import java.awt.GridLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.BorderFactory; import javax.swing.Box; import javax.swing.BoxLayout; import javax.swing.JButton; import javax.swing.JComboBox; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.JTextArea; import javax.swing.JTextField; public class SudokuLayout extends JFrame{ JTextArea txtArea;//for output JPanel gridPanel,butPanel;//panels for sudoku bord and buttons JButton hint,reset,solve,newPuzzel;//declare buttons JComboBox difficultyBox; SudokuLayout() { setName("Sudoku Bord");//set name of frame // setTitle("Sudoku Bord");//set...

  • Add a timer to the lightbulb ON/OFF program such that the bulb stays on for 10...

    Add a timer to the lightbulb ON/OFF program such that the bulb stays on for 10 seconds after the ON button is pushed and then goes off.   Put a new label on the lightbulb panel that counts the number of seconds the bulb is on and displays the number of seconds as it counts up from 0 to 10. THIS IS A JAVA PROGRAM. The image above is what it should look like. // Demonstrates mnemonics and tool tips. //********************************************************************...

  • I got this programming its used to highlight text, but i couldn't remove highlight again from...

    I got this programming its used to highlight text, but i couldn't remove highlight again from the text. i need someone to write me code that removes text highlight here is my code import java.awt.*; import java.awt.event.*; import java.util.Map; import java.util.HashMap; import javax.swing.*; import javax.swing.text.*; public class TextHighlight {    private JTextArea textarea;    private JComboBox colorbox;    private JTextField top;    private String[] colorOptions = { "GRAY", "YELLOW", "RED", "CYAN", "ORANGE", "PINK" };    private Highlighter.HighlightPainter grayPainter;    private...

  • I have this problem for a final thats in my book thats i'm stuck on ....

    I have this problem for a final thats in my book thats i'm stuck on . Any help would be really appreciated. Design and code a Swing GUI to translate text that is input in English into Pig Latin. You can assume that the sentence contains no punctuation. The rules for Pig Latin are as follows: ⦁ For words that begin with consonants, move the leading consonant to the end of the word and add “ay.” For example, “ball” becomes...

  • I want the content of the combo box updates with the corresponding radiobutton, how can I write the two radioBut...

    I want the content of the combo box updates with the corresponding radiobutton, how can I write the two radioButton actionListeners? and mycomboBox addItemListener? There are 4 GUI components at the top (NORTH) of the ContentPane of the JFrame, label, two radio buttons, and a combo box. The combo box is empty. When a user has clicked a radio button, the content of the combo box will be update You will need to use the setModel method and set the...

  • i want answers please ??? Answers to Self-Review Exercises 625 14.3 d) In a BorderLayout, two...

    i want answers please ??? Answers to Self-Review Exercises 625 14.3 d) In a BorderLayout, two buttons added to the NORTH region will be placed side by side. e) A maximum of five components can be added to a BorderLayout ) Inner classes are not allowed to access the members of the enclosing class. B) A TextArea's text is always read-only. h) Class TextArea is a direct subclass of class Component. Find the error(s) in each of the following statements,...

  • JAVA Hello I am trying to add a menu to my Java code if someone can...

    JAVA Hello I am trying to add a menu to my Java code if someone can help me I would really appreacite it thank you. I found a java menu code but I dont know how to incorporate it to my code this is the java menu code that i found. import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.ButtonGroup; import javax.swing.JCheckBoxMenuItem; import javax.swing.JFrame; import javax.swing.JMenu; import javax.swing.JMenuBar; import javax.swing.JMenuItem; import javax.swing.JRadioButtonMenuItem; public class MenuExp extends JFrame { public MenuExp() { setTitle("Menu Example");...

  • I want the content of the combo box updates with the corresponding radiobutton, how can I...

    I want the content of the combo box updates with the corresponding radiobutton, how can I write the two radioButton actionListeners? and mycomboBox addItemListener? There are 4 GUI components at the top (NORTH) of the ContentPane of the JFrame, label, two radio buttons, and a combo box. The combo box is empty. When a user has clicked a radio button, the content of the combo box will be update You will need to use the setModel method and set the...

  • I have been messing around with java lately and I have made this calculator. Is there...

    I have been messing around with java lately and I have made this calculator. Is there any way that I would be able to get a different sound to play on each different button 1 - 9 like a nokia phone? Thank you. *SOURCE CODE* import java.awt.*; import java.awt.event.*; import javax.swing.*; import javax.swing.event.*; class Calculator extends JFrame { private final Font BIGGER_FONT = new Font("monspaced",Font.PLAIN, 20); private JTextField textfield; private boolean number = true; private String equalOp = "="; private...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT