Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Can't enter data into textfield

Status
Not open for further replies.

stevepuri

Programmer
Mar 30, 2001
24
0
0
US
/*
MyApp.java

Upon clicking on the
the menu item "myMenuItem"
from the menu "myMenu",
A dialog box will appear.
This dialog box contains
a textfield; but you cannot
enter data into it - Why?!

Thanks,
Steve.
*/

import java.awt.*;
import java.awt.event.*;

class MyApp extends Frame implements ActionListener
{
private MenuBar myMenuBar = new MenuBar();

private Menu myMenu = new Menu("myMenu");
private MenuItem myMenuItem = new MenuItem("myMenuItem");

MyApp()
{
super("MyApp");
setLocation(new Point(0, 0));
setSize(300, 300);
setResizable(false);

myMenuItem.addActionListener(this);
myMenu.add(myMenuItem);

myMenuBar.add(myMenu);
setMenuBar(myMenuBar);

this.addWindowListener(new windowAdapter());
}

public void actionPerformed(ActionEvent e)
{
if (e.getSource() == myMenuItem)
{
MyDialog myDialog = new MyDialog(this, 300, 100);
myDialog.setModal(true);
myDialog.show();
}
}

public static void main(String[] args)
{
MyApp myApp = new MyApp();
myApp.setVisible(true);
}
}

class MyDialog extends Dialog
{
private Label myLabel;
private TextField myTextField;

public MyDialog(Frame parent, int w, int h)
{
super(parent, "MyDialog", false);
setSize(new Dimension(w, h));
setResizable(false);

myLabel = new Label("myLabel", Label.CENTER);

myTextField = new TextField(15);
myTextField.setEditable(true);

Panel myPanel = new Panel();
myPanel.setLayout(new GridLayout(1, 1));
myPanel.add(myLabel);
myPanel.add(myTextField);

add(myPanel);
}
public boolean handleEvent(Event e)
{
if (e.id == Event.WINDOW_DESTROY)
{
dispose();
}

return true;
}
}

class windowAdapter extends WindowAdapter
{
public void windowClosing(WindowEvent e)
{
e.getWindow().dispose();
System.exit(0);
}
}
 
Hi,

There seems to be some problems with the listener for textfield. I am also unsure of what the error is. But I changed the textfield to JTextField and it works.

I suggest that if you are using jdk1.2 and above, it might be a gd idea to use swing instead of awt because when I was compiling your program, I received some warning thats say you are overwriting some deprecated method. I didn't manage to figure out which method that deprecated.

Although the program will still run, but I guess it might be due to using deprecated method resulting as to why you cannot enter text into the textfield.

Good luck,
Leon If you need additional help, you can email to me at zaoliang@hotmail.com I don't guaranty that I will be able to solve your problems but I will try my best :)
 
Leon, is correct. Never mix AWT components with Swing Components. You will have problems with layout managers, painting of the components and event handling.

Brian
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top