Code Snippets ⇒ Word counter with BoxLayout
This Java code creates a GUI with a BoxLayout.
If you click on the button, the words in the text area are counted.
⇐ Go back
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
/**
* Word counter with BoxLayout.
* This class represents a frame.
*
* @version 1.0 07 May 2011
* @author Michel Neeser
*
*/
public class WordCounter extends JFrame implements ActionListener {
// Because we extend JFrame, we have access to all methods defined there.
// With that, we're able to define the behaviour of our frame.
// In addition, we implement the ActionListener interface.
// So we're able to use this class not just to represent our frame,
// but also to operate as a listener for the button on it.
private static final long serialVersionUID = 1L;
private JTextArea textArea;
private JButton button;
/**
* This is the no-arg constructor.
* It takes no arguments (surprise!).
* Calls initGUI().
*/
private WordCounter() {
// Build GUI in a seperate method.
// This is good practice.
initGUI();
}
/**
* Builds the GUI.
* Called by the constructor.
*/
private void initGUI() {
// Attention! Here begins the fun ...
// set frame size (setSize is inherited from JFrame)
setSize(500, 400);
// show frame in center screen (setLocationRelativeTo is inherited)
setLocationRelativeTo(null);
// set title of the frame (setTitle is inherited)
setTitle("This word counter rocks!");
// set layout, we're going to use a vertical box layout
setLayout(new BoxLayout(getContentPane(), BoxLayout.Y_AXIS));
// add label within a box
Box labelBox = Box.createHorizontalBox();
labelBox.add(Box.createRigidArea(new Dimension(0, 50)));
labelBox.add(new JLabel("Hi there! This is a word counter with box layout."));
add(labelBox); // add box to layout
// add text area within a box
Box areaBox = Box.createHorizontalBox();
areaBox.add(Box.createRigidArea(new Dimension(50, 0)));
textArea = new JTextArea();
areaBox.add(new JScrollPane(textArea));
areaBox.add(Box.createRigidArea(new Dimension(50, 0)));
add(areaBox); // add box to layout
// add button within a box
Box buttonBox = Box.createHorizontalBox();
buttonBox.add(Box.createRigidArea(new Dimension(0, 50)));
button = new JButton("Count words!");
buttonBox.add(button);
add(buttonBox); // add box to layout
// add action listener to the button
button.addActionListener(this);
}
/**
* Called when the user pushes on the button (inherited from ActionListener interface).
*/
public void actionPerformed(ActionEvent evt) {
String text = textArea.getText();
int wordNumber;
if (text.equals("")) {
wordNumber = 0;
}
else {
String[] countWords = textArea.getText().split(" "); // split words
wordNumber = countWords.length; // count words
}
JOptionPane.showMessageDialog(null, "Number of words: " + wordNumber, "Message", JOptionPane.INFORMATION_MESSAGE);
}
/**
* Instantiates a new instance of WordCounter.
* @param args the command line args -> not used in code
*/
public static void main(String[] args) {
// create instance of WordCounter and make it visible
WordCounter wc = new WordCounter();
wc.setVisible(true);
}
}

