i am new to swing

hello friends,

i am new to swings and was pastly working with AWT now that i want to convert my AWT desktop application to swings i have simply placed a keyword "j" before all the components but it is not working. please have a look at the code and help me out thanks,

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

public class tmpswing extends JFrame implements ActionListener

{

JButton blogin;

tmpswing()

{setTitle("in constructor");

setLayout(null);

setLocation(50,50);

setVisible(true);

setSize(500,500);

JLabel lid=new JLabel("User Id",0);

lid.setLocation(150,200);

lid.setSize(50,20);

lid.setVisible(true);

add(lid);

JLabel lpassword=new JLabel("Password",0);

lpassword.setLocation(150,240);

lpassword.setSize(70,20);

lpassword.setVisible(true);

add(lpassword);

JTextField tfuid=new JTextField();

tfuid.setLocation(220,200);

tfuid.setSize(130,20);

tfuid.setVisible(true);

add(tfuid);

tfuid.setText("");

JTextField tfpassword=new JTextField();

tfpassword.setLocation(220,240);

tfpassword.setSize(130,20);

tfpassword.setVisible(true);

add(tfpassword);

tfpassword.setText("");

blogin=new JButton("Log In");

blogin.setLocation(220,300);

blogin.setSize(50,20);

blogin.setVisible(true);

add(blogin);

blogin.addActionListener(this);

setTitle("constructor ends");

}

public void actionPerformed(ActionEvent ae)

{

try{

if(ae.getSource()==blogin)

{

setTitle("login button pressed");

}

}

catch(Exception e)

{}

}

public static void main(String args[])

{

tmpswing j=new tmpswing();

}

}

[1865 byte] By [suhail_anwar_khana] at [2007-10-3 8:35:16]
# 1
i have simply placed a keyword "j" before all the componentsLOLbut it is not workingMm. Define "not working."
itchyscratchya at 2007-7-15 3:42:50 > top of Java-index,Desktop,Core GUI APIs...
# 2
Putting a J before every component will not convert it to Swing. Take a look at each AWT componet and decide what swing componet it will be. Also use code tags.
zadoka at 2007-7-15 3:42:50 > top of Java-index,Desktop,Core GUI APIs...
# 3
sooosilly of me,thank u guys but can u plz tel me how can i add a JTextField or some thing like that to my form
suhail_anwar_khana at 2007-7-15 3:42:50 > top of Java-index,Desktop,Core GUI APIs...
# 4
JTextField text = new JTextField();getContentPane().add(text);
zadoka at 2007-7-15 3:42:50 > top of Java-index,Desktop,Core GUI APIs...
# 5

not working

i wanna make the above program work so i have changed the coding as

JTextField tfuid=new JTextField();

tfuid.setLocation(220,200);

tfuid.setSize(130,20);

tfuid.setVisible(true);

add(tfuid);

tfuid.setText("");

getContentPane().add(tfuid);

but i dont c the textfield visible

help plz

suhail_anwar_khana at 2007-7-15 3:42:50 > top of Java-index,Desktop,Core GUI APIs...
# 6

Use the code formatting tags when posting code to the code is readable.

With Swing you don't play with the size and location. You should be using Layout Managers and let them postion the components. You let each component determine its ideal size or you use the setPreferredSize() method to override the default.

A simple example would be:

JTextField textField = new JTextField(10);

getContentPane().add( textField );

pack();

setVisible( true );

Read the [url http://java.sun.com/docs/books/tutorial/uiswing/TOC.html]Swing tutorial[/url] for more information on using Swing:

camickra at 2007-7-15 3:42:50 > top of Java-index,Desktop,Core GUI APIs...