HomeDigital EditionSys-Con RadioSearch Java Cd
Advanced Java AWT Book Reviews/Excerpts Client Server Corba Editorials Embedded Java Enterprise Java IDE's Industry Watch Integration Interviews Java Applet Java & Databases Java & Web Services Java Fundamentals Java Native Interface Java Servlets Java Beans J2ME Libraries .NET Object Orientation Observations/IMHO Product Reviews Scalability & Performance Security Server Side Source Code Straight Talking Swing Threads Using Java with others Wireless XML
 

"Macro Component Use"
Vol. 1, Issue 3, P.52

	

Listing 1: Labeled choice method

import java.awt.*;
public class
labelledChoice extends Panel
{
    Label label;
    Choice choice;
    public
    labelledChoice(String l)
    {
	label = new Label(l);
	choice = new Choice();
	add(label);
	add(choice);
    }
    public Choice
    getChoice()
    {
	return choice;
    }
}

Listing 2: IPList method

import java.awt.*;
class IPList extends Panel
{
  List        addressList;
  TextField   ipTextField;
  Button      addButton;
  Button      removeButton;
  Label       listLabel;

  public
  IPList(String label)
  {
    setLayout(new GridBagLayout());
    listLabel = new Label(label);
    constrain(  listLabel,
		0, 0, 1, 1,
		GridBagConstraints.NONE,
		GridBagConstraints.NORTHWEST,
		0.0, 0.0,
		1, 1, 1, 1);

    Panel ipPanel = new Panel();
    ipPanel.setLayout(new GridLayout(1,2));
    Label ipLabel = new Label("IP");
    ipPanel.add("West",ipLabel);
    ipTextField = new TextField(15);
    ipPanel.add("East",ipTextField);
    constrain(  ipPanel,
		0, 1, 1, 1,
		GridBagConstraints.NONE,
		GridBagConstraints.NORTHWEST,
		0.0, 0.0,
		1, 1, 1, 10);

    addButton = new Button("Add");
    constrain(  addButton,
		1, 1, 1, 1,
		GridBagConstraints.NONE,
		GridBagConstraints.NORTHWEST,
		0.0, 0.0,
		1, 1, 1, 5);

    addressList = new List();
    addressList.setMultipleSelections(true);
    constrain(  addressList,
		0, 2, 1, 5,
		GridBagConstraints.BOTH,
		GridBagConstraints.NORTHWEST,
		1.0, 1.0,
		1, 10, 10, 10);

    removeButton = new Button("Remove");
    constrain(  removeButton,
		1, 2, 1, 1,
		GridBagConstraints.NONE,
		GridBagConstraints.NORTHWEST,
		0.0, 0.0,
		1, 1, 1, 5);
  }
  private void 
  constrain(  Component component, 
	      int grid_x,      int grid_y, 
	      int grid_width,  int grid_height,
	      int fill, 
	      int anchor, 
	      double weight_x, double weight_y,
	      int top,         int left, 
	      int bottom,      int right)
  {
    GridBagConstraints cst = new GridBagConstraints();
    cst.gridx = grid_x; 
    cst.gridy = grid_y;
    cst.gridwidth = grid_width; 
    cst.gridheight = grid_height;
    cst.fill = fill; 
    cst.anchor = anchor;
    cst.weightx = weight_x; 
    cst.weighty = weight_y;
    cst.insets = new Insets(top, left, bottom, right);
    ((GridBagLayout)getLayout()).setConstraints(component, cst);
    add(component);
  }

  public List
  getList()
  {
    return addressList;
  }
  private void
  doRemoveIP()
  {
    int sel[] = addressList.getSelectedIndexes();
    for (int i = 0; i < sel.length; i++)
    {
      addressList.delItem(sel[i]);
    }
    ipTextField.setText("");
  }
  private void
  doAddIP()
  {
    String ip = ipTextField.getText();
    ipTextField.setText("");
    addressList.addItem(ip);
  }
  private void
  doIPShow()
  {
    String ip = addressList.getSelectedItem();
    ipTextField.setText(ip);
  }
  private void
  handleActionEvents(Event event)
  {
    if (event.target == addButton)
    {
      doAddIP();
    }
    else if (event.target == removeButton)
    {
      doRemoveIP();
    }
  }
  private boolean
  handleKeyEvents(Event event)
  {
    if (event.target == ipTextField)
    {
      switch (event.key)
      {
	case Event.LEFT:
	case Event.RIGHT:
	case '\b':
	case '.':
	case '0': case '1': case '2':
	case '3': case '4': case '5':
	case '6': case '7': case '8':
	case '9':
	  break;
	default:
	  return true;
      }
    }
    return false;
  }
  public boolean
  handleEvent(Event event)
  {
    switch (event.id)
    {
      case Event.ACTION_EVENT:
	handleActionEvents(event);
	break;
      case Event.LIST_SELECT:
	doIPShow();
	break;
      case Event.KEY_PRESS:
	return handleKeyEvents(event);
    }
    return super.handleEvent(event);
  }
  public void
  paint(Graphics g)
  {
    Rectangle r = bounds();
    g.drawRect(0, 0, r.width-1, r.height-1);
  }
}

 

All Rights Reserved
Copyright ©  2004 SYS-CON Media, Inc.
  E-mail: [email protected]

Java and Java-based marks are trademarks or registered trademarks of Sun Microsystems, Inc. in the United States and other countries. SYS-CON Publications, Inc. is independent of Sun Microsystems, Inc.