Question

I need help on Java Swing, how to create a Java Swing program that can able...

I need help on Java Swing, how to create a Java Swing program that can able to Display all the name list from the text file then Display the Longest name...

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

CODE :

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

public class SwingPrg {
public static void main(String[] args) {
  
   // Creating File Chooser Dialog Object
JFileChooser chooseFile = new JFileChooser();
  
   // Creating JFrame
   JFrame jframe = new JFrame();
jframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
   //Set JFrame size
   jframe.setSize(400, 400);
   //Set JFrame layout
jframe.setLayout(new FlowLayout());
  
   // Label before TextArea of File Data
   JLabel lbl1 = new JLabel("FILE DATA : ");
  
   // TextArea to display file data
   JTextArea textarea = new JTextArea(20, 20);
   // LineWrap enabling for TextArea
   textarea.setLineWrap(true);
   // Set ReadOnly TextArea
   textarea.setEditable(false);
  
   // Label before TextArea of Longest Word
   JLabel lbl2 = new JLabel("Longest Word : ");
  
   // TextArea to display file longest word
   JTextArea textarea2 = new JTextArea(20, 10);
   // LineWrap enabling for TextArea
   textarea2.setLineWrap(true);
   // Set ReadOnly TextArea
   textarea2.setEditable(false);
  
   // Button for Browsing File
JButton btnFileBrowse = new JButton("OPEN FILE");
  
   // Button click action listener
   btnFileBrowse.addActionListener(ev -> {
       // Store file browse dialog return value
       int returnVal = chooseFile.showOpenDialog(jframe);
      
       // If file browsed from browse file dialog
       if (returnVal == JFileChooser.APPROVE_OPTION) {
          
           // File object to get selected file from browse dialog
           File file = chooseFile.getSelectedFile();
           try {
               // Reading data from file object and set data from file to textarea
               textarea.read(new InputStreamReader(new FileInputStream(file)), "Reading Data from File");
          
               // scanner object to read data from file
               Scanner scanner = new Scanner(file);

               // variable to store longest word
               String longWord = "";
                  
               // while file has lines  
               while (scanner.hasNextLine()) {
                  
                   // scanner object to read each word from line
                   Scanner wordScanner = new Scanner(scanner.nextLine());
                  
                   // loop while words present
                   while (wordScanner.hasNext()) {
                      
                       // store word to variable
                       String str = wordScanner.next();
                      
                       // compare with longWord value & store if word is longer than previous stored value
                       if(str.length() >= longWord.length())
                           longWord = str;
                   }
               }
              
               // Set longest word to textarea2
               textarea2.setText(longWord);
          
           } catch (Exception e) {
           e.printStackTrace();
           }
       }
});
  
   // Adding lable1 to jframe
   jframe.add(lbl1, BorderLayout.CENTER);
// Adding textarea for file data to jframe with scrollpane
   jframe.add(new JScrollPane(textarea), BorderLayout.CENTER);
   // Adding lable2 to jframe
   jframe.add(lbl2, BorderLayout.CENTER);
   // Adding textarea for file data to jframe with scrollpane
   jframe.add(new JScrollPane(textarea2), BorderLayout.CENTER);
// Adding file browse button to jframe
   jframe.add(btnFileBrowse, BorderLayout.PAGE_END);
jframe.pack();
   // Set jframe visible
jframe.setVisible(true);
}
}

OUTPUT:

Add a comment
Know the answer?
Add Answer to:
I need help on Java Swing, how to create a Java Swing program that can able...
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
  • Create a Java program that analyzes a program and produces the output into another file. The...

    Create a Java program that analyzes a program and produces the output into another file. The program must retrieve the path to the .java file. Once the path to the file is found the program must figure out the length of the longest line is in the program. For example, working with a program called MainFile.java. Once it figures out the longest line in the code it will put the output into MainFile.java.stats and it will be located in the...

  • I need help with my assignment. It is a java program. Please make it as simple...

    I need help with my assignment. It is a java program. Please make it as simple as you can. Create an ArrayList with elements the objects of previous class. Using a simple loop populate the ArrayList with data from some arrays with data, or from console. Use a loop with iterator and write the information in a File using PrintWriter. Use this address for the file (c:\\result\\MyData.txt). Use the Scanner class to display the content of the file on console.

  • PYTHON CODING HELP: BELOW ARE THE STEPS I NEED HELP WITH PLEASE. :) First create a...

    PYTHON CODING HELP: BELOW ARE THE STEPS I NEED HELP WITH PLEASE. :) First create a text file named "items.txt" that has the following data in this order: Potatoes Tomatoes Carrots ... and have it be in the same directory as the Python program you are coding for this assignment. IMPORTANT: Your program should work with any size file. It should work with 100 items listed or with no items in the file! Write a Python program that ... 1....

  • How can I create Loops and Files on Java? • Create a program that reads a...

    How can I create Loops and Files on Java? • Create a program that reads a list of names from a source file and writes those names to a CSV file. The source file name and target CSV file name should be requested from the user • The source file can have a variable number of names so your program should be dynamic enough to read as many names as needed • When writing your CSV file, the first row...

  • I need help parsing a large text file in order to create a map using Java....

    I need help parsing a large text file in order to create a map using Java. I have a text file named weather_report.txt which is filled with hundreds of different indexes. For example: one line is "POMONA SUNNY 49 29 46 NE3 30.46F". There are a few hundred more indexes like that line with different values in the text file and they are not delimited by commas but instead by spaces. Therefore, in this list of indexes we only care...

  • I need help building code in python for this: Create a program that: Creates a sales...

    I need help building code in python for this: Create a program that: Creates a sales receipt, displays the receipt entries and totals, and saves the receipt entries to a file Prompt the user to enter the Item Name Item Quantity Item Price Display the item name, the quantity, and item price, and the extended price (Item Quantity multiplied by Item Price) after the entry is made Save the item name, quantity, item price, and extended price to a file...

  • Need Java help: 1. Create a Java program that accepts input String input from a user...

    Need Java help: 1. Create a Java program that accepts input String input from a user of first name. 2. Create a Java program that accepts input String input from a user of last name. 3. Concatenate the Strings in a full name variable and print to the console.

  • I need help with the following. I need to write a program code in Java using...

    I need help with the following. I need to write a program code in Java using NetBeans. It is from How to Program Java book Chapter 2. This program should calculate the packing of Candles. First read: The number of candles The number of candles that fit in each case Then calculate and print: The number of full cases The number of candles left over (partial case) If this order is large (more than 5 full cases needed) An extra...

  • Write a java program to create a student file which includes first name, last name, and...

    Write a java program to create a student file which includes first name, last name, and GPA. Write another Java program to open the students file. Display the students list and calculate the average GPA of the class.  

  • JAVA We are learning about java databases in my highschool java class. I Need some help...

    JAVA We are learning about java databases in my highschool java class. I Need some help with it. Create a Java program (feel free to do it all in a main method) that works with a database that contains your 'Contractors' table (see the last exercise). Id (integer, primary key) CompanyName (varchar, 30 characters) Phone(varchar, 12 characters) ContactName(varchar, 30 characters) Rating (integer) OutOfStateService (boolean) # company name phone # Name rating Out of state service 1 Joe's Brewery 1111111111 Joe...

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