Results 1 to 6 of 6
Thread: a little problem
- 02-05-2012, 03:00 AM #1
Member
- Join Date
- Oct 2011
- Posts
- 16
- Rep Power
- 0
a little problem
ei guys i got a little problem about the textfield?? i dont know if that need a loop ...
i want that if the user not fill all the blanks he cannot pass ...
heres my code ..
Java Code:import javax.swing.*; import java.awt.*; import java.awt.event.*; public class Information extends JFrame implements ActionListener{ JLabel lbl = new JLabel("Employee No.:"); JTextField txt = new JTextField(); JLabel lbl2 = new JLabel("Last Name : "); JTextField txt2 = new JTextField(); JLabel lbl3 = new JLabel("First Name : "); JTextField txt3 = new JTextField(); JLabel lbl4 = new JLabel("Middle Name : "); JTextField txt4 = new JTextField(); JLabel lbl5 = new JLabel("Department : "); JTextField txt5 = new JTextField(); JLabel lbl6 = new JLabel("Position : "); JTextField txt6 = new JTextField(); JButton btn = new JButton("SAVE"); JButton btn2 = new JButton("CLOSE"); public Information(){ super("Enployee's Information"); Container c = getContentPane(); c.setLayout(null); c.setBackground(Color.BLACK); //Posistions lbl.setBounds(10,100,100,20); txt.setBounds(100,100,200,20); lbl2.setBounds(10,140,100,20); txt2.setBounds(100,140,200,20); lbl3.setBounds(10,180,100,20); txt3.setBounds(100,180,200,20); lbl4.setBounds(10,220,100,20); txt4.setBounds(100,220,200,20); lbl5.setBounds(10,260,100,20); txt5.setBounds(100,260,200,20); lbl6.setBounds(10,300,100,20); txt6.setBounds(100,300,200,20); //buttons btn.setBounds(100,350,80,30); btn2.setBounds(200,350,80,30); btn.addActionListener(this); btn2.addActionListener(this); //label colors lbl.setForeground(Color.WHITE); lbl2.setForeground(Color.WHITE); lbl3.setForeground(Color.WHITE); lbl4.setForeground(Color.WHITE); lbl5.setForeground(Color.WHITE); lbl6.setForeground(Color.WHITE); //layout c.add(lbl); c.add(txt); c.add(lbl2); c.add(txt2); c.add(lbl3); c.add(txt3); c.add(lbl4); c.add(txt4); c.add(lbl5); c.add(txt5); c.add(lbl6); c.add(txt6); c.add(btn); c.add(btn2); setSize(350,450); setResizable(false); setDefaultCloseOperation(EXIT_ON_CLOSE); show(); } public void actionPerformed(ActionEvent e){ int num = 0; String emp = " "; String Fname = " "; String Lname = " "; String Mname = " "; String Dep = " "; String Pos = " "; emp = txt.getText(); Fname = txt2.getText(); Lname = txt3.getText(); Mname = txt4.getText(); Dep = txt5.getText(); Pos = txt6.getText(); if(e.getSource() == btn){ if(emp.equals(" ") && Fname.equals(" ")&& Lname.equals(" ") && Mname.equals(" ") && Dep.equals(" ")&& Pos.equals(" ") ) { JOptionPane.showMessageDialog(null,"Sorry, fill all the blank"); } else { JOptionPane.showMessageDialog(null,"Succes"); } } public static void main(String [] args){ Information information = new Information(); information.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } }
- 02-05-2012, 03:38 AM #2
Member
- Join Date
- Feb 2012
- Posts
- 31
- Rep Power
- 0
Re: a little problem
When you run this program, do you get the pop-up when you miss a field?
- 02-05-2012, 03:50 AM #3
Member
- Join Date
- Oct 2011
- Posts
- 16
- Rep Power
- 0
Re: a little problem
no ... when i click the save button it say "Success" but all the txt field was blank..
- 02-05-2012, 03:56 AM #4
Member
- Join Date
- Feb 2012
- Posts
- 31
- Rep Power
- 0
Re: a little problem
Ah, okay, try this.
The way you were running it as "&&" was meaning it will ONLY run the error message if every single JTextField had " " within it.Java Code:if(e.getSource() == btn){ if(emp.equals("") || Fname.equals("") || Lname.equals("") || Mname.equals("") || Dep.equals("") || Pos.equals("") ) { JOptionPane.showMessageDialog(null,"Sorry, fill all the blank"); } else { JOptionPane.showMessageDialog(null,"Succes"); }If I have helped you, + rep
- 02-05-2012, 04:48 AM #5
Member
- Join Date
- Oct 2011
- Posts
- 16
- Rep Power
- 0
Re: a little problem
i tried this one ..
But i fill all the blanks then it say : sorry,fill all the blank then Succes..Java Code:if(e.getSource() == btn){ while(emp.equals(" ") || Fname.equals(" ")|| Lname.equals(" ") || Mname.equals(" ") || Dep.equals(" ")|| Pos.equals(" ") ) { JOptionPane.showMessageDialog(null,"Sorry, fill all the blank"); break; } JOptionPane.showMessageDialog(null,"Succes");
-
Re: a little problem
No, don't use a while loop of that nature in a Swing application. Use your if/else block just as you were doing before. Also, this:
if (Lname.equals(" "))
is unreliable and unstable. For one, you don't initialize your JTextFields with the " " String, so this will likely always equate to false. And even if you did that, if the user deletes the space and you're left with a "" String, is it now considered a "good" input? Perhaps you want to instead do
if (Lname.trim().isEmpty())
or something similar.
Also, you should follow Java naming conventions in that all variable names should start with a lower-case letter and should use "camel" case. All class names should start with a capital letter as well.


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks