help newbie : frame resets

M

Madhur Ahuja

Hello

In following code attatched, whenever I press send button, the whole of
my frame resets. The JTextField are set to zero columns. The server field
can
be set to *localhost*.

Please Help, why this resetting happens.

import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
import java.net.*;
import java.io.*;

class mainapp implements ActionListener,KeyListener
{

JPanel mainpanel;

JTextField to,server,from;

JTextArea body,result;
JFrame mainframe;
GridBagLayout gbc;
GridBagConstraints constraints;
JButton send;
JLabel lfrom,lto,lserver,lbody,lresult,iphost;

public mainapp()
{

gbc=new GridBagLayout();
mainpanel=new JPanel(gbc);
constraints = new GridBagConstraints();

mainframe =new JFrame("Mail send");
mainpanel.addKeyListener(this);
mainframe.addKeyListener(this);
addwidgets();

mainframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// mainframe.setSize(new Dimension(120, 40));
mainframe.getContentPane().add(mainpanel);
mainpanel.setSize(400,400);

mainframe.pack();
mainframe.validate();
mainframe.setVisible(true);
}


void addwidgets()
{
from=new JTextField(10);
from.setColumns(10);
body=new JTextArea(10,30);
to=new JTextField(10);
to.setColumns(10);
server=new JTextField(10);
server.setColumns(10);
send=new JButton("Send");
result=new JTextArea(4,30);

lfrom=new JLabel("From:");
lbody=new JLabel("Body:");
lto=new JLabel("To:");
lserver=new JLabel("Server:");
lresult=new JLabel("Result:");
iphost=new JLabel("ffffffffff");

from.addActionListener(this);
to.addActionListener(this);
server.addActionListener(this);
send.addActionListener(this);


buildConstraints(constraints,0,0,1,1,80,10);
gbc.setConstraints(lfrom, constraints);
mainpanel.add(lfrom);

buildConstraints(constraints,1,0,1,1,20,10);
gbc.setConstraints(from, constraints);
mainpanel.add(from);

buildConstraints(constraints,0,3,1,1,0,10);
gbc.setConstraints(lbody, constraints);
mainpanel.add(lbody);


buildConstraints(constraints,1,3,1,1,0,10);
gbc.setConstraints(body, constraints);
mainpanel.add(body);


buildConstraints(constraints,0,1,1,1,0,10);
gbc.setConstraints(lto, constraints);
mainpanel.add(lto);


buildConstraints(constraints,1,1,1,1,0,10);
gbc.setConstraints(to, constraints);
mainpanel.add(to);


buildConstraints(constraints,0,2,1,1,0,10);
gbc.setConstraints(lserver, constraints);
mainpanel.add(lserver);


buildConstraints(constraints,1,2,1,1,0,10);
gbc.setConstraints(server, constraints);
mainpanel.add(server);

buildConstraints(constraints,2,2,1,1,0,10);
gbc.setConstraints(iphost, constraints);
mainpanel.add(iphost);


buildConstraints(constraints,1,4,1,1,0,0);
gbc.setConstraints(send, constraints);
mainpanel.add(send);


buildConstraints(constraints,0,5,1,1,0,10);
gbc.setConstraints(lresult, constraints);
mainpanel.add(lresult);

buildConstraints(constraints,1,5,1,1,0,10);
gbc.setConstraints(result, constraints);
mainpanel.add(result);

}

public void keyTyped(KeyEvent e)
{
System.out.println("dfd");
if(from.getText()=="" || to.getText()==""||server.getText()=="")
{
send.setEnabled(false);
System.out.println("dfd");
}
else
send.setEnabled(false);
}

public void keyPressed(KeyEvent e)
{
System.out.println("dfd");
}

public void keyReleased(KeyEvent e)
{
}

public static void main(String args[])
{
new mainapp();

}


public void actionPerformed(ActionEvent e)
{
if(e.getSource().getClass()==JTextField.class)
{
System.out.println("dfd");
JTextField obj=(JTextField)e.getSource();
if(obj.getText()=="")
{
send.setEnabled(false);
}
else
send.setEnabled(true);
}



if(e.getSource()==send)
{

new mailit();

}


}

void buildConstraints(GridBagConstraints gbc, int gx, int gy,
int gw, int gh, int wx, int wy) {
gbc.gridx = gx;
gbc.gridy = gy;
gbc.gridwidth = gw;
gbc.gridheight = gh;
gbc.weightx = wx;
gbc.weighty = wy;
}



class mailit extends Thread
{
Socket smtpsocket;
String host;
InetAddress add;
PrintWriter pw;
BufferedReader br;
public void run()
{

try
{
smtpsocket=new Socket();
System.out.println(add);
smtpsocket.connect(new InetSocketAddress(add,25));
br=new BufferedReader(new InputStreamReader(smtpsocket.getInputStream()));
pw=new PrintWriter(new OutputStreamWriter(smtpsocket.getOutputStream()));

pw.println("Helo mad");
pw.println("mail from :"+from.getText());
pw.println("rcpt to:"+to.getText());
pw.println("data");
pw.flush();
pw.println(body.getText());
pw.println(".");
pw.flush();

while(true)
{
String line=br.readLine();
if(line==null) break;
result.append(line+"\n");

System.out.println(line);

}

smtpsocket.close();

}

catch(Exception IOException)
{
IOException.printStackTrace();
JOptionPane.showMessageDialog(null,"Error during
connection","Mail",JOptionPane.ERROR_MESSAGE);
}



}

mailit()
{
try
{
add=InetAddress.getByName(server.getText());
iphost.setText(add.getHostAddress());
System.out.println(add.getHostAddress()) ;
this.start();
}
catch(Exception UnknownHostException)
{
JOptionPane.showMessageDialog(null,"Cannot resolve
hostname","Mail",JOptionPane.ERROR_MESSAGE);
}

}

}


}


--
Winners dont do different things, they do things differently.

Madhur Ahuja
India

Homepage : http://madhur.netfirms.com
Email : madhur<underscore>ahuja<at>yahoo<dot>com
 
Z

zoopy

Hello

In following code attatched, whenever I press send button, the whole of
my frame resets. The JTextField are set to zero columns. The server field
can
be set to *localhost*.

Please Help, why this resetting happens.
If you enlarge the frame beyond certain size it doesn't happen anymore. It's likely caused by the
improper values for GridBagConstraints' weightx/weighty. GridBagLayout is quite complex and I've
always interpreted GBC weightx/y as values relative weights: a component with weightx==1 takes 1
amount of the width at hand (whatever that may be), weightx==2 takes 2 amounts, a component with
weightx==0 doesn't contribute for calculating the width of components with weightx>0, however it
does take the space it needs. [If I'm wrong, please correct me].

I've changed the weightx/y in the code below (and some other things, such as justifying some fields
horizontally).

Some other remarks:
- It's not necessary to do validate() right after pack()
- Use GridBagConstraints' fill field for justifying component horizontally/vertically/both
- GridBagLayout largely determines the size of a component (relative to others), therefore setting
number of columns and rows of a JTextField/JTextArea isn't always honoured.
- Don't use == (or !=) for comparing String *values*: use someString.equals(anotherString) instead;
to test if a string is empty you can use someString.length() == 0

-------------
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
import java.net.*;
import java.io.*;

public class mainapp implements ActionListener, KeyListener
{

JPanel mainpanel;

JTextField to, server, from;

JTextArea body, result;
JFrame mainframe;
GridBagLayout gbc;
GridBagConstraints constraints;
JButton send;
JLabel lfrom, lto, lserver, lbody, lresult, iphost;

public mainapp()
{

gbc = new GridBagLayout();
mainpanel = new JPanel(gbc);
constraints = new GridBagConstraints();

mainframe = new JFrame("Mail send");
mainpanel.addKeyListener(this);
mainframe.addKeyListener(this);
addwidgets();

mainframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// mainframe.setSize(new Dimension(120, 40));
mainframe.getContentPane().add(mainpanel);
mainpanel.setPreferredSize(new Dimension(400, 400));

mainframe.pack();
//mainframe.validate();
mainframe.setVisible(true);
}

void addwidgets()
{
from = new JTextField(10);
from.setColumns(10);
body = new JTextArea(10, 30);
//body = new JTextArea();
to = new JTextField(10);
to.setColumns(10);
server = new JTextField(10);
server.setColumns(10);
send = new JButton("Send");
result = new JTextArea(4, 30);
//result = new JTextArea();

lfrom = new JLabel("From:");
lbody = new JLabel("Body:");
lto = new JLabel("To:");
lserver = new JLabel("Server:");
lresult = new JLabel("Result:");
iphost = new JLabel("ffffffffff");

from.addActionListener(this);
to.addActionListener(this);
server.addActionListener(this);
send.addActionListener(this);


buildConstraints(constraints, 0, 0, 1, 1, 0, 0);
gbc.setConstraints(lfrom, constraints);
mainpanel.add(lfrom);

buildConstraints(constraints, 1, 0, 1, 1, 1, 0);
constraints.fill = GridBagConstraints.HORIZONTAL;
gbc.setConstraints(from, constraints);
mainpanel.add(from);

buildConstraints(constraints, 0, 3, 1, 1, 0, 0);
gbc.setConstraints(lbody, constraints);
mainpanel.add(lbody);

buildConstraints(constraints, 1, 3, 1, 1, 1, 2);
constraints.fill = GridBagConstraints.BOTH;
gbc.setConstraints(body, constraints);
mainpanel.add(body);

buildConstraints(constraints, 0, 1, 1, 1, 0, 0);
gbc.setConstraints(lto, constraints);
mainpanel.add(lto);

buildConstraints(constraints, 1, 1, 1, 1, 1, 0);
constraints.fill = GridBagConstraints.HORIZONTAL;
gbc.setConstraints(to, constraints);
mainpanel.add(to);

buildConstraints(constraints, 0, 2, 1, 1, 0, 0);
gbc.setConstraints(lserver, constraints);
mainpanel.add(lserver);

buildConstraints(constraints, 1, 2, 1, 1, 1, 0);
constraints.fill = GridBagConstraints.HORIZONTAL;
gbc.setConstraints(server, constraints);
mainpanel.add(server);

buildConstraints(constraints, 2, 2, 1, 1, 0, 0);
gbc.setConstraints(iphost, constraints);
mainpanel.add(iphost);

buildConstraints(constraints, 1, 4, 1, 1, 0, 0);
constraints.fill = GridBagConstraints.NONE;
gbc.setConstraints(send, constraints);
mainpanel.add(send);

buildConstraints(constraints, 0, 5, 1, 1, 0, 0);
gbc.setConstraints(lresult, constraints);
mainpanel.add(lresult);

buildConstraints(constraints, 1, 5, 1, 1, 1, 1);
constraints.fill = GridBagConstraints.BOTH;
gbc.setConstraints(result, constraints);
mainpanel.add(result);

}

public void keyTyped(KeyEvent e)
{
System.out.println("dfd");
if ( from.getText().length() == 0
|| to.getText().length() == 0
|| server.getText().length() == 0)
{
send.setEnabled(false);
System.out.println("dfd");
}
else
send.setEnabled(true);
}

public void keyPressed(KeyEvent e)
{
System.out.println("dfd");
}

public void keyReleased(KeyEvent e)
{
}

public static void main(String args[])
{
new mainapp();

}

public void actionPerformed(ActionEvent e)
{
if (e.getSource().getClass() == JTextField.class)
{
System.out.println("dfd");
JTextField obj = (JTextField) e.getSource();
if (obj.getText().length()==0)
{
send.setEnabled(false);
}
else
send.setEnabled(true);
}

if (e.getSource() == send)
{

new mailit();

}

}

void buildConstraints(
GridBagConstraints gbc,
int gx,
int gy,
int gw,
int gh,
double wx,
double wy)
{
gbc.gridx = gx;
gbc.gridy = gy;
gbc.gridwidth = gw;
gbc.gridheight = gh;
gbc.weightx = wx;
gbc.weighty = wy;
}

class mailit extends Thread
{
Socket smtpsocket;
String host;
InetAddress add;
PrintWriter pw;
BufferedReader br;
public void run()
{

try
{
smtpsocket = new Socket();
System.out.println(add);
smtpsocket.connect(new InetSocketAddress(add, 25));
br =
new BufferedReader(
new InputStreamReader(smtpsocket.getInputStream()));
pw =
new PrintWriter(new OutputStreamWriter(smtpsocket.getOutputStream()));

pw.println("Helo mad");
pw.println("mail from :" + from.getText());
pw.println("rcpt to:" + to.getText());
pw.println("data");
pw.flush();
pw.println(body.getText());
pw.println(".");
pw.flush();

while (true)
{
String line = br.readLine();
if (line == null)
break;
result.append(line + "\n");

System.out.println(line);

}

smtpsocket.close();

}

catch (Exception IOException)
{
IOException.printStackTrace();
JOptionPane.showMessageDialog(
null,
"Error during connection",
"Mail",
JOptionPane.ERROR_MESSAGE);
}

}

mailit()
{
try
{
add = InetAddress.getByName(server.getText());
iphost.setText(add.getHostAddress());
System.out.println(add.getHostAddress());
this.start();
}
catch (Exception UnknownHostException)
{
JOptionPane.showMessageDialog(
null,
"Cannot resolve hostname",
"Mail",
JOptionPane.ERROR_MESSAGE);
}

}

}

}
 
M

Madhur Ahuja

zoopy said:
If you enlarge the frame beyond certain size it doesn't happen
anymore. It's likely caused by the improper values for
GridBagConstraints' weightx/weighty. GridBagLayout is quite complex
and I've always interpreted GBC weightx/y as values relative weights:
a component with weightx==1 takes 1 amount of the width at hand
(whatever that may be), weightx==2 takes 2 amounts, a component with
weightx==0 doesn't contribute for calculating the width of components
with weightx>0, however it does take the space it needs. [If I'm
wrong, please correct me].

I've changed the weightx/y in the code below (and some other things,
such as justifying some fields horizontally).

Some other remarks:
- It's not necessary to do validate() right after pack()
- Use GridBagConstraints' fill field for justifying component
horizontally/vertically/both
- GridBagLayout largely determines the size of a component (relative
to others), therefore setting number of columns and rows of a
JTextField/JTextArea isn't always honoured. - Don't use == (or !=)
for comparing String *values*: use someString.equals(anotherString)
instead; to test if a string is empty you can use someString.length()
== 0

Thank you


--
Winners dont do different things, they do things differently.

Madhur Ahuja
India

Homepage : http://madhur.netfirms.com
Email : madhur<underscore>ahuja<at>yahoo<dot>com
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top