This week's book giveaway is in the OCPJP forum.
We're giving away four copies of OCA/OCP Java SE 7 Programmer I & II Study Guide and have Kathy Sierra & Bert Bates on-line!
See this thread for details.
The moose likes Swing / AWT / SWT and the fly likes problem with JDialog (urgent) please help Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "problem with JDialog (urgent) please help" Watch "problem with JDialog (urgent) please help" New topic
Author

problem with JDialog (urgent) please help

kishore kallam
Greenhorn

Joined: Apr 03, 2002
Posts: 5
hi
i have some combo boxes and textfields.
in textfield focus lost i am validating the value if invalid i am showing jDailog to show the message.
i am in textfield and enterdrd invalid data and with mouse clicked on the combo.then it is showing the jDailog but on click of o.k on the dailog box.the combo is getting expanded.
but i have to set the focus back to textfield.i am doing textfield.requestFocus() ,but its not working
please help.
[ April 03, 2002: Message edited by: kishore kallam ]
Rajendar Goud
Ranch Hand

Joined: Mar 06, 2002
Posts: 220
Hi kishore,
iam a little bit confused with ur explanation,
can u post the code here,so that we can take a look at it and,probably will be able to understand it more clearly.
Raj
kishore kallam
Greenhorn

Joined: Apr 03, 2002
Posts: 5
thanks for your reply
the actual problem is.
there is one text field and combo.
i am entering some value into textfield and with mouse i clicked on combo.
so first focus lost of textfield will be fired and then mouse click on combo.right.
then on focus lost i am showing error Dialog(it has only o.k button).
what is happening it shows error meassage and on click of o.k it is going to combo even i say textfield.requestfocus().
code:
if (!(val.isAlphaNumeric(txtfState.getText().trim()))) {
errorDialog.errorDialog(15019, 'E');
errorDialog.setLocationRelativeTo(this);
errorDialog.show();
txtfState.requestFocus(); // setting back the focus
validateFlag = false;
}
but it is going to combo
please help
Rajendar Goud
Ranch Hand

Joined: Mar 06, 2002
Posts: 220
Hi kishore,
i tried as per ur explanation and it worked fine for me..
i hope this is what u need.
do check out this code.
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class focusevent extends JFrame
{
JTextField tf;
JComboBox cm;
JDialog dialog;
JButton ok;
Container cont;
focusevent()
{
cont = getContentPane();
tf = new JTextField(10);
cm = new JComboBox();
dialog = new JDialog();
dialog.setSize(150,100);
ok = new JButton("ok");
dialog.getContentPane().add(ok);
cm.addItem("1");
cm.addItem("2");
cont.add(tf);
cont.add(cm);
cont.setLayout(new FlowLayout());
tf.addFocusListener(new focuslistener());
ok.addActionListener(new actionListener());
}
class focuslistener extends FocusAdapter
{
public void focusLost(FocusEvent fe)
{
dialog.setVisible(true);
}
}

class actionListener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
if(e.getSource().equals(ok))
{
dialog.dispose();
tf.requestFocus();
}
}
}
public static void main(String a[])
{
focusevent fc = new focusevent();
fc.setSize(300,250);
fc.setVisible(true);

}
}

cheers,
Raj
Paul Stevens
Ranch Hand

Joined: May 17, 2001
Posts: 2823
If you are using a JTextField, why not use a PlainDocument to do your validation?
kishore kallam
Greenhorn

Joined: Apr 03, 2002
Posts: 5
hi rajendar
in your example you can observe that even dailog box is there ytou can still cange the combo box values.
my requirement is we should not select any of the other components when on click of o.k we are setting focus back to textfield.
this problem exists in you code also.
can you see this and help me please.
kishore kallam
Greenhorn

Joined: Apr 03, 2002
Posts: 5
Originally posted by Paul Stevens:
If you are using a JTextField, why not use a PlainDocument to do your validation?


thanks paul but its requirement that we need to do on focus lost.
can you help on this
Rajendar Goud
Ranch Hand

Joined: Mar 06, 2002
Posts: 220
hi kishore,
if u want that ,u dont want focus on other components when u got that 'Dialog' then u need to make that dialog a Modal one.
so modify the line
dialog = new JDialog();
to
dialog = new JDialog(this,true);
this will sort the error .
Raj
kishore kallam
Greenhorn

Joined: Apr 03, 2002
Posts: 5
hi
this works fine.but still problem exists.
when you enterd something invalid in textfield and with mouse you click on combo.then what happens first error dialog will come and on click of o.k on that it expands combo because you clicked on combo.
i have to restrict that
is there any way?
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: problem with JDialog (urgent) please help