Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert a console app to a swing app [closed]

Tags:

java

swing

As a beginner Java programmer I would like some help as to understanding the best way or technique for building a swing UI for my console app.

The console app works well now I would like to convert it to a swing app. I would like to have a swing JTextArea in a JScrollPane that the user can edit to input their string then click a countwords button and get an output as a int.

The code for my console app and my attempt to the swing app as well is below. I have spent quite a bit of time trying to research this but I can't seem to get it right.

My console app :

 import java.util.*;
 public class WordCounter{

 public static void main(String args[])
    {
     // Create Scanner object
     Scanner s=new Scanner(System.in);

    // Read a string
     String st=s.nextLine();

     //Split string with space
     String words[]=st.trim().split(" ");

     System.out.println("No. of words "+words.length);
    }
  }

My attempt at Swing :

import java.io.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.JTextArea;

import java.util.*;

class Countswords extends JPanel {

    public Countswords() {
        myTextArea();

    }

    private void myTextArea() {
        this.setLayout(new BorderLayout());
        this.setPreferredSize(new Dimension(400, 200));

        JTextArea textArea = new JTextArea(5, 40);
        JScrollPane scrollPane = new JScrollPane(textArea);
        // textArea.setEditable(true);
        JTextArea.setText(userInput);

    }

    public static void showFrame() {
        JPanel panel = new Countswords();
        panel.setOpaque(true);

        JFrame frame = new JFrame("My Text Area");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setContentPane(panel);
        frame.pack();
        frame.setVisible(true);
    }

    public static void main(String args[]) {
        // Create Scanner object
        // Scanner s=new Scanner(System.in);

        // Read a string
        // String st=s.nextLine();

        // Split string with space
        // String words[]=st.trim().split(" ");

        // System.out.println("No. of words "+words.length);
        SwingUtilities.invokeLater(new Runnable() {

            public void run() {
                Countswords.showFrame();
            }
        });
    }
}
like image 491
Reign Avatar asked Oct 21 '22 06:10

Reign


1 Answers

You can make use of this code to make your app as Swing based.

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

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

public class CountWords extends JFrame {
JTextArea   textArea;
JScrollPane scrollPane;
Container   container;

JPanel      panel;
JLabel      label;

JButton     button;

public CountWords() {
    initialize();
    addToPanel();
}

public void initialize() {
    container = getContentPane();
    textArea = new JTextArea(5,20);
    scrollPane = new JScrollPane(textArea);

    button = new JButton(" Count Words ");

    panel = new JPanel();
    label = new JLabel(" Total Words : ");

    label.setBackground(Color.yellow);
}

public void addToPanel() {

    panel.add(button);

    container.add(scrollPane, BorderLayout.NORTH);
    container.add(panel, BorderLayout.SOUTH);
    container.add(label, BorderLayout.CENTER);

    button.addActionListener(new ActionListener(){
        public void actionPerformed(ActionEvent ae) {
            label.setText("No.of Words : "+ getTotalWords(textArea.getText()));
        }
    });
}

public int getTotalWords( String text ){
    String words[]= text.trim().split(" ");
    return words.length;
}
public static void main(String args[]) {
    CountWords cw = new CountWords();
    cw.setDefaultCloseOperation(3); // JFrame.EXIT_ON_CLOSE => 3
    cw.pack();
    cw.setSize(400,300);
    cw.setVisible(true);
}
}
like image 127
chandra Avatar answered Oct 24 '22 10:10

chandra