Utility Classes
In
this chapter, we will write two utility classes, which will be used
to build the user interface.
Files needed for this chapter are:
- ButtonPanel.java
- SwingComp.java
The SwingComp Class
This class is a factory class that can be used to create standard Swing
components.
Listing
15.1 SwingComp.java
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
//Factory class used to create swing components
public class SwingComp{
//private constructor
private SwingComp(){
System.out.println("Cannot create object");
}
//This method is used to construct a
//new JTextField with the specified
//number of columns.
public static JTextField createTextField(Container con,int col){
JTextField tf=new JTextField(col);
//add the text field to the specified container.
con.add(tf);
return tf;
}
//This method is used to construct a new
//JTextField with the specified
//number of columns. The method adds the textfield to the
//specified container and adds the specified listener
//to the text field.
public static JTextField createTextField
(Container con,ActionListener listener,int col){
JTextField tf=new JTextField(col);
tf.addActionListener(listener);
con.add(tf);
return tf;
}
//This method is used to create a button
//with the specified text. The method adds
//the button to the specified container and adds the
//given listener to the button.
public static JButton createButton(Container con,
ActionListener al,String text){
JButton btn=new JButton(text);
btn.addActionListener(al);
con.add(btn);
return btn;
}
//This method is used to create a label
//with the specified text. The method adds
//the label to the specified container.
public static JLabel createLabel(Container con,String text){
JLabel lbl=new JLabel(text);
lbl.setForeground(Color.black);
con.add(lbl);
return lbl;
}
//This method is used to create a label
//with the specified text and alignment.
//The method adds the label to the specified container.
public static JLabel createLabel(Container con,
String text,int align){
JLabel lbl=new JLabel(text,align);
lbl.setForeground(Color.black);
con.add(lbl);
return lbl;
}
//This method is used to create a menu
//item with the specified text.
//The method adds the menu item to the specified menu
//and adds the given listener to the menu item.
public static JMenuItem createMenuItems(JMenu mnu,
ActionListener listener,String text){
JMenuItem mi = new JMenuItem(text);
mi.addActionListener(listener);
mnu.add(mi);
return mi;
}
//This method is used to create a
//menu with the given text. The method adds the
//menu to the specified menu bar.
public static JMenu createMenu(JMenuBar mBar,
String text){
JMenu mnu = new JMenu(text);
mBar.add(mnu);
return mnu;
}
//This method is used to construct a panel.
//The method adds the panel object to the given container
//using the specified constraints object.
public static JPanel createPanel(Container cp,
String placement){
JPanel panel =new JPanel();
cp.add(panel,placement);
return panel;
}
//This method is used to construct a panel
//with the specified layout manager.
//The method adds the panel object to the given container
//using the specified constraints object.
public static JPanel createPanel(Container cp,
String placement,LayoutManager manager){
JPanel panel =new JPanel(manager);
cp.add(panel,placement);
return panel;
}
}
|