Home C C++ Java Python Perl PHP SQL JavaScript Linux Online Test Forum

Home » Java » Java SWT GUI Project to count words

Java SWT GUI Project to count words in text

This GUI project has one text box to enter text, Submit button to count words, clear button to clear the input text from text box, and a lable to display text.


import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.ScrolledComposite;
import org.eclipse.swt.events.MouseAdapter;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.graphics.Font;
import org.eclipse.swt.graphics.FontData;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;

public class Main {

	public class WordCounter {
	private Display display;
	private Shell shell;
	private Text txt1;
	private Button btnClear;
	private Button btnSubmit;
	private Text text;
	private Label lbl1;

	private Group grpStart;
	private ScrolledComposite scrolledComposite;
	private Composite shellComposite;

	public WordCounter() {
		display = new Display();
		shell = new Shell(display);
		shell.setSize(700, 400);
		shell.setText("Word Counter - CppBuzz [ Java & SWT ]");

		shell.setLayout(new FillLayout());
		scrolledComposite = new ScrolledComposite(shell, SWT.H_SCROLL | SWT.V_SCROLL);
		scrolledComposite.setExpandVertical(true);
		scrolledComposite.setExpandHorizontal(true);
		shellComposite = new Composite(scrolledComposite, SWT.NONE);
		shellComposite.setLayout(new GridLayout(2, false));
		scrolledComposite.setContent(shellComposite);
		scrolledComposite.addListener(SWT.Resize, new Listener() {
			public void handleEvent(Event event) {
				scrolledComposite.setMinSize(shellComposite.computeSize(SWT.DEFAULT, SWT.DEFAULT));
			}
		});

		Font fontBold = new Font(shell.getDisplay(), new FontData("Mono", 13, SWT.NORMAL));
		Font font = new Font(shell.getDisplay(), new FontData("Mono", 11, SWT.NORMAL));
		shell.setFont(font);

		grpStart = new Group(shellComposite, SWT.NONE);
		grpStart.setText("Input Text");
		grpStart.setFont(font);
		grpStart.setLayout(new GridLayout(2, false));
		GridData gd_grpStart = new GridData(SWT.FILL, SWT.FILL, false, false, 1, 1);
		gd_grpStart.heightHint = 213;
		gd_grpStart.widthHint = 374;
		grpStart.setLayoutData(gd_grpStart);

		text = new Text(grpStart, SWT.MULTI | SWT.BORDER | SWT.WRAP | SWT.V_SCROLL);
		text.setOrientation(SWT.LEFT_TO_RIGHT);
		GridData gd_text = new GridData(SWT.FILL, SWT.FILL, true, true, 8, 2);
		gd_text.widthHint = 509;
		gd_text.heightHint = 95;
		text.setLayoutData(gd_text);
		text.setFont(font);

		btnSubmit = new Button(grpStart, SWT.NONE);
		btnSubmit.setToolTipText("Schedule");
		btnSubmit.setText("Submit");// 229e");
		btnSubmit.setFont(font);

		btnClear = new Button(grpStart, SWT.NONE);
		btnClear.setToolTipText("Schedule");
		btnClear.setText("Clear");// 229e");
		btnClear.setFont(font);

		lbl1 = new Label(grpStart, SWT.NONE);
		lbl1.setText("Number of words are : ");
		lbl1.setFont(fontBold);

		txt1 = new Text(grpStart, SWT.BORDER | SWT.READ_ONLY);
		txt1.setTextLimit(30);
		txt1.setFont(fontBold);

		btnSubmit.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseDown(MouseEvent e) {
				System.out.println("CppBuzz Debug: Submit button clicked");
				String input = text.getText();
				if (input.length() == 0)
					txt1.setText(Integer.toString(0));
				else {
					int countWords = 0;
					String[] words = input.split("\\s+");
					countWords = words.length;
					txt1.setText(Integer.toString(countWords));
				}
			}
		});

		btnClear.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseDown(MouseEvent e) {
				System.out.println("CppBuzz Debug: Clear button clicked");
				text.setText("");
				txt1.setText("");
			}
		});

		shell.open();
		while (!shell.isDisposed()) {
			if (!display.readAndDispatch()) {
				display.sleep();
			}
		}
		display.dispose();
	}
	}

public Main() {
	new WordCounter();
}

public static void main(String[] args) {
	new Main();
}

}

Count words in any text

swt java project to cound words in text