Page 2 of 2 FirstFirst 12
Results 21 to 34 of 34
  1. #21
    JavaTrouble is offline Member
    Join Date
    May 2010
    Posts
    16
    Rep Power
    0

    Default

    i've done it another way and it works.

    thanks for all your replies

  2. #22
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,762
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by JavaTrouble View Post
    Tell me, is the question not clear? if not please let me know
    I've been reading this thread and following its progression but no, your question isn't clear. It started off nice and simple but then you obfuscated matters by writing that a getText() method wasn't recognised, next it was and the thread went on and on. The check is as simple as: get the text, check if the String is empty; if so refuse to do anything.

    kind regards,

    Jos

  3. #23
    JavaTrouble is offline Member
    Join Date
    May 2010
    Posts
    16
    Rep Power
    0

    Default

    its all done now jos. I did know the simple sequence of "get the text, check if the String is empty; if so refuse to do anything" but I didn't know how to put it into code thats why I came here and asked.
    I'm sorry if i sound grumpy but that code's driven me crazy

  4. #24
    cselic is offline Senior Member
    Join Date
    Apr 2010
    Location
    Belgrade, Serbia
    Posts
    278
    Rep Power
    5

    Default

    I have written small program, for validation. :cool:

    Java Code:
    import java.awt.BorderLayout;
    import java.awt.Dimension;
    import java.awt.GridLayout;
    
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JOptionPane;
    import javax.swing.JPanel;
    import javax.swing.JTextField;
    import javax.swing.SwingUtilities;
    
    public class Validations {
    	public static void main(String[] args) {
    		SwingUtilities.invokeLater(new Runnable() {
    			
    			@Override
    			public void run() {
    				ValidationFrame frame = new ValidationFrame();
    				frame.showUI();
    			}
    		});
    	}
    }
    
    class ValidationFrame extends JFrame {
    	JButton validateButton;
    	ValidationPanel panel;
    	public ValidationFrame() {
    		panel = new ValidationPanel();
    		getContentPane().add(panel,BorderLayout.CENTER);
    	
    	validateButton = new JButton("Validate");
    	
    	validateButton.addActionListener(new ActionListener() {
    		public void actionPerformed(ActionEvent e) {
    			for(int i = 0; i < panel.getFormFields().length; i++)
    				if(panel.get(i).getText().isEmpty())
    					JOptionPane.showMessageDialog(null, 
    						("NOT VALIDATED!\n Field " + i + " is empty."));
    		}
    	});
    		
    	add(validateButton, BorderLayout.SOUTH);
    	
    	}
    	
    	public void showUI() {
    		setTitle("Validation Form");
    		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		setPreferredSize(new Dimension(250,170));
    		pack();
    		setVisible(true);
    	}
    }
    
    class ValidationPanel extends JPanel {
    
    	JLabel firstNameLabel;
    	JLabel secondNameLabel;
    	JLabel phoneNumber;
    	JTextField[] fields;
    	
    	public ValidationPanel() {
    		fields = new JTextField[3];
    		setLayout(new GridLayout(4,2));
    		firstNameLabel = new JLabel("First name: ");
    		secondNameLabel = new JLabel("Second name: ");
    		phoneNumber = new JLabel("Phone number: ");
    		
    		fields[0] = new JTextField(15);
    		fields[1] = new JTextField(15);
    		fields[2] = new JTextField(15);
    		
    		add(firstNameLabel);
    		add(fields[0]);
    		add(secondNameLabel);
    		add(fields[1]);
    		add(phoneNumber);
    		add(fields[2]);
    	}
    	
    	public JTextField[] getFormFields() {
    		return fields;
    	}
    	
    	public JTextField get(int index) {
    		return fields[index];
    	}
    }

  5. #25
    JavaTrouble is offline Member
    Join Date
    May 2010
    Posts
    16
    Rep Power
    0

    Default

    Thank you very much, very helpful code
    appreciated:)

  6. #26
    Aseem is offline Senior Member
    Join Date
    Mar 2009
    Location
    USA
    Posts
    127
    Rep Power
    0

    Default

    Quote Originally Posted by JavaTrouble View Post
    vague question?
    How do you prevent the user from leaving empty/blank fields in a swing form?
    What functions do i use? I just want to see something simple as i've search for days and no luck. Please do take into account that it might be something too simple but i am quite new

    Tell me, is the question not clear? if not please let me know
    running example....just copy and paste...
    import javax.swing.JFrame;
    import javax.swing.*;
    import javax.swing.border.*;
    import java.awt.*;
    import java.awt.event.*;

    public class TestGUI extends JFrame implements ActionListener {
    //instance variables
    private JTextField tfname;
    private JLabel lblname;
    private JLabel lblerror;
    private JPanel panel;
    private JButton btnsubmit;
    private JButton btnexit;
    /**
    * @param args
    */
    public TestGUI(){
    //create your components
    lblname = new JLabel("Name:");
    lblerror = new JLabel();

    tfname = new JTextField(10);
    panel = new JPanel();
    btnsubmit = new JButton("Submit");
    //add actionlistener to buttons so that buttons work after you click it
    btnsubmit.addActionListener(this);
    btnexit = new JButton("Exit");
    btnexit.addActionListener(this);

    //add whatever you have created to panel
    // good practice to add in panel before displaying
    panel.add(lblname);
    panel.add(tfname);
    panel.add(btnsubmit);
    panel.add(btnexit);
    panel.add(lblerror);

    // set the layout for panel...

    panel.setLayout(new GridLayout(3,1));
    // as everything is in panel, add panel to container and display it
    this.getContentPane().add(panel,"North");
    this.setSize(500,300);
    this.show();
    }
    public static void main(String[] args) {
    // TODO Auto-generated method stub
    TestGUI tg = new TestGUI();
    //tg.show();
    }
    // your submit button must work...so we need actionPerformed method
    public void actionPerformed(ActionEvent e){
    if(e.getSource()==btnsubmit){
    // validation is done here
    // if we didnt get any text upon pressing submit button, we are
    //setting error msg to label called lblerror
    if(tfname.getText()==null||tfname.getText().equals ("")){
    lblerror.setText("Field are empty...Please enter again");
    }
    else{
    lblerror.setText("You entered:\t"+tfname.getText());
    }

    }
    if(e.getSource()==btnexit){
    System.exit(0);
    }
    }
    }

  7. #27
    PhHein's Avatar
    PhHein is offline Senior Member
    Join Date
    Apr 2009
    Location
    Germany
    Posts
    1,429
    Rep Power
    7

    Default

    Wow, a spoonfeeding contest.
    Math problems? Call 1-800-[(10x)(13i)^2]-[sin(xy)/2.362x]
    The Ubiquitous Newbie Tips

  8. #28
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,762
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by PhHein View Post
    Wow, a spoonfeeding contest.
    I happen to know a few people who gratefully copy and paste it all, rip it to pieces, glue it together in no particular order and post it here again asking why it doesn't work. Most of the time they sprinkle in "plllllz"s, "URGENT" and a lot of exclamation marks. It's called "programming". ;-)

    kind regards,

    Jos

  9. #29
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,225
    Rep Power
    20

    Default

    That thread has made you a bitter man, Jos...

  10. #30
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,762
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by Tolls View Post
    That thread has made you a bitter man, Jos...
    Not at all; it just surprises me with every reply: if someone knows C++, s/he knows how to pass parameters around and about control flow and about classes and methods and the entire shebang. That thread shows utter ignorance about it all. A happy surprise every day ;-)

    kind regards,

    Jos

  11. #31
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,225
    Rep Power
    20

    Default

    Ah, it's amazement then...

    I do agrre, though. That thread is a wonder to behold.

  12. #32
    cselic is offline Senior Member
    Join Date
    Apr 2010
    Location
    Belgrade, Serbia
    Posts
    278
    Rep Power
    5

    Default

    Wow, a spoonfeeding contest.
    Call it spoonfeeding if you want :D
    That what you are calling "spoonfeeding" in this case I call it:
    'Exemplis Discimus'. This Latin maxim can be translated as 'We learn by examples '. Some people also say: one picture is worth a thousand words.

  13. #33
    PhHein's Avatar
    PhHein is offline Senior Member
    Join Date
    Apr 2009
    Location
    Germany
    Posts
    1,429
    Rep Power
    7

    Default

    Call it what you like, in these forums it's called spoonfeeding.
    Math problems? Call 1-800-[(10x)(13i)^2]-[sin(xy)/2.362x]
    The Ubiquitous Newbie Tips

  14. #34
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,225
    Rep Power
    20

    Default

    Except what happens is they cut and paste and learn nothing.
    Explaining the concepts is preferable to simply handing over code. That is explaining nothing.

Page 2 of 2 FirstFirst 12

Similar Threads

  1. Date formatting
    By bikkerss in forum New To Java
    Replies: 13
    Last Post: 05-08-2010, 05:07 PM
  2. Number formatting
    By gandalf5166 in forum New To Java
    Replies: 7
    Last Post: 03-26-2010, 03:54 AM
  3. Formatting String
    By ersachinjain in forum Advanced Java
    Replies: 13
    Last Post: 10-21-2009, 10:26 PM
  4. formatting..
    By sireesha in forum New To Java
    Replies: 16
    Last Post: 06-26-2009, 08:11 PM
  5. Formatting a toString
    By MooNinja in forum New To Java
    Replies: 8
    Last Post: 03-31-2009, 08:32 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •