Link to home
Start Free TrialLog in
Avatar of jiayan
jiayan

asked on

More on the TextField Question

Here is more detail on my textfield question.

The program is given below:

In it the method Constraint(), just provide a position on
the panel.

What happends is the textfiled will show on the panel,
and when I click on it, it does recognize as an event.
event.targer = textfield1; but I can not enter any thing
in the textfield.  I wonder what happens.

P.S. The points on the other one will also be given
if you can help me.  Thank you.:
import java.io.*;
import java.awt.*;
import java.util.*;

public class EditLogin extends Frame {
      String temp,temp1,temp2,temp3,temp4;
      TextField textfield1;
      
      Panel panel1;
      GridBagLayout gridbag = new GridBagLayout();

      public EditLogin(String title)
      {
        super(title);

        panel1 = new Panel();
        panel1.setLayout(gridbag);

        textfield1 = new TextField(10);

        constrain(panel1,textfield1,0,5,1,1);

        this.setLayout(gridbag);
        constrain(this,panel1,0,0,1,1,GridBagConstraints.VERTICAL,
              GridBagConstraints.NORTHWEST,0.0,1.0,10,10,5,5);


      }

      public boolean handleEvent(Event event)
      {
      
            if (event.target == textfield1)
            { textarea.setText("Entering Text\n");
              temp1 = textfield1.getText();
              textarea.appendText(astring+"\n");
            }

            return true;
      }  
 

      public void constrain(Container container, 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 c = new GridBagConstraints();
        c.gridx = grid_x; c.gridy = grid_y;
        c.gridwidth = grid_width; c.gridheight = grid_height;
        c.fill = fill; c.anchor = anchor;
        c.weightx = weight_x; c.weighty = weight_y;
        if (top+bottom+left+right > 0)
            c.insets = new Insets(top, left, bottom, right);
       
        ((GridBagLayout)container.getLayout()).setConstraints(component, c);
        container.add(component);
    }
   
    public void constrain(Container container, Component component,
                  int grid_x, int grid_y, int grid_width, int grid_height) {
        constrain(container, component, grid_x, grid_y,
              grid_width, grid_height, GridBagConstraints.NONE,
              GridBagConstraints.CENTER, 0.0, 0.0, 0, 0, 0, 0);
    }
   
    public void constrain(Container container, Component component,
                  int grid_x, int grid_y, int grid_width, int grid_height,
                  int top, int left, int bottom, int right) {
        constrain(container, component, grid_x, grid_y,
              grid_width, grid_height, GridBagConstraints.NONE,
              GridBagConstraints.CENTER,
              0.0, 0.0, top, left, bottom, right);
    }

      public static void main(String[] args) {
            Frame editlog = new EditLogin("Edit Login");
            editlog.pack();
            editlog.show();
      }
}

Avatar of cyberwolf
cyberwolf

Perhaps you need to request focus for the textfield?
Avatar of jiayan

ASKER

solved already by using action() instead of eventhandler()
ASKER CERTIFIED SOLUTION
Avatar of Arkadiy
Arkadiy

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial